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 " 是否正确