【问题标题】:Entity Framework, Stored Procedures,ASP.NET GridView - Insert Empty Row实体框架、存储过程、ASP.NET GridView - 插入空行
【发布时间】:2009-09-11 19:55:17
【问题描述】:

我想在我的网格中添加一个空行。

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

在 GridViewInsert 事件中:

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();

在保存更改时出现此错误:

System.Data.OptimisticConcurrencyException 未被用户代码处理 Message="存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体以来,实体可能已被修改或删除。刷新 ObjectStateManager 条目。" 源="系统.数据.实体" 堆栈跟踪: 在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter 适配器) 在 System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) 在 System.Data.Objects.ObjectContext.SaveChanges(布尔型 acceptChangesDuringSave) 在 System.Data.Objects.ObjectContext.SaveChanges() 在 c:\Documents and Settings\mranganathan\My Documents\Visual Studio 2008\WebSites\Accounting\Default1.aspx.cs:line 68 中的 Default1.GridViewInsert(Object sender, EventArgs e) 在 System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) 在 System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(字符串 eventArgument) 在 System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

【问题讨论】:

    标签: entity-framework stored-procedures gridview entity


    【解决方案1】:

    存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目。 此异常让您知道没有更新任何行。在我的情况下,它发生是因为我忘记添加表字段 ID。而实体框架只是不知道要更新哪些记录。

    【讨论】:

      【解决方案2】:

      我无权访问您的所有代码,但似乎正在发生类似以下情况:

      • 对象在内存中创建
      • 对象被保存到数据库中,其中一个字段被更改
      • 对内存中的对象进行了更改而不从数据库中刷新它
      • 尝试保存更改

      然后你会得到上面的错误。

      数据绑定前是否需要保存更改?

      【讨论】:

        【解决方案3】:

        检查表字段中的数据类型是否符合文档 SQLite (Datatypes In SQLite Version 3 - 2.2 Affinity Name Examples) 中指定的要求。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多