【问题标题】:Gridview update using RowUpdating event from stored procedure error使用存储过程错误中的 RowUpdating 事件更新 Gridview
【发布时间】:2013-08-27 08:53:39
【问题描述】:

我有一个使用 gridview 的购物车示例。我想使用存储过程只更新一列。但在更新声明中我遇到了问题。

这是我的sql数据源

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

这是我的网格视图

  <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 插入“数量”列、表 ...

【问题讨论】:

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


    【解决方案1】:

    我用更简单的方法解决了这个问题。我用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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2011-12-25
      相关资源
      最近更新 更多