protected void Button1_Click(object sender, EventArgs e)
{
string strConnection = System.Configuration.ConfigurationSettings.AppSettings[ "strConnection "];
string strCmd= "select count(*) from userInfo where name = ' "+ TextBox1.Text + " ' and password = ' "+ TextBox2.Text + " ' ";
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = strConnection;
SqlCommand myCommand=new SqlCommand(strCmd,myConnection);
myCommand.Connection.Open();
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
myConnection.Close();
if (flag> 0)
{
Session[ "username "]=TextBox1.Text;
Response.Redirect( "main.aspx ");
}
else
{
Label1.Text= "对不起!请输入正确的用户名和密码 ";
TextBox2.Text= " ";
}
}
异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。
源错误:
行 21:
行 22: SqlCommand myCommand=new SqlCommand(strCmd,myConnection);
行 23: myCommand.Connection.Open();
行 24:
行 25: int flag=(int)myCommand.ExecuteScalar();
源文件: c:\Inetpub\wwwroot\Default.aspx.cs 行: 23
要怎么修改?
------解决方案--------------------------------------------------------
换成
<appSettings>
<add key= "strConnection " value= "server=(local);database=vod;Persist Security Info=True;Password=;User ID=sa; " />
</appSettings>
试试