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

时间:2013-08-27 08:53:39

标签: c# asp.net sql gridview stored-procedures

我有一个使用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',表...

1 个答案:

答案 0 :(得分:0)

我用更简单的方法解决问题。我用boundfields改变了我的模板字段。我做了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>