ConnectionString 属性尚未初始化(我是新手,希望能得到各位的指点)
namespace admin
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Info_Lb;
protected System.Web.UI.WebControls.Label Msg;
protected System.Web.UI.WebControls.TextBox txt_name;
protected System.Web.UI.WebControls.RadioButtonList Rbt_sex;
protected System.Web.UI.WebControls.TextBox txt_idno;
protected System.Web.UI.WebControls.TextBox txt_birth;
protected System.Web.UI.WebControls.TextBox txt_address;
protected System.Web.UI.WebControls.TextBox txt_mobile;
protected System.Web.UI.WebControls.TextBox txt_email;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.TextBox txt_pass;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.LinkButton btn_OK;
public string editid;
public string type;
public string BindUser;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void Initializecomponent()
{
this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btn_OK_Click(object sender, System.EventArgs e)
{
string strRealName = this.txt_name.Text.Trim();
string strPassWord = this.txt_pass.Text.Trim();
if(strRealName.Length==0)
{
RequiredFieldValidator1.Enabled=true;
return;
}
if(strPassWord.Length==0)
{
RequiredFieldValidator2.Enabled=true;
}
SqlConnection MyConn = new SqlConnection(ConfigurationSettings.AppSettings[ "strConnection "]);
MyConn.Open();
string sqlstr= "insert into [userinformation](userName,userSex,userAddess,userCard,userEmail,userPhone,userBirthDay, "
+ "values( ' "+this.txt_name.Text + " ', "+ this.Rbt_sex.SelectedValue + ", "
+ this.txt_address.Text + ", "+ this.txt_mobile.Text + ", ' "+ this.txt_email.Text + " ', ' "
+ this.txt_idno.Text + " ', ' "+ this.txt_birth.Text + " ',) ";
SqlCommand MyComm = new SqlCommand(sqlstr, MyConn);
try
{
MyComm = new SqlCommand(sqlstr, MyConn);
MyComm.ExecuteNonQuery();
Msg.Text= "新增用户信息成功! ";
MyComm.Dispose();
MyConn.Dispose();
MyConn.Close();
}
catch(Exception ex)
{
Msg.Text=ex.ToString();
Response.Write(sqlstr);
}
finally
{
if(MyConn.State!=ConnectionState.Closed)
MyConn.Close();
}
}
}
}
这段代码在运行后,能出现注册的页面,但是当添好资料提交后就出现ConnectionString 属性尚未初始化的问题
ConnectionString 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。
源错误:
行 85: }
行 86: SqlConnection MyConn = new SqlConnection(ConfigurationSettings.AppSettings[ "strConnection "]);
行 87: MyConn.Open();
行 88: string sqlstr= "insert into [userinformation](userName,userSex,userAddess,userCard,userEmail,userPhone,userBirthDay, "
行 89: + "values( ' "+this.txt_name.Text + " ', "+ this.Rbt_sex.SelectedValue + ", "
源文件: d:\inetpub\wwwroot\admin\user.aspx.cs 行: 87
堆栈跟踪:
[InvalidOperationException: ConnectionString 属性尚未初始化。]
System.Data.SqlClient.SqlConnection.Open() +433
admin.WebForm1.btn_OK_Click(Object sender, EventArgs e) in d:\inetpub\wwwroot\admin\user.aspx.cs:87
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +108
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292
希望能帮我解答,谢谢了
[解决办法]
ConfigurationSettings.AppSettings[ "strConnection "]
可能是这里有问题吧,建议2.0的用ConfigurationManager类
[解决办法]
看看 ConfigurationSettings.AppSettings[ "strConnection "]
的值是多少?
[解决办法]
<connectionStrings>
<add name= "strConnection " connectionString= "server=.;
database=NorthWind;uid=sa;pwd=sa; " providerName= "System.Data.SqlClient "/>
</connectionStrings>
看看Web.Config里配置
[解决办法]
应该是ConfigurationSettings.AppSettings[ "strConnection "]有问题,上网查一下,vs2.0读取webconfig配置文件变量的方法,应该能解决,我以前也遇到过,我是这么写的.connectstring = ConfigurationManager.ConnectionStrings[ "jckConnectionString "].ToString();
[解决办法]
<add key= "report1 " value= "workstation id=YRJ008;server=localhost;packet size=4096;user id=sa;initial catalog=report;persist security info=True;password=123456 "> </add>
SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "report1 "]);
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection);
DataSet product = new DataSet();
sqlAdapter1.Fill(product, TableName);
return product.Tables[0].Rows[0];