protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sms_connstr = System.Configuration.ConfigurationManager.AppSettings["SMS_dbconn"];
// 建立连接
sms_conn = new SqlConnection(sms_connstr);
sms_conn.Open();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str_pwd = this.mypassword.Text.Trim().Replace("'", "''");
string sqlstr = "select usertype from Users where username='"+username.Text+"'and mypassword=@password";
SqlCommand cmd = new SqlCommand(sqlstr,sms_conn);
cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar,50));
cmd.Parameters["@password"].Value = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str_pwd, "MD5").ToString();//密码加密
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
Session["user"] = this.username.Text.Trim();//管理员用户,Session进行传值
Session["type"] = dr["userType"].ToString().Trim();//管理员类型
FormsAuthentication.RedirectFromLoginPage(username.Text, false);
//sms_conn.Close();
}
else
{
sms_conn.Close();
mypassword.Text = "您必须输入有效的用户名和密码!";
}
}
------解决方案--------------------------------------------------------
请确保你连接数据库的字符串是正确的,并且已经正常读取
应该是con对象的ConnectionString 属性是空的.你把你数据库的连接字符串赋给这个属性试下.
con.ConnectionString = 你的连接字符串;这句写在Open()之前
在WEB.CONFIG的字符串连接
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=10.1.129.36;Initial
Catalog=AMS_DB2007;User ID =sa;password =Me$W9z" providerName="System.Data.SqlClient"/>
</connectionStrings>
页面程序中读取:
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;
------解决方案--------------------------------------------------------
我告诉你吧,你把pageload里面实例化connection的代码挪到click里面去 就好了
------解决方案--------------------------------------------------------
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sd=new SqlDataAdapter(cmd)
DataSet ds=new DataSet();
sd.Fill(ds);
Session["type"] = ds.Tables[0].Rows[0]["userType"].ToString().Trim();//管理员类型
--SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)-- 这些去了,换成上面的
------解决方案--------------------------------------------------------
鼠标点到实例化Connection那里,按下F9(下个断点),然后在按F5(启动调试),然后在F11(单步调试),问题应该是出在Connection实例化的时候 ConnectionString是空值,也就是说
SqlConnection sms_conn = new SqlConnection(sms_connstr);
你的这句代码里面的sms_connstr这个变量是空的,然后做如下调整,测试下:
- C# code
SqlConnection sms_conn = new SqlConnection("Data Source=10.1.129.36;Initial Catalog=AMS_DB2007;User ID =sa;password =Me$W9z");//这里不写变量,直接写死代码,也就是把你config里面的连裤字符串拿到这里来再看看
------解决方案--------------------------------------------------------