读书人

The ConnectionString property has n

发布时间: 2011-12-10 00:07:34 作者: rapoo

The ConnectionString property has not been initialized. 谁来帮我看看一下,就十几行代码
web.config已做好,用户表也建好了,怎么根据用户名和密码来登录后就能看到数据库的内容,代码怎么写
public partial class _Default : System.Web.UI.Page
{
SqlConnection sms_conn;
protected void Page_Load(object sender, EventArgs e)
{
string sms_connstr = System.Configuration.ConfigurationManager.AppSettings[ "sms_dbconn "];
// 建立连接
sms_conn = new SqlConnection(sms_connstr);

}
protected void Button1_Click(object sender, EventArgs e)
{
string str_pwd = this.TextBox1.Text.Trim().Replace( " ' ", " ' ' ");
string sqlstr = "select username from user where username= ' "+ TextBox1.Text+ " 'and password=@password ";
SqlCommand cmd = new SqlCommand(sqlstr,sms_conn);
cmd.Parameters.Add(new SqlParameter( "@password ", SqlDbType.VarChar,50));
sms_conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
Session[ "user "] = this.TextBox1.Text.Trim();//管理员用户,Session进行传值
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
sms_conn.Close();
}

}

[解决办法]
string sms_connstr = System.Configuration.ConfigurationManager.AppSettings[ "sms_dbconn "];

========

可能是这里取道了空值 null

检查键 "sms_dbconn " 是否正确

读书人网 >asp.net

热点推荐