locked
ListView Update Item Problem RRS feed

  • Question

  • User1924570507 posted

    HI

    I use asp.net with c# and EF linq and sql

    I have list view control with Insert and Delete and Update Method.

    my insert and delete worked correctly but i can not update sql with EF code.

    please tell me what is Code for Update Item.

    <asp:ListView runat="server" ID="ListView1" DataKeyNames="Id" InsertItemPosition="LastItem" 
          SelectMethod="ListView1_GetData"   
          InsertMethod="ListView1_InsertItem" 
          DeleteMethod="ListView1_DeleteItem" 
          UpdateMethod="ListView1_UpdateItem" >
          <InsertItemTemplate>
              <tr>
                  <td><asp:Label ID="Label1" runat="server" Text="نام شاخه"/></td>
                  <td><asp:TextBox ID="IName" runat="server" Width="200" Text='<%#Bind("Name") %>'/></td>
              </tr>
    
              <tr>
                  <td><asp:Label ID="Label2" runat="server" Text="ترتيب"/></td>
                  <td><asp:TextBox ID="SortOrder" runat="server" Width="100" Text='<%#Bind("SortOrder") %>'/></td>
              </tr>
    
              <tr>
                  <td style="border-bottom:1px solid black;"></td>
                  <td dir="ltr" style="border-bottom:1px solid black;" >
                      <asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert"/></td>
              </tr>
          </InsertItemTemplate>
          <ItemTemplate>
              <tr>
                  <td><asp:Label ID="Label1" runat="server" Text="نام شاخه"/></td>
                  <td><asp:TextBox ID="TName" runat="server" Width="200" Text='<%#Eval("Name") %>'/></td>
              </tr>
    
              <tr>
                  <td><asp:Label ID="Label2" runat="server" Text="ترتيب"/></td>
                  <td><asp:TextBox ID="SortOrder" runat="server" Width="100" Text='<%#Eval("SortOrder") %>'/></td>
              </tr>
    
              <tr>
                  <td style="border-bottom:1px solid black;"></td>
                  <td dir="ltr" style="border-bottom:1px solid black;">
                      <asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete"/>
                      <asp:Button ID="UpdateButton" runat="server" Text="Update" CommandName="Update"  />
                  </td>
              </tr>
          </ItemTemplate>
          <LayoutTemplate>
              <table runat="server" >
                  <tr runat="server" id="itemPlaceholder" >
                    
                  </tr>
              </table>
          </LayoutTemplate>
    
      </asp:ListView>
    public void ListView1_UpdateItem(int id)
        {
            
            using (var myEntitie = new sbxEntities())
            {
                var genre = (from p in myEntitie.Genres
                             where p.Id == id
                             select p).Single();
               
                myEntitie.SaveChanges();
            }
    
        }



    Wednesday, December 21, 2016 7:52 AM

All replies

  • User-1838255255 posted

    Hi AmirAmeri,

    AmirAmeri

    public void ListView1_UpdateItem(int id)

    {

    using (var myEntitie = new sbxEntities())

    {

    var genre = (from p in myEntitie.Genres

    where p.Id == id

    select p).Single();

    myEntitie.SaveChanges();

    }

    }

    According to this code, I notice you only find this record, if you want to update field value, you need assign a new value to some field before SaveChanges method.

    Best Regards,

    Eric Du

    Thursday, December 22, 2016 9:22 AM