I created a table named as "Assignment" with two columns assignment_id(Primary Key) and assignment_title, both were set to not allow to enter a null value. Here my code below is showing insertion and modification functions. Modification is working fine but as i insert any value in it always generate exception that "Cannot insert the value NULL into column 'assignment_title', table 'news.dbo.Assignment'; column does not allow nulls. INSERT fails." I have attached images to make it clear to you.
Please guide me to solve this problem. Thank you!
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";
}
}
}