[英]Why am I getting this error: The ConnectionString property has not been initialized
[英]Why is there a ConnectionString not initialized error
內容頁:
protected void Page_Load(object sender, EventArgs e)
{
string gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString;
if (Master.showCheck(s))
{
//do something...
}
}
母版頁:
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'
}
}
為什么會出現以下錯誤: The ConnectionString property has not been initialized
更改
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'
}
}
至
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'
}
}
基本上,變量是通過與調用它的位置不同的方法聲明的,因此您只需將其范圍擴大到該類。
如果您將“ gs”作為班級成員,請將內容頁面更改為
protected void Page_Load(object sender, EventArgs e)
{
gs = ConfigurationManager.ConnectionStrings["ging"].ConnectionString;
if (Master.showCheck(s))
{
//do something...
}
}
您所擁有的字符串def隱藏了類成員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.