Click here to Skip to main content
14,216,290 members
Rate this:
Please Sign up or sign in to 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?
Rate this:
Please Sign up or sign in to vote.

Solution 1

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?
fixthebugg 24-Oct-12 11:46am
   
this is the context that contain your entities.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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)



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 24 Oct 2012
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100