【问题标题】:Delete button is deleting random item although I have the right code尽管我有正确的代码,但删除按钮正在删除随机项目
【发布时间】:2015-07-07 13:38:23
【问题描述】:

我有一个从数据库获取数据的网格视图。它还具有删除功能,并设置为根据 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>

这是我的代码。具体删除功能:

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);
    }

【问题讨论】:

    标签: c# asp.net button sql-delete


    【解决方案1】:

    更改代码

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

    来自

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

    【讨论】:

    • 这是正确的,但不只是提供代码,我想请您添加一些解释,说明问题是什么以及为什么您的解决方案可以解决它。这可能看起来微不足道,但如果一开始就犯了错误,那么显然对每个人来说都不是微不足道的。
    • 您已提供 gdview.DataKeys[0].Value.ToString() 它无法理解行索引的实际位置,这就是它使用此 gdview.DataKeys[e.RowIndex] 随机删除的原因它将指定要在哪个 id 上触发事件
    • 是的。您可以在实际答案中添加类似的内容以获得更好的可见性。这将有助于未来的读者。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多