删除按钮删除随机项目,虽然我有正确的代码

本文关键字:删除 代码 按钮 随机 项目 | 更新日期: 2023-09-27 18:01:19

我有一个从数据库获取数据的网格视图。它还具有删除功能,并被设置为根据productID删除一行。然而,当我试图删除一行,是的,它删除一行,但随机。不是我想删掉的那个

这是我的网格视图:
<asp:GridView ID="gdview"  runat="server" AutoGenerateColumns="False"  
    DataKeyNames="ProductID"  OnRowCancelingEdit="gdview_RowCancelingEdit" 
    OnRowDeleting="gdview_RowDeleting" Width="100%" CssClass="table table-hover table-striped" OnRowDataBound="gdview_RowDataBound">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="ProductID" SortExpression="ProductID" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="ProductCategory" DataField="CategoryName" SortExpression="CategoryNaame" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:ImageField HeaderText ="ImageUrl" DataImageUrlField="ImageUrl" SortExpression="ImageUrl" ControlStyle-Width ="10">
        <ControlStyle Width="50px"></ControlStyle>
        </asp:ImageField>
        <asp:BoundField HeaderText="ProductQuantity" DataField="ProductQuantity" SortExpression="ProductQuantity" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="ProductSold" DataField="ProductSold" SortExpression="ProductSold" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="AvailableStock" DataField="AvailableStock" SortExpression="AvailableStock" >
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <%--<asp:CommandField ShowEditButton="True">
            <ItemStyle Width="100px" />
        </asp:CommandField>--%>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete" 
        OnClientClick="return confirm('Confirm Delete?');"></asp:LinkButton>
        </ItemTemplate>
        <ItemStyle Width="100px" />
        </asp:TemplateField>
    </Columns>

,这是我的代码。特别是delete函数:

protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int prodid = int.Parse(gdview.DataKeys[0].Value.ToString());
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        conn.Open();
        da.DeleteCommand = new SqlCommand("delete from Products where ProductID=" + prodid, conn);
        da.DeleteCommand.ExecuteNonQuery();
        conn.Close();
        GetProducts(0);
    }

删除按钮删除随机项目,虽然我有正确的代码

更改代码

int prodid = Convert.ToInt32(gdview.DataKeys[e.RowIndex].Values["ProductID"].ToString());

int prodid = int.Parse(gdview.DataKeys[0].Value.ToString());