Take the 2-minute tour ×

I have my connection string stored in a myGlobals.cs page as below:

/* Connection String */
public static string conString
    get { return _conString; }
    set { _conString = ConfigurationManager.ConnectionStrings["BaseConnectionString"].ToString(); }

I have my code setup in the 4 tier architecture. So I have a Business Object folder, Business Access Layer & Data Access Layer. If I move the connection string into the DAL structure it works fine. Other wise I get this error:

The ConnectionString property has not been initialized

Is the myGlobals.cs class not being included before all this or does it need to be changed around?

Here is my Data Access Layer:

public DataTable Load()
        SqlConnection conn = new SqlConnection(MyGlobals.conString);
        SqlDataAdapter dAd = new SqlDataAdapter("administratorGetAll", conn);
        dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet dSet = new DataSet();
            dAd.Fill(dSet, "AdministratorsTable");
            return dSet.Tables["AdministratorsTable"];
share|improve this question
You're only setting the connection string when it's being... set. So it's never initialized unless you set it (in which case, you ignore the value it's being set to anyway...) –  Cameron Mar 22 '11 at 2:50
add comment

1 Answer

up vote 3 down vote accepted

The property 'setter' doesn't get called automatically; its code is executed when you put the property on the left-hand side of an assignment - MyClass.MyProperty = "new value";

You want to be doing this:

public static string conString 
    get { return ConfigurationManager.ConnectionStrings["BaseConnectionString"]; }
share|improve this answer
Ah I knew it was something simple like that. Thanks a lot man! –  Bry4n Mar 22 '11 at 2:51
add comment

Your Answer


