我使用查詢生成器插入到LeaveApplication表。據說當我點擊申請假時,他們應該有一個消息「申請成功」。相反,我得到這個錯誤:「無法將NULL值插入列‘STAFFID’,表,列不允許空INSERT失敗的語句已終止。」。這可能與我的查詢生成器來解決錯誤有關。但我不知道如何解決它。我將在我的代碼下面附上幾張圖片,並附上錯誤信息。無法將NULL值插入列「 STAFFID」,表
我的輸出:
LeaveApplication DB表:
CREATE TABLE [dbo].[LeaveApplications] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[StaffId] INT NOT NULL,
[LeaveTypeId] INT NOT NULL,
[StartDate] VARCHAR (10) NOT NULL,
[StartLeavePeriodId] INT NOT NULL,
[EndDate] VARCHAR (10) NOT NULL,
[EndLeavePeriodId] INT NOT NULL,
[NumDays] FLOAT (53) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_LeaveApplications_ToStaff] FOREIGN KEY ([StaffId]) REFERENCES [dbo].[Staff] ([StaffId]),
CONSTRAINT [FK_LeaveApplications_ToLeaveType] FOREIGN KEY ([LeaveTypeId]) REFERENCES [dbo].[LeaveType] ([Id]),
CONSTRAINT [FK_LeaveApplications_ToStartLeavePeriod] FOREIGN KEY ([StartLeavePeriodId]) REFERENCES [dbo].[LeavePeriod] ([LeavePeriodId]),
CONSTRAINT [FK_LeaveApplications_ToEndLeavePeriod] FOREIGN KEY ([EndLeavePeriodId]) REFERENCES [dbo].[LeavePeriod] ([LeavePeriodId])
);
ApplyLeave.aspx編碼:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ApplyLeave.aspx.cs" Inherits="BookReservation.ApplyLeave" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<h3>Leave Application</h3>
<asp:Label ID="lblLeaveType" runat="server" CssClass="labelClass" Text="Leave Type:"></asp:Label>
<asp:DropDownList ID="ddlLeaveType" runat="server" Width="200px" DataSourceID="SqlDSLeaveType" DataTextField="Description" DataValueField="Id">
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" CssClass="labelClass" Text="Start Date:"></asp:Label>
<asp:TextBox ID="tbStartDate" runat="server" Width="200px"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtStartDate" runat="server" TargetControlID="tbStartDate" />
<br />
<asp:Label ID="lblStartLeavePeriod" runat="server" CssClass="labelClass" Text="Start Leave Period:"></asp:Label>
<asp:DropDownList ID="ddlStartLeavePeriod" runat="server" Width="110px" DataSourceID="SqlDSStartLeavePeriod" DataTextField="Description" DataValueField="LeavePeriodId">
</asp:DropDownList>
<br />
<asp:Label ID="lblEndDate" runat="server" CssClass="labelClass" Text="End Date:"></asp:Label>
<asp:TextBox ID="tbEndDate" runat="server" Width="200px"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtEndDate" runat="server" TargetControlID="tbEndDate" />
<asp:CompareValidator ID="cvEndDate" runat="server" ErrorMessage="Select a date" ControlToCompare="tbStartDate" ControlToValidate="tbEndDate" ForeColor="Red"></asp:CompareValidator>
<br />
<asp:Label ID="lblEndLeavePeriod" runat="server" CssClass="labelClass" Text="End Leave Period:"></asp:Label>
<asp:DropDownList ID="ddlEndLeavePeriod" runat="server" Width="110px" DataSourceID="SqlDSEndLeavePeriod" DataTextField="Description" DataValueField="LeavePeriodId">
</asp:DropDownList>
<br />
<br />
<asp:Button ID="btnApply" runat="server" Text="Apply Leave" OnClick="btnApply_Click" />
<br />
<br />
<asp:Label ID="lblOutput" runat="server"></asp:Label>
<br /><br />
<asp:SqlDataSource ID="SqlDSLeaveType" runat="server" ConnectionString="<%$ ConnectionStrings:LeaveManagementCS %>" SelectCommand="SELECT * FROM [LeaveType]"></asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDSStartLeavePeriod" runat="server" ConnectionString="<%$ ConnectionStrings:LeaveManagementCS %>" SelectCommand="SELECT * FROM [LeavePeriod]"></asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDSEndLeavePeriod" runat="server" ConnectionString="<%$ ConnectionStrings:LeaveManagementCS %>" SelectCommand="SELECT * FROM [LeavePeriod]"></asp:SqlDataSource> <br />
<asp:SqlDataSource ID="SqlDSApplyLeave" runat="server" ConnectionString="<%$ ConnectionStrings:LeaveManagementCS %>" DeleteCommand="DELETE FROM [LeaveApplications] WHERE [Id] = @Id" InsertCommand="INSERT INTO [LeaveApplications] ([StaffId], [LeaveTypeId], [StartDate], [StartLeavePeriodId], [EndDate], [EndLeavePeriodId], [NumDays]) VALUES (@StaffId, @LeaveTypeId, @StartDate, @StartLeavePeriodId, @EndDate, @EndLeavePeriodId, @NumDays)" SelectCommand="SELECT * FROM [LeaveApplications]" UpdateCommand="UPDATE [LeaveApplications] SET [StaffId] = @StaffId, [LeaveTypeId] = @LeaveTypeId, [StartDate] = @StartDate, [StartLeavePeriodId] = @StartLeavePeriodId, [EndDate] = @EndDate, [EndLeavePeriodId] = @EndLeavePeriodId, [NumDays] = @NumDays WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="StaffId" Type="Int32" />
<asp:ControlParameter ControlID="ddlLeaveType" Name="LeaveTypeId" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="tbStartDate" Name="StartDate" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="ddlStartLeavePeriod" Name="StartLeavePeriodId" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="tbEndDate" Name="EndDate" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="ddlEndLeavePeriod" Name="EndLeavePeriodId" PropertyName="SelectedValue" Type="Int32" />
<asp:Parameter Name="NumDays" Type="Double" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="StaffId" Type="Int32" />
<asp:Parameter Name="LeaveTypeId" Type="Int32" />
<asp:Parameter Name="StartDate" Type="String" />
<asp:Parameter Name="StartLeavePeriodId" Type="Int32" />
<asp:Parameter Name="EndDate" Type="String" />
<asp:Parameter Name="EndLeavePeriodId" Type="Int32" />
<asp:Parameter Name="NumDays" Type="Double" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource><br />
</asp:Content>
個ApplyLeave.aspx.cs編碼:
public partial class ApplyLeave : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CalExtStartDate.StartDate = DateTime.Today;
}
protected void btnApply_Click(object sender, EventArgs e)
{
try
{
SqlDSApplyLeave.Insert();
lblOutput.Text = "Application success";
}
catch(Exception ex)
{
lblOutput.Text = ex.Message;
}
}
}
InsertQuery:
似乎很清楚,你是不是提供一個STAFFID值。解決方案是爲StaffId提供一個值,或者在創建列時刪除「not null」。 – mason
您需要提供STAFFID爲這對維護表之間的引用完整性的值。如果您允許StaffId的空值,那麼在此表中填寫此列沒有任何意義,因爲不能保證您始終從此處獲得staffID。 – Dileep
你已經引用了LeavApplications中的staffid,所以你應該只將數據插入到員工表中的數據中,因此你不能插入空值只是檢查員工ID的值你是怎麼傳遞的 –