gpt4 book ai didi

asp.net - 登录不正确时显示消息

转载 作者:行者123 更新时间:2023-12-03 07:58:57 26 4
gpt4 key购买 nike

我已经尝试过此代码

当我使用不正确的用户名和密码登录时,它重定向到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());

}

}

登录方法
  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”的第一个参数
可以找到(您是否缺少using指令或程序集引用?)

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

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

最佳答案

更换

 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();
}

尝试这个

关于asp.net - 登录不正确时显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com