0

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.

Grizzly
  • 5,873
  • 8
  • 56
  • 109
Seb
  • 173
  • 3
  • 14
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – yaakov May 05 '16 at 13:11
  • where is the value from database? – Krishna Chaithanya Muthyala May 05 '16 at 13:18
  • I was hoping that the values from the databse were accsessible through ApplicationUser, but in my debugger it turns out as null. Where am I wrong? – Seb May 06 '16 at 09:26

1 Answers1

1
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 test@email.com in your database and someone enters TEST@EMAIL.COM 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.

Grizzly
  • 5,873
  • 8
  • 56
  • 109