templatefield中的下拉列表 - 更新失败(空值插入)

时间:2012-07-10 20:41:26

标签: asp.net data-binding gridview drop-down-menu edititemtemplate

我有一个gridview。在这里我有一个带有DropDownList的模板字段(EditItemTemplate模式下的DDL,ItemTemplate模式下的标签)。 当我点击detailsview的行的编辑时,我可以从DDL中选择任何值(DDL是从sqldatasource填充的),但是如果我尝试执行更新,它会失败,因为它认为我没有提供数据...

这是确切的错误(DB拒绝NULL数据):

  

无法将值NULL插入列'status',表中   'gyumolcs.dbo.orders';列不允许空值。更新失败。该   声明已被终止。

以下是gridview的代码:

<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>" 
    SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date" 
    DeleteCommand="DELETE FROM orders WHERE (ID = @ID)" 
    UpdateCommand="UPDATE orders SET status = @status WHERE (ID = @ID)">
    <DeleteParameters>
        <asp:Parameter Name="ID" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="status" />
        <asp:Parameter Name="ID" />
    </UpdateParameters>
</asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="orderadminSqlDataSource" DataKeyNames="ID" Width="608px" 
        AllowPaging="True" AllowSorting="True" PageSize="15">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" 
                SortExpression="ID" InsertVisible="False" ReadOnly="True" >
            <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="UserName" HeaderText="Felhasználónév" 
                SortExpression="UserName" >
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="Quantity" HeaderText="Mennyiség (kg)" 
                SortExpression="Quantity" >
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="Product" HeaderText="Termék" 
                SortExpression="Product" >
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="Rendelés státusz" SortExpression="status">
                <EditItemTemplate>
                    <!--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>-->

                    <asp:DropDownList ID="DropDownList1" runat="server" 
                        DataSourceID="statustypeDDLSqlDataSource" DataTextField="name"
                        DataValueField="ID">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>" 
                        SelectCommand="SELECT [ID], [name] FROM [status]">
                    </asp:SqlDataSource>

                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:BoundField DataField="Date" HeaderText="Dátum" SortExpression="Date" />
        </Columns>
        <PagerSettings PageButtonCount="15" Mode="NumericFirstLast" />
    </asp:GridView>

这是orderadminSqlDataSource的代码(gridview的数据源)

<!-- language: c# -->
    <asp:SqlDataSource ID="orderadminSqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>" 
        SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date" 
        DeleteCommand="DELETE FROM orders WHERE (ID = @ID)" 
        UpdateCommand="UPDATE orders SET status = @status WHERE (ID = @ID)">
        <DeleteParameters>
            <asp:Parameter Name="ID" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="status" />
            <asp:Parameter Name="ID" />
        </UpdateParameters>
    </asp:SqlDataSource>

请帮助我,我无法弄清楚问题。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您忘记设置DropDown的SelectedValue属性:

<asp:DropDownList ID="DropDownList1"
                  runat="server" 
                  DataSourceID="statustypeDDLSqlDataSource"
                  DataTextField="name"
                  DataValueField="status"
                  SelectedValue='<%# Bind("status") %>'
                  AppendDataBoundItems="True" >

更改statustypeDDLSqlDataSource的代码:

<asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>" 
                   SelectCommand="SELECT [ID] as status, [name] FROM [status]">
</asp:SqlDataSource>

你也可以简化这个(不需要括号):

UPDATE orders SET [status] = @status WHERE [ID] = @ID

注意:我必须重命名查询中的ID列,以使其符合您在此处定义的status参数:

<UpdateParameters>
    <asp:Parameter Name="status" />
    <asp:Parameter Name="ID" />
</UpdateParameters>

我上次编辑和尝试:D

仔细查看GridView的SELECT语句后,我会看到错误所在:

SELECT orders.ID,
       aspnet_Users.UserName,
       orders.quantity AS Quantity,
       product.name AS Product,
       status.name AS Status,
       orders.date AS Date
FROM orders
INNER JOIN product ON
orders.ordertype = product.ID
INNER JOIN status ON orders.status = status.ID
INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId
ORDER BY date

将其更改为:

SELECT orders.ID,
       aspnet_Users.UserName,
       orders.quantity AS Quantity,
       product.name AS Product,
       status.ID AS status,
       status.name AS StatusName,
       orders.date AS Date
FROM orders
INNER JOIN product ON
orders.ordertype = product.ID
INNER JOIN status ON orders.status = status.ID
INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId
ORDER BY date

现在你必须改变这个部分:

<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%# Bind("StatusName") %>'></asp:Label>
</ItemTemplate>

顺便说一句......这是关于此事的一个很好的循序渐进的教程:

Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control