0

I've got a simple form with 3 text boxes and and a button. All I want to do is send the data in the form to my database. In my database I have a table called contacts with three columns (ID-Primary Key,fname,lname,phone) I've created an Ado.net Entity framework model and called it Contacts.

This is the code I've got so far:

  protected void Button1_Click(object sender, EventArgs e)
{
    Contact con = new Contact();
    con.fname = TextBox1.Text;
    con.sname = TextBox2.Text;
    con.phone = TextBox3.ToString();

    ContactDb db = new ContactDb();
    db.Contacts.Add(con);
    db.SaveChanges();
}

When I click ok this error comes up:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

user3120015
  • 181
  • 4
  • 5
  • 17

2 Answers2

0

EntityValidationErrors occurs usually when we pass NULL value for required fields.

Try passing Primary key ID value as 0 and check.

also you can check which field is causing issue, using this catch statement,

refer Richard answer in this post. EF Code First: How do I see 'EntityValidationErrors' property from the nuget package console?

Community
  • 1
  • 1
0

I'm guessing that con.phone = TextBox3.ToString(); should be TextBox3.Text;

You may have some validation attributed on your ContactDb class that may be worth posting for us to look at as well.

Garrison Neely
  • 3,238
  • 3
  • 27
  • 39
  • How do I check if validation has been attributed? Thanks Dan – user3120015 Mar 26 '14 at 15:40
  • Whoops, meant attributeS. Find the class implementation of ContactDb and update your post with it. If it's big, just include the implementations of the `fname`, `sname`, and `phone` properties. – Garrison Neely Mar 26 '14 at 16:04