简体   繁体   English

无法使用ASP.Net中的GridView删除行

[英]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: 这是我的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: 这是我的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. 我可以使用update命令,但是以某种方式单击删除按钮时,它总是出现“必须声明标量变量“ @Id””错误。 Pls help me :" im getting frustated 请帮我:“我很沮丧

Sorry for bad english, and thanks before 对不起,英语不好,谢谢

Please add DataKeyNames in gridview properties. 请在gridview属性中添加DataKeyNames。 DataKeyNames="Id" DataKeyNames =“ Id”

refer this post : http://www.vkinfotek.com/gridview/gridview-commandfield.html 请参阅这篇文章: 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. 如果是Delete,则按ID进行删除,并且GridView控件最初呈现该行时,将通过ID标识该行。 So you will need to refer to @original_Id. 因此,您将需要引用@original_Id。

Also it is good practice to put enter the values in DataKeyNames property of the GridView. 最好在GridView的DataKeyNames属性中输入值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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