简体   繁体   中英

LINQ Entity Framework Value cannot be null. Parameter name: String

I'm trying to return a collection of view-model objects to my view, from my Entity data-model and get the following error:

Value cannot be null. Parameter name: String Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: String

    Source Error:


Line 22:         {
Line 23:             IEnumerable<CollegeContactViewModel> contacts = db.CollegeContacts.Where(cc => cc.CollegeContactID >0).AsEnumerable().ToList()
Line 24:                 .Select(c => new CollegeContactViewModel()
Line 25:                 {
Line 26:                     Id          = c.CollegeContactID,

Here is my index action

public ActionResult Index()
        {
            IEnumerable<CollegeContactViewModel> contacts = db.CollegeContacts.AsEnumerable().ToList()
                .Select(c => new CollegeContactViewModel()
                {
                    Id          = c.CollegeContactID,
                    Status      = c.CollegeContStatus,
                    Center      = c.CollegeContCenter,
                    Salutation  = c.CollegeContSalutation,
                    FirstName   = c.CollegeContFname,
                    LastName    = c.CollegeContLname,
                    Position    = c.CollegeContPosition,
                    Department  = c.CollegeContDept,
                    Institution = c.CollegeContInstitution,
                    Address = new Address()
                    {
                        AddressLine1 = c.CollegeContAdd1,
                        AddressLine2 = c.CollegeContAdd2,
                        City         = c.CollegeContCity,
                        State        = c.CollegeContSt,
                        PostalCode   = c.CollegeContZip
                    },
                    Email                = c.CollegeContEmail,
                    Phone                = c.CollegeContPhone,
                    Opt                  = c.CollegeContOpt,
                    ContactDate          = c.CollegeContDate,
                    OnMailingList        = c.CollegeContMailListSingle,
                    NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),
                    DateSent             = c.Datesent.ToString(),
                    DateEntered          = c.DateEntered.ToString(),
                    Reception            = c.Reception,
                    Partner              = c.Partner,
                    Website              = c.SAwebsite,
                    Poster               = c.CollegeContPoster
                });
            return View(contacts.ToList());
        }

The CollegeContactViewModel

public class CollegeContactViewModel
    {
        public int Id { get; set; }
        public string Status { get; set; }
        public string Center { get; set; }
        public string Salutation { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
        public string Department { get; set; }
        public string Institution { get; set; }
        public Address Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Opt { get; set; }
        public string ContactDate { get; set; }
        public bool? OnMailingList { get; set; }
        public int NumberOfCatalogsSent { get; set; }
        public string DateSent { get; set; }
        public string DateEntered { get; set; }
        public string Reception { get; set; }
        public string Partner { get; set; }
        public string Website { get; set; }
        public string Poster { get; set; }
    }

I have the exact same type of code for another entity in my database and it works just fine. Also all the fields, with the exception of the primary key, allow null, so that can't be it. I've also stepped through the code and the Id is getting populated.

Can somebody please tell me why I keep getting this exception?

Scanning your code for a string parameter, this looks like the most likely (only?) candidate:

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),

If c.NumCatalogsSent is null, that is the error you'd get.


I'd try this instead (EF v4 can translate the null-coalescing operator):

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent ?? "0"),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM