登录错误时显示消息

时间:2016-08-31 10:12:28

标签: asp.net login error-handling

我试过这段代码

当我使用错误的用户名和密码登录时,它会重定向到webform2.aspx,我想在其中显示消息错误的用户名和密码

我是怎么做到的?

我正在尝试下面的代码,但它无法正常工作

 protected void Button1_Click(object sender, EventArgs e)
    {
        if(loginmethod(txt_us.Text,txt_pwd.Text)!="NA")
        {
            FormsAuthentication.Initialize();
            String strRole =Assignroles(txt_us.Text);
            FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, txt_us.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strRole, FormsAuthentication.FormsCookiePath);
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
            FormsAuthentication.Encrypt(fat)));
            loginmethod(txt_us.Text, txt_pwd.Text);
            Response.Redirect("WebForm2.aspx");
        }
        else
        {
            Label1.Text = ("Incorrect UserName/Password");
            Label1.Visible = true;
        }
        txt_us.Text = "";
        txt_pwd.Text = "";
    }
    private string loginmethod(string UserName, string Password)
    {
       try
       {
           login_class lg_class = new login_class();
           Entities2 login = new Entities2();
           string logn = Convert.ToString(lg_class.loginfu(UserName, Password).Rows[0]["id"]);
           Session["ID"] = logn.ToString();
           return (logn);
       }
       catch (Exception ex)
       {

           return (ex.Message.ToString());

       }

    }

loginfu方法

  public DataTable loginfunction(string username,string password)
        {
            try
            {
                Entities2 lg = new Entities2();
               List<SP_GetLogin_Result> gr = lg.SP_GetLogin(username, password).ToList();
                DataTable dt = new DataTable();
                dt.Columns.Add("id", typeof(string));
                foreach (var l in gr)
                {
                    dt.Rows.Add(l.id);
                }
                return dt;   
            }

           catch (Exception)
            {
                throw new Exception();

            }

        }

更新

我这样做但显示错误

 Entities2 lg = new Entities2();
        DataTable dt = new DataTable();
        dt=lg.SP_GetLogin(username,password).ToList();
        if (dt.Rows.Count== 0)
        {
            return "NA";
        }
        else
        {
            return dt.Rows[0].ID.ToString();
        }

'System.Data.DataRow'不包含'ID'的定义,也没有扩展方法'ID'接受'System.Data.DataRow'类型的第一个参数 可以找到(你错过了使用指令或程序集引用吗?)

无法将类型'string'隐式转换为'System.Data.DataTable'

'System.Collections.Generic.List'到'System.Data.DataTable'

1 个答案:

答案 0 :(得分:0)

替换

 Entities2 lg = new Entities2();
               List<SP_GetLogin_Result> gr = lg.SP_GetLogin(username, password).ToList();
                DataTable dt = new DataTable();
                dt.Columns.Add("id", typeof(string));
                foreach (var l in gr)
                {
                    dt.Rows.Add(l.id);


          }

通过

Entities2 lg = new Entities2();
     DataTable dt = new DataTable();
    dt = lg.SP_GetLogin(username, password);
    if(dt.rows.count == 0)
    {
    return "NA";
    }
    else
    {
    return dt.rows[0].something. tostring();
    }

试试这个

相关问题