Reputation: 3751
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
Upvotes: 0
Views: 208
Reputation: 2453
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.
Upvotes: 1
Reputation: 4188
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.
Upvotes: 1