I need to use the same variable name to declare the connection string. But when i do this, i will have further error. I declared "SqlConnection sqlCon = new SqlConnection(strCon);" for the first variable, can i use it again? According to my teacher, i should use the same variable.
string strCon = Database.GetConStr();
SqlConnection sqlCon = new SqlConnection(strCon);
try
{ string strSql = "SELECT Name, ID FROM student WHERE Status = 'A' ORDER BY Name";
SqlCommand sqlCmd = new SqlCommand(strSql, sqlCon);
sqlCon.Open();
SqlDataReader reader = sqlCmd.ExecuteReader();
while (reader.Read())
{
ddlStaff.Items.Add(new ListItem(reader["Name"].ToString(), reader["ID"].ToString()));
}
reader.Close();
}
catch (Exception ex)
{
Session["Error"] = "Error in getting ... System Msg: " + ex.Message;
Server.Transfer("Error.aspx");
}
finally
{
if (sqlCon.State == ConnectionState.Open)
sqlCon.Close();
}
string strCon2 = Database.GetConStr();
sqlCon = new SqlConnection(strCon2);
try
{ string strSql2 = "SELECT Desc1, Desc2 FROM Parameter WHERE Paracode1 = 'Test' AND Status = 'A' ORDER BY Desc1";
SqlCommand sqlCmd2 = new SqlCommand(strSql2, sqlCon);
sqlCon.Open();
SqlDataReader reader2 = sqlCmd2.ExecuteReader();
while (reader2.Read())
{
ddlModule.Items.Add(new ListItem(reader2["Desc1"].ToString(), reader2["Desc22"].ToString()));
}
reader2.Close();
}
catch (Exception ex)
{
Session["Error"] = "Error in getting ... System Msg: " + ex.Message;
Server.Transfer("Error.aspx");
}
finally
{
if (sqlCon.State == ConnectionState.Open)
sqlCon.Close();
}
Is it because i cannot use the same variable?