繁体   English   中英

从存储过程错误使用RowUpdating事件进行Gridview更新

[英]Gridview update using RowUpdating event from stored procedure error

我有一个使用gridview的购物车示例。 我只想使用存储过程更新一列。 但是关于更新声明,我有一个问题。

这是我的sqldata源

<asp:SqlDataSource ID="SqlDataCart" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringSolar %>" DeleteCommand="DeleteItemFromCart" DeleteCommandType="StoredProcedure" SelectCommand="getCartbyUserName" SelectCommandType="StoredProcedure" UpdateCommandType="StoredProcedure" UpdateCommand="UpdQuantity">
            <DeleteParameters>
                <asp:Parameter Name="cartID" Type="Int32" />
            </DeleteParameters>
            <SelectParameters>
                <asp:Parameter Name="userName" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="cartID" Type="Int32" />
                <asp:Parameter Name="quantity" Type="Int32" />        
            </UpdateParameters>
        </asp:SqlDataSource>

这是我的gridview

  <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="cartID" DataSourceID="SqlDataCart" OnPreRender="GridView1_PreRender" OnRowDeleting="GridView1_RowDeleting" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" Width="730px" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Product Name">
                <ItemStyle HorizontalAlign="Center" />
                <ItemTemplate>                            
                <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("productName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Quantity">
                <ItemStyle HorizontalAlign="Center" />
                <ItemTemplate>                            
                <asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("quantity")%>'></asp:TextBox>
                </EditItemTemplate>
                </asp:TemplateField>

            <asp:TemplateField HeaderText="Price">
                <ItemStyle HorizontalAlign="Center" />
                <ItemTemplate>                            
                <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("price") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
           <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
    </asp:GridView>

和背后的代码

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox tx1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtQuantity");


        if (GridView1.SelectedDataKey != null)
        {
            int item = Convert.ToInt32(GridView1.SelectedDataKey.Value);

            SqlDataCart.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
            SqlDataCart.UpdateCommand = "UpdQuantity";

            SqlDataCart.UpdateParameters.Clear();
            SqlDataCart.UpdateParameters.Add("cartID", item.ToString());
            SqlDataCart.UpdateParameters.Add("quantity", tx1.Text);
            SqlDataCart.Update();
            SqlDataCart.DataBind();

        }

    }

我有没有问题的删除方法。 我已经测试了我的存储过程,并且它完全可以正常工作,而且我需要什么。 当我使用更新时,出现以下错误:无法将值NULL插入表的“ quantity”列中...

我用更简单的方法解决了问题。 我用boundfields更改了templatefields。 我将不需要更新的内容设为ReadOnly =“ true”,因此删除了隐藏的代码。 现在的工作版本如下所示:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="cartID" DataSourceID="SqlDataCart" OnPreRender="GridView1_PreRender" OnRowDeleting="GridView1_RowDeleting" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" Width="730px">
                    <Columns>
                   <asp:BoundField DataField="productName" HeaderText="Product Name" SortExpression="productName" ItemStyle-HorizontalAlign="Center" InsertVisible="False" ReadOnly="true">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>

 <asp:BoundField DataField="quantity" HeaderText="Quantity" SortExpression="quantity" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="price" DataFormatString="{0:0.00} $" HeaderText="Price" SortExpression="price" ItemStyle-HorizontalAlign="Center" ReadOnly="true">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:BoundField>

                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    </Columns>
 </asp:GridView>

暂无
暂无

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

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