Reputation: 24349
I have a Global.cs within my App_Code.
Here is the variable I have set:
static string _conString;
//Connection String
public static string conString
{
get { return _conString; }
set { _conString = ConfigurationManager.ConnectionStrings["BreakersConnectionString"].ToString(); }
}
When I use Global.conString
in my web form code behind it comes up as null.
What am I doing wrong?
Upvotes: 0
Views: 1170
Reputation: 2714
First,
I believe you should use
ConfigurationManager.ConnectionStrings["BreakersConnectionString"].ConnectionString
http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringsettings.aspx
Second, did you check your web.config to make sure the connection string is there?
Also, you need to return that in your get acessor.
get
{
return ConfigurationManager.ConnectionStrings["BreakersConnectionString"].ConnectionString
}
Upvotes: 2
Reputation: 6916
why do you need to set the conString? You should update to
public static string conString
{
get { return ConfigurationManager.ConnectionStrings["BreakersConnectionString"].ToString(); }
}
Upvotes: 1
Reputation: 887365
Since you never set the property, it's always null
.
In addition, your setter is wrong; when you write Global.conString = "abc"
, the "abc"
is never used.
You almost definitely want to make a readonly property without a backing field that simply returns the connection string from the configuration.
Upvotes: 1