Click here to Skip to main content
15,174,232 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Here's the error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.

I keep getting this error, but I know this code should work. I am using a button to insert the data into a SQL database. Anyone got any suggestions, here's my code?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using USBatchDataModel;

public partial class _Default : System.Web.UI.Page
{
    USBatchDataEntities db = new USBatchDataEntities();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonIn_Click(object sender, EventArgs e)
    {
        Contact con = new Contact();
        con.id = Convert.ToInt32(TextBox1.Text);
        con.fname = TextBox2.Text;
        con.lname = TextBox3.Text;
        con.phone = TextBox4.Text;
        db.Contacts.AddObject(con);
        db.SaveChanges(); 
       
    }
Posted
Comments
Pete O'Hanlon 6-Nov-12 12:06pm
   
What does the table look like that you are trying to insert into? Have you got an ID field in there that you are using as a Primary Key that you haven't set to be an identity column?

try this:


try {
context.SaveChanges();
} catch (OptimisticConcurrencyException) {
context.Refresh(RefreshMode.ClientWins, db.Contacts);
context.SaveChanges();
}
   
Comments
JasonMacD 24-Oct-12 10:03am
   
Where does the context. come from?
[no name] 24-Oct-12 11:46am
   
this is the context that contain your entities.
You could try instantiating USBatchDataEntities db = new USBatchDataEntities(); in the method as close to where you need it rather than the way you have done it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900