I have an error which is
failed-to-enable-constraints-one-or-more-rows-contain-values-violating-non-null
my error is simple but for me it is hard to fix it because my knowledge is not good . which is when i Enter the record by clicking the Addbutton of AddStudent record then the record that i deleted like record StudentID = 1, FirstName =Raju ,LastName=Abbasi
After Delete it again and press save Change even After that when i again try to use the ID=1 means enter the Record with ID=1 that i have been deleted and record the StudentID=1 ,FirstName =Ram ,LastName=Rakish .Then error is occur which is
private static SqlDataAdapter CreateSutdentDataAdapter()
{
string gettSQL = "SELECT * FROM Student";
string insertSQL = "SET IDENTITY_INSERT Student ON;INSERT INTO Student(StudentID, FirstName,LastName,Gender,GPA,MyImage)" +
"VALUES (@StudentID,@FirstName,@LastName,@Gender,@GPA,@MyImage);SET IDENTITY_INSERT Student OFF";
string updateSQL = "UPDATE Student SET FirstName=@FirstName,LastName=@LastName ,Gender=@Gender, MyImage=@MyImage," +
" GPA=@GPA WHERE StudentID=@StudentID";
string deleteSQL = "DELETE FROM Student WHERE StudentID=@StudentID";
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = new SqlCommand(gettSQL, ConnectionManager.GetConnection());
dataAdapter.InsertCommand = new SqlCommand(insertSQL, ConnectionManager.GetConnection());
dataAdapter.InsertCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID";
dataAdapter.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar,25 ).SourceColumn = "FirstName";
dataAdapter.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 25 ).SourceColumn = "LastName";
dataAdapter.InsertCommand.Parameters.Add("@Gender", SqlDbType.VarChar ,1).SourceColumn = "Gender";
dataAdapter.InsertCommand.Parameters.Add("@GPA", SqlDbType.Float ).SourceColumn = "GPA";
dataAdapter.InsertCommand.Parameters.Add("@MyImage", SqlDbType.VarBinary).SourceColumn = "MyImage";
dataAdapter.UpdateCommand = new SqlCommand(updateSQL, ConnectionManager.GetConnection());
dataAdapter.UpdateCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID";
dataAdapter.UpdateCommand.Parameters.Add("@FirstName", SqlDbType.VarChar,25 ).SourceColumn = "FirstName";
dataAdapter.UpdateCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 25 ).SourceColumn = "LastName";
dataAdapter.UpdateCommand.Parameters.Add("@Gender", SqlDbType.VarChar ,1).SourceColumn = "Gender";
dataAdapter.UpdateCommand.Parameters.Add("@GPA", SqlDbType.Float ).SourceColumn = "GPA";
dataAdapter.UpdateCommand.Parameters.Add("@MyImage", SqlDbType.VarBinary).SourceColumn = "MyImage";
dataAdapter.DeleteCommand = new SqlCommand(deleteSQL, ConnectionManager.GetConnection());
dataAdapter.DeleteCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID";
return dataAdapter;
}
private static void DefinestudentTableSchema(DataTable table)
{
DataColumn StudentIDColumn = table.Columns.Add("StudentID", typeof(string));
StudentIDColumn.AllowDBNull = false;
table.PrimaryKey = new DataColumn[] { StudentIDColumn };
DataColumn StudentFirstName = table.Columns.Add("FirstName", typeof(string));
StudentFirstName.MaxLength = 150;
DataColumn StudentLastName = table.Columns.Add("LastName", typeof(string));
StudentLastName.MaxLength = 150;
DataColumn StudentGender = table.Columns.Add("Gender", typeof(string ));
DataColumn StudentGPA = table.Columns.Add("GPA", typeof(string ));
DataColumn StudentImage = table.Columns.Add("MyImage", typeof(Byte[]));
}
private static DataSet CreateStudentTrackerDataSet()
{
DataSet StudentTrackerDataSet = new DataSet();
DataTable StudentTable = StudentTrackerDataSet.Tables.Add("Student");
DefinestudentTableSchema(StudentTable);
return StudentTrackerDataSet;
}
public static DataSet GetData()
{
DataSet StudentTrakerDataSet = CreateStudentTrackerDataSet();
StudentTrakerDataSet.EnforceConstraints = false;
StudentDataAdapter.Fill(StudentTrakerDataSet.Tables["Student"]);
StudentTrakerDataSet.EnforceConstraints = true;
return StudentTrakerDataSet;
}
public AddModifyStudentRecords(DataSet ds, DataRow row)
{
public static void SaveData(ref DataSet changesDataSet)
{
DataSet addedDataSet = changesDataSet.GetChanges(DataRowState.Added);
if (addedDataSet != null)
{
StudentDataAdapter.Update(addedDataSet.Tables["Student"]);
changesDataSet.Merge(addedDataSet); // Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
}
DataSet modifiedDataSet = changesDataSet.GetChanges(DataRowState.Modified);
if (modifiedDataSet != null)
{
StudentDataAdapter.Update(modifiedDataSet.Tables["Student"]);
changesDataSet.Merge(modifiedDataSet);
}
DataSet deletedDataSet = changesDataSet.GetChanges(DataRowState.Deleted);
if (deletedDataSet != null)
{
StudentDataAdapter.Update(deletedDataSet.Tables["Student"]);
deletedDataSet.Merge(deletedDataSet);
}
And When i terminate the program and re execute the program then i saw that StudntID=1 is also save to Database with ID=1
or When i Delete the StudentID =1 and press save Change after press save Change when i also Terminate the program and re exicute the program and after that when i Enter the StudentID =1 Then no error is occur
And other way without termination is to Delete the record StudentID1 but when you Add the Student record but not Add the StudentID=1 But add the StudentID other then 1 in this case error is also not happend
S0 Please tell me how to fix this error and please for solving this error write some code for me also that i can solve this error Sir Please and thanks to those how replaying me already