Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Same variable name in scope
    primarykey
    data
    text
    <p>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.</p> <pre><code>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(); } </code></pre> <p>Is it because i cannot use the same variable?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload