gpt4 book ai didi

c# - 登录错误消息对象引用

转载 作者:行者123 更新时间:2023-12-03 07:53:48 25 4
gpt4 key购买 nike

我有两个页面,分别称为Login.aspxHome.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("home.aspx");
}
else if (Session["ID"] != null)
{

u = Session["ID"].ToString();
}

else
{
Label1.Text = ("Incorrect UserName/Password");

Label1.Visible = true;
Response.Redirect("home.aspx");
}
txt_us.Text = "";
txt_pwd.Text = "";
}

private string loginmethod(string UserName, string Password)
{
try
{
login_class lg_class = new login_class();
Entities login = new Entities();
string logn = Convert.ToString(lg_class.loginfunction(UserName, Password).Rows[0]["id"]);
Session["ID"] = logn.ToString();
return (logn);
}
catch (Exception ex)
{
return (ex.Message.ToString());
}
}

private string Assignroles(string username)
{
if ((txt_us.Text != string.Empty) && (txt_pwd.Text != string.Empty))
return "";
else
return string.Empty;
}

public DataTable loginfunction(string username, string password)
{
try
{
Entities lg = new Entities();
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();
}
}

并在 home.aspx页面上加载:
    string id;

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
id = Session["ID"].ToString();
this.FFID = "All";
vehcileinfo(id);
}
}
SP_GetLogin返回ID,即当我输入用户名 abc和pssword 1224时,此返回ID CU-2343
当我使用正确的用户名和密码输入时,此重定向到 home.aspx
但是当我输入错误的用户名或密码时,这会在此行显示错误
u = Session["ID"].ToString();

System.NullReferenceException: Object reference not set to an instance of an object.



我想在 login.aspx上显示错误消息
“错误的用户名和密码”

我该怎么做?

最佳答案

根据评论中的讨论,获取NullReferenceException的异常(exception)

这意味着,您正在尝试使用空值。这意味着您要么将其设置为null,要么根本不将其设置为任何值。

因此,只需检查Home.aspx的Page_Load

if(Session["ID"] != null)
{
id = Session["ID"].ToString();
}

else
{
// Do logic here
}

关于c# - 登录错误消息对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39674026/

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