其他页面是从定义的基类继承的,以下是基类
public class Base: System.Web.UI.Page
{
public string strStyleCss,strSchoolName,strBanner,strFooter;
public static string strConn=ConfigurationSettings.AppSettings["strConn"];
public SqlConnection myConn=new SqlConnection(strConn); public Base()
{
//提取CSS路径
string strSQL="select * from xStyle where isDefault=1";
SqlCommand myCMD=new SqlCommand(strSQL,myConn);
myConn.Open();
SqlDataReader myReader=myCMD.ExecuteReader();
myReader.Read();
strStyleCss=myReader["Path"].ToString();
myReader.Close();
myConn.Close();
//提取Title
strSQL="select * from xConfig";
myCMD=new SqlCommand(strSQL,myConn);
myConn.Open();
myReader=myCMD.ExecuteReader();
myReader.Read();
strSchoolName=myReader["SchoolName"].ToString();
strBanner=myReader["Banner"].ToString();
strFooter="Copyright © 2005 "+strSchoolName;
myReader.Close();
myConn.Close();
}
}这个是首页继承基类:
public class Index : HomePage.Base
{
//Code
}但是一旦在.Net中打开首页时就提示:
试图创建Homepage.Base的实例时发生异常,异常为ConnectiongString属性未初始化,确保在项目中生成或引用了页中所使用的所有类。
public class Base: System.Web.UI.Page
{
public string strStyleCss,strSchoolName,strBanner,strFooter;
public static string strConn=ConfigurationSettings.AppSettings["strConn"];
public SqlConnection myConn=new SqlConnection(strConn); public Base()
{
//提取CSS路径
string strSQL="select * from xStyle where isDefault=1";
SqlCommand myCMD=new SqlCommand(strSQL,myConn);
myConn.Open();
SqlDataReader myReader=myCMD.ExecuteReader();
myReader.Read();
strStyleCss=myReader["Path"].ToString();
myReader.Close();
myConn.Close();
//提取Title
strSQL="select * from xConfig";
myCMD=new SqlCommand(strSQL,myConn);
myConn.Open();
myReader=myCMD.ExecuteReader();
myReader.Read();
strSchoolName=myReader["SchoolName"].ToString();
strBanner=myReader["Banner"].ToString();
strFooter="Copyright © 2005 "+strSchoolName;
myReader.Close();
myConn.Close();
}
}这个是首页继承基类:
public class Index : HomePage.Base
{
//Code
}但是一旦在.Net中打开首页时就提示:
试图创建Homepage.Base的实例时发生异常,异常为ConnectiongString属性未初始化,确保在项目中生成或引用了页中所使用的所有类。
1。检查配置文件中的key名字是不是strConn
2、这里读取的配置文件是web目录的web.config而不是基类项目的app.config
参看http://blog.joycode.com/ghj/archive/2005/03/10/45554.aspx获得详细说明
public static string strConn=ConfigurationSettings.AppSettings["strConn"];
public SqlConnection myConn=new SqlConnection(strConn);放到Base()构造函数里面,试下.