I have a question regarding a project I am working on at the moment.
I have this code:
var query = from user in dwe.UsersTable
where user.LoginName.Equals(usernameBox.Text) && user.Password.Equals(pwBox.Text)
select user;
if (query.Count() == 1)
{
Session["User"] = usernameBox.Text;
Response.Redirect("Edit.aspx");
}
else
{
LabelError.Text = "Error try again";
}
}
In my "UsersTable" I have a coulmn named "UserID". I want to send the "userID" as a session to the redirected page (Edit.aspx) the userID must equal the result of comparression between Username and password.
you just need to write down
var query = (from user in dwe.UsersTable
where user.LoginName.Equals(usernameBox.Text) &&
user.Password.Equals(pwBox.Text)
select user).FirstOrDefault();
if(query!=null)
{
Session["User"] = query.UserID;
Response.Redirect("Edit.aspx");
}
else
{
LabelError.Text = "Error try again";
}
No need to write donw code you have which use Count method instead of this just make use Of FirstOrDefault
will give you the result easily.
var query = from user in dwe.UsersTable
where user.LoginName.Equals(usernameBox.Text)
&& user.Password.Equals(pwBox.Text)
select user;
// get user from query
// If SingleOrDefault is not supported (<4.0) use FirstOrDefault instead.
// Thanks Tim Schmelter
var user = query.SingleOrDefault();
if (user != null)
{
Session["UserID"] = user.UserID;
Response.Redirect("Edit.aspx");
}
else
{
LabelError.Text = "Error try again";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With