简体   繁体   中英

Can't delete row using GridView in ASP.Net

Hello all this is my code and im trying to fix it and googled it but i can't find the answer.

This is my SqlDataSource:

<asp:SqlDataSource
      id="SqlDataSource1"
      runat="server"
      ConnectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\xxx.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"
      SelectCommand="SELECT * FROM JarkomTable"
      UpdateCommand="UPDATE JarkomTable SET [Nama]=@Nama, [Kode]=@Kode WHERE [Id]=@Id"
      DeleteCommand="DELETE FROM JarkomTable WHERE [Id]=@Id">
    <UpdateParameters>
        <asp:Parameter Name="Id" />
    </UpdateParameters>
    <DeleteParameters>
        <asp:Parameter Name="Id" />
    </DeleteParameters>
</asp:SqlDataSource>

And this is my GridView:

<asp:GridView
    id="GridView1"
    runat="server"
    DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>           
        <asp:BoundField HeaderText="Id" DataField="Id" />
        <asp:BoundField HeaderText="Nama" DataField="Nama" />
        <asp:BoundField HeaderText="Kode" DataField="Kode" />
        <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
        <asp:CommandField ShowEditButton="True" ButtonType="Button" />
    </Columns>

I can use the update command but somehow when i click the delete button its always appeared "Must declare the scalar variable "@Id"" Error. Pls help me :" im getting frustated

Sorry for bad english, and thanks before

Please add DataKeyNames in gridview properties. DataKeyNames="Id"

refer this post : http://www.vkinfotek.com/gridview/gridview-commandfield.html

may this helps you.

In the case of a Delete you are deleting by ID and the GridView control identifies the row by the ID when it originally rendered the row. So you will need to refer to @original_Id.

Also it is good practice to put enter the values in DataKeyNames property of the GridView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM