开发者

Global ConnectionString not being initialized

开发者 https://www.devze.com 2023-02-18 02:19 出处:网络
I have my connection string stored in a myGlobals.cs page as below: /* Connection String */ public static string conString

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.ConnectionSt开发者_JS百科rings["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();
        try
        {
            dAd.Fill(dSet, "AdministratorsTable");
            return dSet.Tables["AdministratorsTable"];
        }
        catch
        {
            throw;
        }
        finally
        {
            dSet.Dispose();
            dAd.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }


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"]; }
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消