异常详细信息:
System.InvalidOperationException: ConnectionString 属性尚未初始化。
public partial class Ex09_03 : System.Web.UI.Page
{
protected SqlConnection myConnection = new SqlConnection();
private void BindData()
{
string selCmd = "select * from Ex09_GuestBook where(ParentID=0) order by PostTime DESC ";
SqlDataAdapter da = new SqlDataAdapter(selCmd, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "word ");
DataGrid1.DataSource = ds.Tables[ "word "].DefaultView;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect( "Ex09_01.aspx ");
}
else if (!IsPostBack)
{
Label1.Text = "欢迎 " + Session[ "username "].ToString();
string strConn = "server=Localhost;uid=sa;pwd=sa;database=Aspnet ";
myConnection.ConnectionString = strConn;
BindData();
}
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindData();
}
}
在 da.Fill(ds, "word ")这行报错ConnectionString 属性尚未初始化。
------解决方案--------------------protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect( "Ex09_01.aspx ");
}
else if (!IsPostBack)
{
Label1.Text = "欢迎 " + Session[ "username "].ToString();
string strConn = "server=Localhost;uid=sa;pwd=sa;database=Aspnet ";
myConnection.ConnectionString = strConn;
BindData();