I have 8 tabs containing number of records in each one and a function that should count the number of records in each tab and put it in the tab’s header name like the following:
public void count_records(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
string[] commands = {
"SELECT * FROM myTable",
"SELECT * FROM myTable WHERE Status=2",
"SELECT * FROM myTable WHERE Status=3",
"SELECT * FROM myTable WHERE Status=8",
"SELECT * FROM myTable WHERE Status=4",
"SELECT * FROM myTable WHERE Status=7",
"SELECT * FROM myTable WHERE Status=1",
"SELECT * FROM myTable WHERE Status=5"
};
int[] LLCount = new int[commands.Length];
try
{
for (int i = 0; i < commands.Length; i++)
{
SqlCommand cmd = new SqlCommand(commands[i], con);
SqlDataReader reader;
int count = 0;
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
count++;
}
LLCount[i] = count;
myTab1.HeaderText += " (" + LLCount[0] + ")";
myTab2.HeaderText += " (" + LLCount[1] + ")";
myTab3.HeaderText += " (" + LLCount[2] + ")";
myTab4.HeaderText += " (" + LLCount[3] + ")";
myTab5.HeaderText += " (" + LLCount[4] + ")";
myTab6.HeaderText += " (" + LLCount[5] + ")";
myTab7.HeaderText += " (" + LLCount[6] + ")";
myTab8.HeaderText += " (" + LLCount[7] + ")";
}
}
catch (Exception ex) { string ee = ex.Message; }
finally { con.Close(); }
}
Now, the problem I’m facing is that the reader gets the number of records of the first command string correctly "LLCount[0]" but the rest of them are zeros.
EDIT:
I added reader.Close(); and moved the assignment outside the loop but it didn't even show the zeros or anything as before. So, I think it dosen't matter whether you put reader.Close(); or not.