我在我的數據庫中的兩個表如下:如何使此DropDownList在ListView內工作?
Employee表:用戶名,姓名,工作,DivisonCode
司表:DivisionCode,DivisionName
我使用的ListView顯示員工的信息。我沒有顯示DivisionCode,而是放置了一個用於顯示DivsionNames的DropDownList,當然,我將這個DropDownList放置在ListView中的EditItemTemplate中。一切工作正常,除了編輯員工信息。當我試圖改變員工的分工,我得到了以下錯誤:
Cannot insert the value NULL into column 'DivisionCode', table 'psspdbTest.dbo.employee'; column does not allow nulls. UPDATE fails. The statement has been terminated.
我在ASP.NET代碼:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Username"
DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" >
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server"
Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel1" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:TextBox ID="JobTitleTextBox" runat="server"
Text='<%# Bind("JobTitle") %>' />
</td>
<td>
<asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
</td>
<td>
<asp:TextBox ID="EmpOrgTypeTextBox" runat="server"
Text='<%# Bind("EmpOrgType") %>' />
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="DivisionName"
DataValueField="SapCode">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
DeleteCommand="DELETE FROM [Divisions] WHERE [SapCode] = @SapCode"
InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionName]) VALUES (@SapCode, @DivisionName)"
SelectCommand="SELECT * FROM [Divisions]"
UpdateCommand="UPDATE [Divisions] SET [DivisionName] = @DivisionName WHERE [SapCode] = @SapCode">
<DeleteParameters>
<asp:Parameter Name="SapCode" Type="Double" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SapCode" Type="Double" />
<asp:Parameter Name="DivisionName" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DivisionName" Type="String" />
<asp:Parameter Name="SapCode" Type="Double" />
</UpdateParameters>
</asp:SqlDataSource>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
<td>
<asp:TextBox ID="UsernameTextBox" runat="server"
Text='<%# Bind("Username") %>' />
</td>
<td>
<asp:TextBox ID="JobTitleTextBox" runat="server"
Text='<%# Bind("JobTitle") %>' />
</td>
<td>
<asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
</td>
<td>
<asp:TextBox ID="EmpOrgTypeTextBox" runat="server"
Text='<%# Bind("EmpOrgType") %>' />
</td>
<td>
<asp:TextBox ID="DivisionCodeTextBox" runat="server"
Text='<%# Bind("DivisionCode") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit"
Text="Edit" />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server" Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0"
style="">
<tr runat="server" style="">
<th runat="server">
</th>
<th runat="server">
Name</th>
<th runat="server">
Username</th>
<th runat="server">
JobTitle</th>
<th runat="server">
BadgeNo</th>
<th runat="server">
EmpOrgType</th>
<th runat="server">
DivisionCode</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server"
Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.employee.Name, dbo.employee.Username, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.EmpOrgType, dbo.employee.DivisionCode
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode"
DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username"
InsertCommand="INSERT INTO [employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)"
UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle, [BadgeNo] = @BadgeNo, [EmpOrgType] = @EmpOrgType, [DivisionCode] = @DivisionCode WHERE [Username] = @Username">
<DeleteParameters>
<asp:Parameter Name="Username" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Username" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="Double" />
<asp:Parameter Name="EmpOrgType" Type="Double" />
<asp:Parameter Name="DivisionCode" Type="Double" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="Double" />
<asp:Parameter Name="EmpOrgType" Type="Double" />
<asp:Parameter Name="DivisionCode" Type="Double" />
<asp:Parameter Name="Username" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
那麼如何解決這個問題?