简体   繁体   中英

Use Same variable name in scope

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?

You can re-use the same variable name. The problem is that you are declaring it twice. Try removing the "SqlConnection" (the variable type) from the second instance.

You cannot declare the same variable name more than once within same declaration space . But you can very well use it.

try removing SqlConnection in second declaration:
    /*SqlConnection*/ sqlCon = new SqlConnection(strCon2);

If you want to declare the same name then you can define the scope for the variable name using {}

For ex:

{
    SqlConnection sqlCon = new SqlConnection(strCon);
   //use sqlCon 
}//scope ends
//sqlCon  is not available after } 
{ //new scope starts
     SqlConnection sqlCon = new SqlConnection(strCon);
}

try like this:- sqlConnection sqlcon= new sqlConnection(); //first connection sqlcon.connectionstring="connection string1" do some work.... //second sqlcon.connectionstring="connection string 2" ......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM