背景
我正在ASP.net 4.0中使用Visual Studio Professional 2010和C#以SQL Server 2005作为后端构建用户输入表单。此表单是使用Microsoft提供的向导构建的。换句话说,我没有为此编写代码,而是通过单击并填写列表视图的对话框来使用提供给我的选项。我没有以任何方式定制这个。编辑,删除,更新和插入已启用。
此表单使用下拉列表过滤记录。此表单中有两个表:列出辅助医疗人员的人员表和列出其班次偏好的“首选项”表。此表单将过滤特定的护理人员,然后添加多个班次偏好。
问题是当我运行页面并尝试使用Insert命令插入数据时,我收到一条错误消息。错误消息显示为:
无法将值NULL插入“Preference_PK”列表中 'ColdFusion_Apps.dbo.Preferences';列不允许空值。插入 失败。该语句已终止。描述:未处理 在执行当前Web请求期间发生异常。 请查看堆栈跟踪以获取有关错误的更多信息 它起源于代码。
异常详细信息:System.Data.SqlClient.SqlException:无法插入 值为NULL的表'Preference_PK',表 'ColdFusion_Apps.dbo.Preferences';列不允许空值。插入 失败。声明已经终止。
我该如何解决这个问题?我怀疑问题是我的Preferences表的主键列,但是如果我按照所有向导并只采用默认选项,为什么会发生这种情况呢?
用于创建数据库的SQL代码
CREATE TABLE Preferences
(
Preference_PK INT NOT NULL IDENTITY(1,1)
, Name_FK INT NOT NULL
, Preference VARCHAR(20)
)
;
INSERT INTO Preferences (Name_FK, Preference)
VALUES
(1,'Make a Selection')
,(2,'Make a Selection')
,(3,'Make a Selection')
,(4,'Make a Selection')
,(5,'Make a Selection')
;
ALTER TABLE Preferences
ADD CONSTRAINT PreferencePrimaryKey PRIMARY KEY CLUSTERED(Preference_PK)
;
ALTER TABLE Preferences
ADD CONSTRAINT PersonnelForeignKey FOREIGN KEY(Personnel_FK) REFERENCES Personnel(Personnel_PK)
;
CREATE TABLE Personnel
(
Personnel_PK INT NOT NULL IDENTITY(1,1)
, Name VARCHAR(50) NULL
, Title VARCHAR(20) NULL
, DateHired SMALLDATETIME NULL
)
;
INSERT INTO Personnel (Name, Title, DateHired)
VALUES
('Abes, Benjamin','Lieutenant','March 18, 2004')
,('Acton, Traci','Paramedic','May 30, 1991')
,('Adams, Bunny','Paramedic','January 4, 2001')
,('Alcime, Gabner','EMT','April 12, 2007')
,('Angel, Craig','Paramedic','November 5, 1992')
;
ALTER TABLE Personnel
ADD CONSTRAINT PersonnelPrimaryKey PRIMARY KEY CLUSTERED(Personnel_PK)
;
ASP.NET 4.0代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="Personnel_sql"
DataTextField="Name" DataValueField="Personnel_PK">
</asp:DropDownList>
<asp:SqlDataSource ID="Personnel_sql" runat="server" ConnectionString="<%$ ConnectionStrings:ColdFusion_AppsConnectionString %>"
SelectCommand="SELECT [Name], [Personnel_PK] FROM [Personnel]"></asp:SqlDataSource>
<asp:SqlDataSource ID="Preference_sql" runat="server" ConnectionString="<%$ ConnectionStrings:ColdFusion_AppsConnectionString %>"
DeleteCommand="DELETE FROM [Preferences] WHERE [Preference_PK] = @Preference_PK"
InsertCommand="INSERT INTO [Preferences] ([Personnel_FK], [Preference], [Preference_PK]) VALUES (@Personnel_FK, @Preference, @Preference_PK)"
SelectCommand="SELECT Preference_PK, Personnel_FK, Preference FROM Preferences WHERE (Personnel_FK = @Personnel_FK)"
UpdateCommand="UPDATE [Preferences] SET [Personnel_FK] = @Personnel_FK, [Preference] = @Preference WHERE [Preference_PK] = @Preference_PK">
<DeleteParameters>
<asp:Parameter Name="Preference_PK" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Personnel_FK" Type="Int32" />
<asp:Parameter Name="Preference" Type="String" />
<asp:Parameter Name="Preference_PK" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Personnel_FK" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Personnel_FK" Type="Int32" />
<asp:Parameter Name="Preference" Type="String" />
<asp:Parameter Name="Preference_PK" Type="Int32" />
<asp:Parameter Name="Preference_PK" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Preference_PK" DataSourceID="Preference_sql"
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="Preference_PKLabel" runat="server" Text='<%# Eval("Preference_PK") %>' />
</td>
<td>
<asp:Label ID="Personnel_FKLabel" runat="server" Text='<%# Eval("Personnel_FK") %>' />
</td>
<td>
<asp:Label ID="PreferenceLabel" runat="server" Text='<%# Eval("Preference") %>' />
</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:Label ID="Preference_PKLabel1" runat="server" Text='<%# Eval("Preference_PK") %>' />
</td>
<td>
<asp:TextBox ID="Personnel_FKTextBox" runat="server" Text='<%# Bind("Personnel_FK") %>' />
</td>
<td>
<asp:TextBox ID="PreferenceTextBox" runat="server" Text='<%# Bind("Preference") %>' />
</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="Preference_PKTextBox" runat="server" Text='<%# Bind("Preference_PK") %>' />
</td>
<td>
<asp:TextBox ID="Personnel_FKTextBox" runat="server" Text='<%# Bind("Personnel_FK") %>' />
</td>
<td>
<asp:TextBox ID="PreferenceTextBox" runat="server" Text='<%# Bind("Preference") %>' />
</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="Preference_PKLabel" runat="server" Text='<%# Eval("Preference_PK") %>' />
</td>
<td>
<asp:Label ID="Personnel_FKLabel" runat="server" Text='<%# Eval("Personnel_FK") %>' />
</td>
<td>
<asp:Label ID="PreferenceLabel" runat="server" Text='<%# Eval("Preference") %>' />
</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">
Preference_PK
</th>
<th runat="server">
Personnel_FK
</th>
<th runat="server">
Preference
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</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="Preference_PKLabel" runat="server" Text='<%# Eval("Preference_PK") %>' />
</td>
<td>
<asp:Label ID="Personnel_FKLabel" runat="server" Text='<%# Eval("Personnel_FK") %>' />
</td>
<td>
<asp:Label ID="PreferenceLabel" runat="server" Text='<%# Eval("Preference") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
</form>
</body>
</html>
问题
答案 0 :(得分:0)
请尝试使用此insert语句。 您不在insert语句中包含标识键。
InsertCommand =“INSERT INTO [Preferences]([Preference], [Preference_PK])VALUES(@ Preference,@ Preference_PK)“