我创建了一个名为“Assignment”的表,其中包含两列 assignment_id(Primary Key) 和 assignment_title,两者都设置为不允许输入空值。下面我的代码显示了插入和修改功能。修改工作正常,但当我在其中插入任何值时,总是会生成异常“无法将值 NULL 插入列‘assignment_title’,表‘news.dbo.Assignment’;列不允许空值。插入失败。” 我附上了图片以使您清楚。 请指导我解决这个问题。谢谢!
private void dgData_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
//Database context
DataClasses1DataContext objNewContext = new DataClasses1DataContext();
//Creates new object to insert new Record in Database
Assignment objStudentDetail = new Assignment ();
//Check if record is not is table then preoceed to insert
if (!objContext. Assignments.Contains(student))
{
objStudentDetail.assignment_title = string.IsNullOrEmpty(student.assignment_title) ? student.assignment_title : student.assignment_title.Trim();
//This will insert new record in table
objNewContext. Assignments.InsertOnSubmit(objStudentDetail);
//This will update changes in database
objNewContext.SubmitChanges();
//Show message when record is inserted
txtStatus.Text = "Success: Data Inserted";
}
else
{
//this will update changes in database for edit operation in database
objContext.SubmitChanges();
//Show message when record is updated
txtStatus.Text = "Success: Data Updated";
}
}
}