I am trying to set a unique identifier in my customer table from the unique identifer from my membership table (asp.net membership).
However I am getting a null reference exception when doing so. This is the code.
Customer customer = db.Customers.SingleOrDefault(c => c.Id == Convert.ToInt32(formValues["CustomerId"]));
Guid userId = new Guid(user.ProviderUserKey.ToString());
customer.UserId = userId;
It crashes on the last line.
I have tried different settings in my Customer table on the database and in the LINQ to SQL model but I keep getting this.
Anyone know why?
Thanks
You are seeing this because this expression:
is returning
nulland thatnullis being assigned to thecustomervariable. When you usecustomertwo lines later it is throwing theNullReferenceException.The best thing to do here is to debug this code and see what
formValues["CustomerId"]is set to – most likely this value is not set which is causing you to look up a customer in the database for a CustomerId that doesn’t exist.