Jon Harding
Jon Harding

Reputation: 4946

Null Exception when adding to table

I am gathering data from a web-form with the code below:

secondContact.FirstName = txtFirstNameContact2.Text;
secondContact.LastName = txtlastNameContact2.Text;
secondContact.EmailAddress = txtEmailAddressContact2.Text;
secondContact.PhoneNumber = txtPhone1Contact2.Text + txtPhone2Contact2.Text + txtPhone3Contact2.Text;
if (!string.IsNullOrEmpty(rblContactTypeContact2.SelectedValue))
{
    secondContact.SecondaryContactTypeID = Convert.ToInt32(rblContactTypeContact2.SelectedValue);
}
secondContact.ContactPosition = 2;
secondContact.ProspectID = prospect.Id;

Sending the code to the repository to be saved with the code below (which it grabs the prospect.Id successfully):

if (prospect.Id != 0)
   {
    if (!string.IsNullOrEmpty(secondContact.FirstName) || !string.IsNullOrEmpty(secondContact.LastName) || !string.IsNullOrEmpty(secondContact.EmailAddress) || !string.IsNullOrEmpty(secondContact.PhoneNumber))
    {
        this.repository.SaveAltContact(prospect.Id, 2, secondContact);
    }
}

The repository trys to save the data:

public void SaveAltContact(int prospectID, int contactPosition, SecondaryContact contact)
{
    using (var context = new CoyleHomeBuyerEntities())
    {
        SecondaryContact currentAltContact = new Model.SecondaryContact();

        currentAltContact = (from sec in context.SecondaryContact
                             where sec.ContactPosition == contactPosition
                             where sec.ProspectID == prospectID
                             select sec).FirstOrDefault();

        if (currentAltContact == null)
        {
            context.AddToSecondaryContact(currentAltContact);
        }

        currentAltContact.FirstName = contact.FirstName;
        currentAltContact.LastName = contact.LastName;
        currentAltContact.EmailAddress = contact.EmailAddress;
        currentAltContact.PhoneNumber = contact.PhoneNumber;
        currentAltContact.SecondaryContactTypeID = contact.SecondaryContactTypeID;
        currentAltContact.ProspectID = prospectID;
        currentAltContact.ContactPosition = contactPosition;

        context.SaveChanges();
    }
}

It always fails after this line with a Null exception:

context.AddToSecondaryContact(currentAltContact);

I'm baffled, because I expect the query above to return a NULL value at this point. SecondaryContactID is set to be the primary key and to auto increment.

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

Can someone point me in the right direction as to why this is failing when trying to add a new row to the database?

Upvotes: 0

Views: 88

Answers (2)

Adil
Adil

Reputation: 148150

You are adding null as record, Add contact instead of currentAltContact when you do not have record in database. The method AddToSecondaryContact will have contact object to add new record and currentAltContact when you want to update the record.

Change

if (currentAltContact == null)
{
    context.AddToSecondaryContact(currentAltContact);
}

To

if (currentAltContact == null)
{
    context.AddToSecondaryContact(contact);
}

Upvotes: 2

Jon Harding
Jon Harding

Reputation: 4946

Actually I got it to work by simply declaring the currentAltContact variable at the top and instantiating a new object only if there was a null record

public void SaveAltContact(int prospectID, int contactPosition, SecondaryContact contact)
    {
        using (var context = new CoyleHomeBuyerEntities())
        {
                SecondaryContact currentAltContact;

            currentAltContact = (from sec in context.SecondaryContact
                                 where sec.ContactPosition == contactPosition
                                 where sec.ProspectID == prospectID
                                 select sec).FirstOrDefault();

            if (currentAltContact == null)
            {
                currentAltContact = new Model.SecondaryContact();
                context.AddToSecondaryContact(currentAltContact);
            }

            currentAltContact.FirstName = contact.FirstName;
            currentAltContact.LastName = contact.LastName;
            currentAltContact.EmailAddress = contact.EmailAddress;
            currentAltContact.PhoneNumber = contact.PhoneNumber;
            currentAltContact.SecondaryContactTypeID = contact.SecondaryContactTypeID;
            currentAltContact.ProspectID = prospectID;
            currentAltContact.ContactPosition = contactPosition;

            context.SaveChanges();
        }
    }

Upvotes: 0

Related Questions