0

i have a shopping cart example using gridview. i want to update just one column using stored procedure. but on update statement i have a problem.

here is my sqldata source

<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>

here is my 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>

and the code-behind

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

        }

    }

i have delete method which is working without problem. i have tested my stored procedure and it is working without problem exactly how i need. when i am using update i have the following error : Cannot insert the value NULL into column 'quantity', table ...

4

1 回答 1

0

我用更简单的方法解决了这个问题。我用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>
于 2013-08-27T10:30:46.147 回答