private void Button1_Click(object sender, System.EventArgs e)
{
if(!Page.IsPostBack){
string admin = Request["admin"];
string password = Request["pass"];
Session["admin"] = admin;
conn(admin,pass);
} }
private void conn(string t1,string t2){
try
{
string con = ConfigurationSettings.AppSettings["SqlConnectionString"];
SqlConnection conn = new SqlConnection(con);
string sql = "SELECT * FROM admin WHERE 管理员 = @管理员 AND 密码 = @密码";
SqlCommand command = new SqlCommand(sql,conn);
SqlParameter admin = command.Parameters.Add("@管理员",SqlDbType.NVarChar,50);
admin.Value = t1;
SqlParameter pass = command.Parameters.Add("@密码",SqlDbType.NVarChar,50);
pass.Value = t2;
SqlDataReader selreader;
selreader = command.ExecuteReader();
if(selreader.Read() == true)
{
Response.Redirect("Admin_Main.aspx");
}
else
{
selreader.Close();
conn.Close();
Label1.Text = "<font color = red>用户名或者密码错误!</font>";
}
}
catch(SqlException ex)
{
Console.WriteLine("Exception in Main:" + ex.Message);
}
}

解决方案 »

  1.   

    Response.Redirect("Admin_Main.aspx");
    不要放到try{}块里面,不然会报错查询密码不用reader,直接用 command.ExcuteScaler();
      

  2.   

    提示conn(admin,pass)那里有无效参数。
    改写成ExcuteScaler()的正确的代码段应该如何写呢?没用过这个方法。
      

  3.   

    怎么改正嘛,晕,把相关的代码改成stradmin也一样的结果啊。
      

  4.   

    参数错误嘛,你根本就没有pass,是password
      

  5.   

    改成conn(admin,password);也还是不行啊,代码没提示出错但是点击按钮却登陆不了,是怎么回事?