【问题标题】:Update a specific record from a SQL database by selecting the update button from a gridview通过从网格视图中选择更新按钮来更新 SQL 数据库中的特定记录
【发布时间】:2013-04-27 13:58:27
【问题描述】:

我有一个从 SQL 数据库填充的 GridView,gridview 中的每一行都有一个更新按钮。现在我要做的是,当我单击特定行的更新按钮时,我希望我的数据库将该特定行从 active = true 设置为 active = false。如何让我的 Linq 查询在我的数据库中选择特定的数据行并仅更新该项目?

这是我在 gridView 中创建按钮的 asp.net 代码

<telerik:GridTemplateColumn UniqueName="TemplateUpdateColumn">
<ItemTemplate>
<asp:LinkButton ID="UpdateLink" runat="server" CommandName="Update" Text="Update" OnClientClick="return confirm('You are about to update this Incident. \nAre you sure?')"><img src="../Images/icons/Trash.png" class="images" alt="Action" style="margin-left:5px"/></asp:LinkButton><br />Delete
</ItemTemplate>
</telerik:GridTemplateColumn>

这是我的 C# 代码,应该更新我的数据库中的特定记录。

protected void grdIncidents_UpdateCommand(object sender, GridCommandEventArgs e)
    {
       Context db = new Context();

        var cmd = (from i in db.Incidents 
                   where i.IncidentID == //8
                   select i).FirstOrDefault();

        cmd.Active = false;

        db.SaveChanges();
    }

【问题讨论】:

    标签: c# asp.net sql linq radgridview


    【解决方案1】:

    您的源代码与您输入的完全相同吗?如果是这样:

    在cmd.Active = false; 之后尝试Context.Entry(cmd.Active).State = EntityState.Modified;

    这应该告诉 DbContext 在您调用“SaveChanges”时更新该实体上的值。为确保您的更改得到实际保存,我将执行以下操作:

    var updateSuccessful = db.SaveChanges() > 0;
    

    如果 updateSuccessful 等于 true,则更新成功。

    【讨论】:

    • var cmd = db.Incidents.FirstOrDefault(x=> x.IncidentID == /*你的值*/);这不起作用,当我单击删除按钮时,我需要 gridview 知道我指的是哪个事件 ID。所以这段代码应该看起来像这样,我猜测 i.IncidentID == GridView 中记录的当前 ID
    • 在 cmd.Active = false 之后;尝试 Context.Entry(cmd.Active).State = EntityState.Modified 也不起作用 VS 不知道 Context.Entry 是什么
    • “上下文”的父类是什么?我假设它是 DbContext 或其他一些实体框架上下文。此外,在您的问题中,您没有指定代码运行时获得的结果。请您添加该信息吗?
    【解决方案2】:

    你应该试试这样的 sql 命令。 如果你使用实体框架,你应该写 db.Entry(cmd).State = EntityState.Modified; ,在 cmd.Active = false; 之后 还要确保 DB 中的 Active 列实际上是 bool 类型。

    protected void grdIncidents_UpdateCommand(object sender, GridCommandEventArgs e)
    {
       Context db = new Context();
    
        var cmd = db.Incidents.FirstOrDefault(x=> x.IncidentID == /*your value*/);
    
        cmd.Active = false;
        db.SubmitChanges();
    }
    

    【讨论】:

      【解决方案3】:

      检查您获取的对象是否为空。如果为 null 则无法更新。

      protected void grdIncidents_UpdateCommand(object sender, GridCommandEventArgs e) { 上下文 db = new Context();

      Incident cmd = (from i in db.Incidents 
             where i.IncidentID == //8
             select i).FirstOrDefault();
      
          if (cmd !=null)
          {
              cmd.Active = false;
      
              db.SaveChanges();
          }
          else
          //Show some kind of message here like unable to update .
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        • 1970-01-01
        • 2016-07-27
        • 1970-01-01
        相关资源
        最近更新 更多