Reputation: 183
I am currently making a project where users should be able to add other users to a group. I have made a textbox where the user can type in the email adress of the other user he wants to add. My plan is to compare this text with the table in the databse to see if it currently exist.
This is how I am trying to do it in my controller:
[HttpPost]
public ActionResult Manage(GroupManage gm)
{
HttpCookie groupId = new HttpCookie("selectBoxValue");
groupId = Request.Cookies["selectBoxValue"];
int user = Convert.ToInt32(groupId.Value);
if (gm.addUser == gm.ApplicationUser.Email)
{
var groupmember = new GroupUser { ApplicationUserId = gm.ApplicationUser.Id, GroupId = user };
db.GroupUsers.Add(groupmember);
db.SaveChanges();
}
return View();
}
When running this i get the error:
Object reference not set to an instance of an object.
And in my debugger the value of ApplicationUser.Email
is null
(which I am using to compare in my if
statement). Although gm.addUser
contains the correct email address. So I am getting the right input from the textbox. However I do not understand how to compare my input to the database.
Upvotes: 0
Views: 87
Reputation: 5943
if (db.ApplicationUser.Any(x => x.EmailAdress.ToUpper() == gm.addUser.ToUpper())
{
// do something
}
if any email address in the database table ApplicationUser
matches gm.addUser
then do something
The .ToUpper
is used to make the comparison more efficient.. so if you have an email address [email protected] in your database and someone enters [email protected] that will pass and add that to your database.. but the .ToUpper
will change the database value and user value to all caps and compare them that way.
Upvotes: 1