Dropdownlist in templatefield - update fails (null value insertion)

筅森魡賤 提交于 2020-01-03 03:23:08

问题


I have a gridview. In this i have a templatefield with a DropDownList (DDL in EditItemTemplate mode, label in ItemTemplate mode). When i hit edit on of of the detailsview's rows i can select any value from the DDL (the DDL is populated from a sqldatasource), but if i try to execute the update, it fails, because it thinks i didn't supply the data...

Here is the exact error (the DB refuse NULL data):

Cannot insert the value NULL into column 'status', table 'gyumolcs.dbo.orders'; column does not allow nulls. UPDATE fails. The statement has been terminated.

Here is the code for the 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>

And here is the orderadminSqlDataSource's code (the gridview's datasource)

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

Please help me, i can't figure out the problem. Thanks in advance!


回答1:


I think you're forgetting to set the DropDown's SelectedValue property:

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

Change your code for statustypeDDLSqlDataSource:

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

You can also simplify this ( no parenthesis necessary ):

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

Note: I had to rename the ID column in the query so that it conforms with the status parameter you defined here:

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

My last edit and attempt here :D

After carefully looking at your GridView's SELECT statement I see where the error lies:

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

Change it to:

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

Now you must change this part too:

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

By the way... here's a nice step by step tutorial on this matter:

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



来源:https://stackoverflow.com/questions/11421607/dropdownlist-in-templatefield-update-fails-null-value-insertion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!