templatefield中的Dropdownlist-更新失败(插入空值) [英] Dropdownlist in templatefield - update fails (null value insertion)
问题描述
我有一个gridview.在这种情况下,我有一个带有DropDownList的templatefield(在EditItemTemplate模式下为DDL,在ItemTemplate模式下为label). 当我对detailsview的行进行编辑时,我可以从DDL中选择任何值(DDL是从sqldatasource填充的),但是如果我尝试执行更新,它将失败,因为它认为我没有提供数据...
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...
这是确切的错误(数据库拒绝NULL数据):
Here is the exact error (the DB refuse NULL data):
无法将值NULL插入表的状态"列中 'gyumolcs.dbo.orders';列不允许为空. UPDATE失败.这 语句已终止.
Cannot insert the value NULL into column 'status', table 'gyumolcs.dbo.orders'; column does not allow nulls. UPDATE fails. The statement has been terminated.
这是gridview的代码:
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>
这是orderadminSqlDataSource的代码(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!
推荐答案
我认为您忘记设置DropDown的SelectedValue
属性:
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" >
更改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
注意:我必须重命名查询中的ID列,以使其与您在此处定义的status
参数一致:
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>
我最后一次编辑并在这里尝试:D
仔细查看GridView的SELECT
语句后,我发现错误所在:
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
将其更改为:
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:
演练:显示下拉列表在GridView Web服务器控件中编辑时
这篇关于templatefield中的Dropdownlist-更新失败(插入空值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!