views:

1920

answers:

3

I want to add a empty row in my Grid.

<asp:TemplateField>
<ItemTemplate>
    <asp:ImageButton id="Insert" runat="server" CausesValidation="false"
    CommandName="Insert" OnClick="GridViewInsert" 
    ImageUrl="~/Images/Grid/Insert.gif" />

In GridViewInsert Event:

Award_Status_List list = new Award_Status_List();
    list.Award_Status_Code = " ";
    list.Award_Status_Desc = " ";
    list.Is_Active = false;
    list.Job_User = " ";
    list.Job_Name = " ";
    list.Job_Date = DateTime.Now;
    _entity.AddToAward_Status_List(list);
    _entity.SaveChanges();
    GridView2.DataBind();

In save changes I get this error:

System.Data.OptimisticConcurrencyException was unhandled by user code Message="Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries." Source="System.Data.Entity" StackTrace: at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave) at System.Data.Objects.ObjectContext.SaveChanges() at Default1.GridViewInsert(Object sender, EventArgs e) in c:\Documents and Settings\mranganathan\My Documents\Visual Studio 2008\WebSites\Accounting\Default1.aspx.cs:line 68 at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

A: 

I don't have access to all your code, but it looks like something similar to the following is happening:

  • Object is created in memory
  • Object is saved to database and a one of the fields gets changed
  • A change is made to object in memory without refreshing it from the database
  • Try to save the changes

Then you would get the above error.

Do you need to save changes before databind?

Shiraz Bhaiji
A: 

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries. This exception is letting you know that no rows were updated. In my case it happened because I forgot to add the table field Id. And the Entity Framework just did not know what records to update.

roman
A: 

Check that the data types in fields of tables conform to the requirements specified in the documentation SQLite (Datatypes In SQLite Version 3 - 2.2 Affinity Name Examples).

m-r Tarakanoff