0

Content page:

protected void Page_Load(object sender, EventArgs e) { string gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString; if (Master.showCheck(s)) { //do something... } } 

MasterPage:

string gs = ""; protected void Page_Load(object sender, EventArgs e) { gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString; } public bool showCheck(string strID) { string strCheckIfParentExist = @""; using (SqlConnection scConn = new SqlConnection(gs)) { scConn.Open(); //throws an error: 'The ConnectionString property has not been initialized' } } 

Why do I receive the following error: The ConnectionString property has not been initialized

10
  • 1
    I suspect because your connection string is empty, so gs has no value. Some debugging would be able to check that. Commented Mar 27, 2015 at 20:02
  • I have the gs set in the page load for the MasterPage. Do I need to add another one inside the showCheck() function? Commented Mar 27, 2015 at 20:03
  • do you have the ConnectionStrings tag setted in web.config file? Commented Mar 27, 2015 at 20:05
  • What does the connection look like in your web.config? Commented Mar 27, 2015 at 20:05
  • 1
    Shouldn't that "gs" be a class member not a local variable? the code as posted does not seem to be compilable. Commented Mar 27, 2015 at 20:14

2 Answers 2

1

change

protected void Page_Load(object sender, EventArgs e) { string gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString; } public bool showCheck(string strID) { string strCheckIfParentExist = @""; using (SqlConnection scConn = new SqlConnection(gs)) { scConn.Open(); //throws an error: 'The ConnectionString property has not been initialized' } } 

To

private string gs = ""; protected void Page_Load(object sender, EventArgs e) { gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString; } public bool showCheck(string strID) { string strCheckIfParentExist = @""; using (SqlConnection scConn = new SqlConnection(gs)) { scConn.Open(); //throws an error: 'The ConnectionString property has not been initialized' } } 

Basically your variable is getting declared in a different method from where you're calling it, so you just need to increase it's scope to the class.

Sign up to request clarification or add additional context in comments.

4 Comments

If he is really going to go that route, he should declare it as a private readonly that he can inject throughout the page life cycle.
I have it as string gs = ""; as a global variable.
@SearchForKnowledge You should do: private readonly string dbConnection = ConfigurationManager.ConnectionStrings["..."].ConnectionString; Then you can simply utilize dbConnection. Also, I recommend not storing a connection for a master page.
Greg agreed, that's how I typically do it. SearchForKnowledge then why are you declaring it again in your page load? + that wouldn't compile.
1

If you have "gs" as a class member, change the content page to

protected void Page_Load(object sender, EventArgs e) { gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString; if (Master.showCheck(s)) { //do something... } } 

string def you have there is shadowing the class member.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.