0

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?

0

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I edited to add the get. Haven't noticed that when I looked at it.
1

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.

Comments

1

why do you need to set the conString? You should update to

public static string conString
{
    get { return ConfigurationManager.ConnectionStrings["BreakersConnectionString"].ToString(); }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.