Search code examples
c#code-behindsqlconnection

The ConnectionString property has not been initialized sqlcommand yet code still works


I've always been a fan of "if it ain't broke dont fix it" but my code, although functioning, is throwing the connectionstring property has not been initialized message. similar posts suggest the conneciton string is null--- so I added a if IsNullOrEmpty around my connection open command. which is the line throwing the exception. Note: my connection string was retreived from a database of connection strings. this is the c# code behind file for a .aspx page. Thank you in advance for any suggestions regarding the cause of the exception.

the code:

using (SqlConnection sconn = new SqlConnection(someconnectionstring.Value.ToString()))
        {
            using (SqlCommand scmd = new SqlCommand("mydb.[dbo].[myStoredProc]", sconn))
            {
                scmd.CommandType = CommandType.StoredProcedure;
                scmd.Parameters.Add("@valueX", SqlDbType.VarChar).Value = 2; 
                scmd.Parameters.Add("@returnValue", SqlDbType.Int);
                scmd.Parameters["@returnValue"].Direction = ParameterDirection.Output;
        //testing for null as suggested...
        if (!string.IsNullOrEmpty(sconn.ToString()) || sconn.ToString() != "")   //the ||  != "" may be double work.. not sure
                {
                    sconn.Open();  //code throw exception here but continues to work.
                    SqlDataReader adar = scmd.ExecuteReader();
                    if (adar.HasRows)
                    {

                        while (adar.Read())
                        {
                            hiddenfieldX.Value = adar["valueX"].ToString();
            ...
                        }
                        sconn.Close();
                    }
                }
            }
        }
    }
    catch (SqlException er)
    {
    //The ConnectionString property has not been initialized thrown here
    } 

Solution

  • As the mates stated, the connection string may be null or is not well formed.

    1- Check your connection string if it is correct (You can post it and we can check)

    2- Your code will be better if it looks like this:

    string someconnectionstring = "yourConnectionString";
    
    if (!string.IsNullOrEmpty(someconnectionstring))
    {
        using (SqlConnection sconn = new SqlConnection(someconnectionstring))
        {
            using (SqlCommand scmd = new SqlCommand("mydb.[dbo].[myStoredProc]", sconn))
            {
                scmd.CommandType = CommandType.StoredProcedure;
                scmd.Parameters.Add("@valueX", SqlDbType.VarChar).Value = 2;
                scmd.Parameters.Add("@returnValue", SqlDbType.Int);
                scmd.Parameters["@returnValue"].Direction = ParameterDirection.Output;
    
                sconn.Open();  //code throw exception here but continues to work.
                SqlDataReader adar = scmd.ExecuteReader();
                if (adar.HasRows)
                {
    
                    while (adar.Read())
                    {
                        //...
                    }
                }                
                sconn.Close();
            }
        }
    }