public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cook = Response.Cookies["asp"];
        string value1 = cook["username"].ToString();
        string value2 = cook["password"].ToString();
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=cookie;Integrated Security=True");
        string sql = "select count(*) from abc where username='" + value1 + "' and password='" + value2 + "'";
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        conn.Close();
        if (i == 1)
        {
            Label1.Text = "登陆成功";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("asp");
        cookie.Values.Add("username", TextBox1.Text.Trim());
        cookie.Values.Add("password", TextBox2.Text.Trim());
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=cookie;Integrated Security=True");
        string sql = "select count(*) from abc where username='" + TextBox1.Text.Trim() + "' and password='" + TextBox2.Text.Trim() + "'";
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        conn.Close();
        if (i==1)
        {
             Label1.Text = "登陆成功";
        }
        else
        {
            Label1.Text ="账号或密码错误";
        }
    }
}
这做的一个登陆,错误如下,各位大哥,怎么回事?

解决方案 »

  1.   

    System.Web.HttpCookie newcookie = new HttpCookie("user");
    newcookie.Values["username"] = username.Text;
    newcookie.Values["password"] = Pwd;
    newcookie.Expires = DateTime.Now.AddDays(15);
    Response.AppendCookie(newcookie);
      

  2.   

    page_load()
    {
      if(cookice!=null)
    {
    //dosomething
    }
    }button1_onclick()
    {
    //设置一个过期时间}
      

  3.   

    protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("asp");
            cookie.Values.Add("username", TextBox1.Text.Trim());
            cookie.Values.Add("password", TextBox2.Text.Trim());
    Response.Cookies.Add(cookie);//少了这句
      

  4.   

    1. HttpCookie cook = Response.Cookies["asp"];这个应该换成
    HttpCookie cook = Request.Cookies.Get("asp");这一个才是从客户端发来的信息里去取出。另外这里还应该判断一个cook是否为空。因为如果是第一次访问这里取出的肯定是空值。
    2. HttpCookie cookie = new HttpCookie("asp");
       cookie.Values.Add("username", TextBox1.Text.Trim());
       cookie.Values.Add("password", TextBox2.Text.Trim());
       请问你这个是什么时候发到客户端去的?可以在后面加上:Response.Cookies.Add(cook);
      

  5.   

     string value1 = cook["username"].ToString();
     string value2 = cook["password"].ToString();cook["username"]=null, null.ToString()就出这个错了;
    用之前判断下有没有值吧