tags:

views:

214

answers:

3

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?

+4  A: 

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.

TLiebe
ohs, I've tried that before. but it doesnt work. it gave me this error: "The name 'sqlCon' does not exist in the current context."
Nana
When you last tried was sqlCon declared inside try block ?
aJ
when i remove the sqlConnection from second instance, it is not inside the try block. but i use the variable sqlCon in the try block(but it doesnt affect right? cuz here is not the declaration. or does it affect?)
Nana
It doesn't matter where you use the sqlCon variable - it matters where it is declared. If you declared it BEFORE you entered the first try..catch block then the sqlCon variable should also be available in the second try..catch block. If you post your updated code we might be able to see what went wrong.
TLiebe
i have updated my codes
Nana
+2  A: 

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);
}
aJ
Thanks. I tried the first method before. it didnt work. the second method works perfectly fine. :)
Nana
I note that you have confused "scope" with "declaration space". It is frequently perfectly legal to declare two variables of the same name within the same scope. For example, class C { int x; void M() { int x; } } -- here there are two variables both called x and both declared inside the scope defined by class C. The difference is that they are in different declaration spaces, which makes it legal.
Eric Lippert
okay I will add this clarification.
aJ
A: 

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