2

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. enter image description here enter image description here 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";
                }
            }
        }
1
  • Is your problem already fixed? Commented Jun 29, 2013 at 9:27

1 Answer 1

4

The exception says NULL value is not allowed in assignment_title column. Are you sure there is no NULL in the title of objStudentDetail object ?

Modify the statement below, as in current form it does not protect you from having NULL value:

objStudentDetail.assignment_title = string.IsNullOrEmpty(student.assignment_title) 
                                    ? student.assignment_title 
                                    : student.assignment_title.Trim();

to this one:

objStudentDetail.assignment_title = student.assignment_title == null
                                    ? string.Empty 
                                    : student.assignment_title.Trim();
Sign up to request clarification or add additional context in comments.

7 Comments

Yes Sir. I am sure that there is no NULL in the objStudentDetail object.
@Tameen Malik I don't mean objStudentDetail itself but its property assignment_title. Check the update, it should solve your problem.
Shouldn't that be student.assignment_title == null (all lowercase)?
@Tim: it should be (null lowercased)
Yes sir @JaroslawWaliszko you are right but two more problems generate after modification. Sir i have updated above two images to show you these two problems. 1- when i entered values it assigned 0 to ID and secondly when i stopped debugging and again started it, it showed value of Id but it didn't show entered value. Above two updated images will help you sir to understand it. Thank you sir .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.