I have a fairly simple page where I am loading a lot of drivers into SQL and then reading them from a second page.
There are two buttons on the second page, the first advances the driver by one and the second loops through the list with a small delay.
Both buttons call the same method to read the data and load it into the gridview. The first works fine. The second does not.
I can confirm that the data is being read in the second case. The datatable shows the correct data as it is bound to the gridview.
In the code you can see the method that loads and displays the driver information as well as the two button event handlers.
I have tried a dozen different ways to force this to work and I am stumped.
C# Code Behind
namespace DriverProgression
{
public partial class DriverMove : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void AdvanceDriver()
{
//Get the current value of ID from DriversCSV.DriverSeed
int currID = 0;
string connectionStr = (@"server = MSIIS01\SQLExpress; integrated security = true;
database = DriversCSV; MultipleActiveResultSets=True;");
using (SqlConnection currIDcon = new SqlConnection(connectionStr))
{
SqlCommand command = new SqlCommand("SELECT * FROM DriverSeed;", currIDcon);
command.Parameters.AddWithValue("@currID", 1);
{
currIDcon.Open();
SqlDataReader reader1 = command.ExecuteReader();
while (reader1.Read())
{
currID = reader1.GetInt32(0);
}
reader1.Close();
//currID = currIDInt;
}
currIDcon.Close();
}
//Create Datatable
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("fName");
dt.Columns.Add("lName");
dt.Columns.Add("Country");
using (SqlConnection con = new SqlConnection(connectionStr))
{
con.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM Drivers WHERE ID = " +
currID + ";", con))
{
SqlDataReader reader2 = command.ExecuteReader();
while (reader2.Read())
{
DataRow row = dt.NewRow();
row["Id"] = reader2["ID"];
int currIDInt = reader2.GetInt32(0);
row["fName"] = reader2["fName"];
row["lName"] = reader2["lName"];
row["Country"] = reader2["Country"];
dt.Rows.Add(row);
GridView2.DataSource = dt;
GridView2.DataBind();
currID = currIDInt;
currID++;
UpdatePanel1.Update();
System.Threading.Thread.Sleep(1000);
}
string sqlput = "UPDATE DriverSeed SET Seed = " + currID + ";";
SqlCommand myCommand = new SqlCommand(sqlput, con);
myCommand.ExecuteNonQuery();
}
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int currID;
string connectionStr = (@"server = MSIIS01\SQLExpress; integrated security = true;
database = DriversCSV; MultipleActiveResultSets=True;");
using (SqlConnection currIDcon = new SqlConnection(connectionStr))
{
currIDcon.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM DriverSeed;",
currIDcon))
{
SqlDataReader reader1 = command.ExecuteReader();
while (reader1.Read())
{
currID = reader1.GetInt32(0);
}
currIDcon.Close();
try
{
AdvanceDriver();
}
catch (Exception)
{
throw;
}
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int currID = 0;
string connectionStr = (@"server = MSIIS01\SQLExpress; integrated security = true;
database = DriversCSV; MultipleActiveResultSets=True;");
using (SqlConnection currIDcon = new SqlConnection(connectionStr))
{
currIDcon.Open();
SqlCommand command = new SqlCommand("SELECT * FROM DriverSeed;", currIDcon);
command.Parameters.AddWithValue("@currID", 1);
{
SqlDataReader reader1 = command.ExecuteReader();
while (reader1.Read())
{
currID = reader1.GetInt32(0);
}
currIDcon.Close();
for (int i = currID; i <= 864; i++)
{
try
{
AdvanceDriver();
}
catch (Exception)
{
throw;
}
}
}
}
}
}
}
ASP
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Advance One Driver" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Autorun Drivers" OnClick="Button2_Click" />
</div>
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" EnableRowsCache="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:TextBox ID="ID" runat="server" Text='<%# Eval("ID")%>' ReadOnly="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="fName" runat="server" Text='<%# Eval("fName")%>' ReadOnly="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lName" runat="server" Text='<%# Eval("lName")%>' ReadOnly="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="Country" runat="server" Text='<%# Eval("Country")%>' ReadOnly="True" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Why does it work for one and not the other?