我的代码中存在以下异常。我已经阅读了许多解决方案,但在我的案例中从未使用过。表tblTask
包含列taskId
和Tno
。 taskId
被设置为一个标识列,我为Tno
传递了一个特定值,但出现了以下异常:
SqlException:无法将值NULL插入表'dbTaskManager.dbo.tblTask'的列'Tno'中;列不允许为空。 INSERT失败。
这是桌子设计
CREATE TABLE [dbo].[tblTask] (
[TaskId] NUMERIC (18) IDENTITY (1, 1) NOT NULL,
[ClientId] INT NULL,
[UserName] VARCHAR (50) NULL,
[Task] VARCHAR (MAX) NULL,
[EmpId] INT NULL,
[AssignedDate] DATETIME2 (7) NULL,
[DueDate] DATETIME2 (7) NULL,
[CompletionDate] DATETIME2 (7) NULL,
[EstTime] FLOAT (53) NULL,
[ActualTime] VARCHAR (50) NULL,
[Status] VARCHAR (50) NULL,
[Priority] VARCHAR (50) NULL,
[Remarks] VARCHAR (8000) NULL,
[TechnicalRemarks1] VARCHAR (8000) NULL,
[TechnicalRemarks2] VARCHAR (8000) NULL,
[TechnicalRemarks3] VARCHAR (8000) NULL,
[TaskPercentage] NUMERIC (18, 2) NULL,
[StartTime] DATETIME2 (7) NULL,
[Tno] INT NOT NULL,
[DeliverToday] BIT NULL,
[dt_Status] BIT NULL,
[UpdateStatusNO] INT CONSTRAINT [DF__tblTask__UpdateS__5070F446] DEFAULT ((0)) NULL,
[patchStatus] INT NULL,
[SupportFile] VARCHAR (MAX) NULL,
[Codecommit] INT NULL,
[Tremark1file] VARCHAR (MAX) NULL,
[Tremark2file] VARCHAR (MAX) NULL,
[remarkfile] VARCHAR (MAX) NULL,
[Taskfile] VARCHAR (MAX) NULL,
[Moduleid] INT NULL,
[StatusDate] DATETIME2 (7) NULL,
[ParentID] INT NULL,
[TaskTypeid] INT NULL,
[Projectid] INT NULL,
[SubTaskTypeid] INT NULL,
[Ptaskid] INT NULL,
[DepartmentID] INT NULL,
[SuggesionRemarks] VARCHAR (8000) NULL,
[DevHours] FLOAT (53) NULL,
[Start] INT NULL,
[IsStartDate] INT NULL,
[ReleaseID] NVARCHAR (MAX) NOT NULL,
[Bug] INT NULL,
[Suggestions] INT NULL,
[Approved] INT NULL,
[Movement] INT NULL,
[QAHrs] NUMERIC (18) NULL,
[MiscHrs] NUMERIC (18) NULL,
[summary] VARCHAR (500) NULL,
[Email] VARCHAR (500) NULL,
[LocalClientDetails] VARCHAR (500) NULL,
CONSTRAINT [PK_tblTask] PRIMARY KEY CLUSTERED ([Tno] ASC)
);
GO
CREATE TRIGGER [dbo].[UpdateTblTask] ON dbo.tblTask
for Update
AS
BEGIN
if Update(Status) or Update(EmpID)
begin
insert into tblTaskLog (TaskId,ClientId,UserName,Task,EmpIdFrom,EmpIdTo,AssignedDate,DueDate,EstTime,StatusFrom,StatusTo,SystemName,CreationDate)
select D.TaskId,D.ClientId,D.UserName,D.Task,D.EmpId,I.EmpId,D.AssignedDate,D.DueDate,D.EstTime,D.Status,i.Status,Host_Name(),Getdate()
From Deleted D inner join Inserted I on D.TaskId=I.TaskId
Where D.Status<>I.Status or D.EmpID<>I.EmpID
------------------------------------------------------
---------------------Status Change---------------------
if Update(Status)
begin
Declare @OldStatus varchar(200)
Declare @NewStatus varchar(200)
select @NewStatus=Status from inserted
select @OldStatus=Status from deleted
--select @NewStatus NewStatus,@OldStatus OldStatus
--if((Select status from inserted)<>(select Status from deleted))
if @NewStatus <> @OldStatus
begin
Update tbltask set Statusdate=Getdate() where taskid=(Select Taskid from inserted)
end
end
--------------------------------------------------------------
End
End
GO
DISABLE TRIGGER [dbo].[UpdateTblTask]
ON [dbo].[tblTask];
这是TaskMainDTO.cs
[Table("tblTask")]
public class TaskMainDTO
{
public int? ClientId { get; set; }
public string Username { get; set; }
public string Task { get; set; }
public int? EmpId { get; set; }
public DateTime? AssignedDate { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? CompletionDate { get; set; }
public double? EstTime { get; set; }
public string ActualTime { get; set; }
public string Status { get; set; }
public string Priority { get; set; }
public string Remarks { get; set; }
public string TechnicalRemarks1 { get; set; }
public string TechnicalRemarks2 { get; set; }
public string TechnicalRemarks3 { get; set; }
public decimal? TaskPercentage { get; set; }
public DateTime? StartTime { get; set; }
[Key]
public int Tno { get; set; }
public bool? DeliverToday { get; set; }
public bool? dt_Status { get; set; }
public int? UpdateStatusNo { get; set; }
public int? patchStatus { get; set; }
public string SupportFile { get; set; }
public int? Codecommit { get; set; }
public string Tremark1file { get; set; }
public string Tremark2file { get; set; }
public string remarkfile { get; set; }
public string Taskfile { get; set; }
public int? Moduleid { get; set; }
public DateTime StatusDate { get; set; }
public int? ParentID { get; set; }
public int? TaskTypeid { get; set; }
public int? Projectid { get; set; }
public int? SubTaskTypeid { get; set; }
public int? Ptaskid { get; set; }
public int? DepartmentID { get; set; }
public string SuggesionRemarks { get; set; }
public double? DevHours { get; set; }
public int? Start { get; set; }
public int? IsStartDate { get; set; }
public string ReleaseID { get; set; }
public int? Bug { get; set; }
public int? Suggestions { get; set; }
public int? Approved { get; set; }
public int? Movement { get; set; }
public decimal? QAHrs { get; set; }
public decimal? MiscHrs { get; set; }
public string summary { get; set; }
public string Email { get; set; }
public string LocalClientDetails { get; set; }
这是TaskMainVM.cs
public class TaskMainVM
{
public TaskMainVM()
{
}
public TaskMainVM(TaskMainDTO model)
{
ClientId = model.ClientId;
Projectid = model.Projectid;
Moduleid = model.Moduleid;
TaskTypeid = model.TaskTypeid;
SubTaskTypeid = model.SubTaskTypeid;
EmpId = model.EmpId;
DepartmentID = model.DepartmentID;
Username = model.Username;
Status = model.Status;
Priority = model.Priority;
ReleaseID = model.ReleaseID;
AssignedDate = model.AssignedDate;
DueDate = model.DueDate;
CompletionDate = model.CompletionDate;
StartTime = model.StartTime;
ActualTime = model.ActualTime;
DevHours = model.DevHours;
QAHrs = model.QAHrs;
MiscHrs = model.MiscHrs;
Tno = model.Tno;
summary = model.summary;
Task = model.Task;
Taskfile = model.Taskfile;
LocalClientDetails = model.LocalClientDetails;
Email = model.Email;
}
public int? ClientId { get; set; }
public string Username { get; set; }
public string Task { get; set; }
public int? EmpId { get; set; }
public DateTime? AssignedDate { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? CompletionDate { get; set; }
public double? EstTime { get; set; }
public string ActualTime { get; set; }
public string Status { get; set; }
public string Priority { get; set; }
public string Remarks { get; set; }
public string TechnicalRemarks1 { get; set; }
public string TechnicalRemarks2 { get; set; }
public string TechnicalRemarks3 { get; set; }
public decimal? TaskPercentage { get; set; }
public DateTime? StartTime { get; set; }
public int Tno { get; set; }
public bool? DeliverToday { get; set; }
public bool? dt_Status { get; set; }
public int? UpdateStatusNo { get; set; }
public int? patchStatus { get; set; }
public string SupportFile { get; set; }
public int? Codecommit { get; set; }
public string Tremark1file { get; set; }
public string Tremark2file { get; set; }
public string remarkfile { get; set; }
public string Taskfile { get; set; }
public int? Moduleid { get; set; }
public DateTime StatusDate { get; set; }
public int? ParentID { get; set; }
public int? TaskTypeid { get; set; }
public int? Projectid { get; set; }
public int? SubTaskTypeid { get; set; }
public int? Ptaskid { get; set; }
public int? DepartmentID { get; set; }
public string SuggesionRemarks { get; set; }
public double? DevHours { get; set; }
public int? Start { get; set; }
public int? IsStartDate { get; set; }
public string ReleaseID { get; set; }
public int? Bug { get; set; }
public int? Suggestions { get; set; }
public int? Approved { get; set; }
public int? Movement { get; set; }
public decimal? QAHrs { get; set; }
public decimal? MiscHrs { get; set; }
public string summary { get; set; }
public string Email { get; set; }
public string LocalClientDetails { get; set; }
这是TaskController.cs
// save task data
public JsonResult SaveTaskDataInDb(TaskMainVM model)
{
var result = false;
try
{
using (Db db = new Db())
{
TaskMainDTO dto = new TaskMainDTO();
dto.ClientId = model.ClientId;
dto.Projectid = model.Projectid;
dto.Moduleid = model.Moduleid;
dto.TaskTypeid = model.TaskTypeid;
dto.SubTaskTypeid = model.SubTaskTypeid;
dto.EmpId = model.EmpId;
dto.DepartmentID = model.DepartmentID;
dto.Username = model.Username;
dto.Status = model.Status;
dto.Priority = model.Priority;
dto.ReleaseID = model.ReleaseID;
dto.AssignedDate = model.AssignedDate;
dto.DueDate = model.DueDate;
dto.CompletionDate = model.CompletionDate;
dto.StartTime = model.StartTime;
dto.ActualTime = model.ActualTime;
dto.DevHours = model.DevHours;
dto.QAHrs = model.QAHrs;
dto.MiscHrs = model.MiscHrs;
dto.Tno = 78999;
dto.summary = model.summary;
dto.Task = model.Task;
dto.Taskfile = model.Taskfile;
dto.LocalClientDetails = model.LocalClientDetails;
dto.Email = model.Email;
db.TaskMain.Add(dto);
db.SaveChanges();
result = true;
}
}
catch (Exception ex)
{
throw ex;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:0)
您的表具有以下NOT NULL列TaskId
,Tno
,ReleaseID
。
在进行插入操作时,不必检查列TaskID
,因为此列具有IDENTITY(1,1,)
。这意味着SQL Server将自己关心此列。
但是,其余两个NOT NULL列(例如Tno
,ReleaseID
)应该由您来照顾。因此,如果为这些列分配了空值,则只需使用空合并运算符??
为这些列设置一个后备值:
using (Db db = new Db())
{
TaskMainDTO dto = new TaskMainDTO();
//...
dto.ReleaseID = model.ReleaseID ?? 0;
//...
dto.Tno = 78999;
}
更新:
此行表示如果model.ReleaseID
没有任何值(表示NULL
),则dto.ReleaseID
将设置为0
。 0
是备用值,可防止获取异常SqlException: Cannot insert the value NULL ...
:
dto.ReleaseID = model.ReleaseID ?? 0;
如果您不想设置0
,则可以引发异常:
if(model.ReleaseID == null || dto.Tno == null)
throw new ArgumentNullException("SaveTaskDataInDb(TaskMainVM model) method
received a null argument!");