C# .NET - Concurrency violation: the DeleteCommand affected 0 of the expected 1

Asked By Rajuje Abbasi on 12-Mar-12 04:15 PM

Please any one who can help me i will be very thank full to him Please help me for this error to solve I am new in C# So please Consider my Question It is big but. your few minutes can solve my Problem and i will pray him tell death So please please help  

I have record in my Database which have following Datatype as

StudentID=INT,

FirstName=VarChar,

LastName=VarChar,

Gender=Varchar,

GPA=Float,

MyImage=Vabinary (MAX)

This is my Database type but i add the Datatype in in my Parameter to inserting and Updating is not corrupt i thing please check that is my Datatype is correct for Adding parameter the SqlDataAdapter 

Then I add the Parameter for these type for inserting Updating Deleting record as 

SqlDataAdapter dataAdapter = new SqlDataAdapter();
  dataAdapter.SelectCommand =newSqlCommand(gettSQL,ConnectionManager.GetConnection());
 dataAdapter.InsertCommand=newSqlCommand(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";
           //Create the UpdateCommand and the Parameters
 dataAdapter.UpdateCommand=newSqlCommand(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=newSqlCommand(deleteSQL,ConnectionManager.GetConnection());
 dataAdapter.DeleteCommand.Parameters.Add

("@StudentID",SqlDbType.Int).SourceColumn="StudentID";

 return dataAdapter;

And TableSchima for the Student Table Also Watch is Datatype is wrong are right  

 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(Char));
          DataColumn StudentImage = table.Columns.Add("MyImage", typeof(Byte[]));

And DataSet for the Student is Consider is constraint is right to false and true also 

               

 private static DataSet CreateStudentTrackerDataSet()
           {DataSet StudentTrackerDataSet = new DataSet();
           DataTable StudentTable = StudentTrackerDataSet.Tables.Add("Student");
           DefinestudentTableSchema(StudentTable);
           return StudentTrackerDataSet;}
        //GetDataFunction that Populat the Gridview 
       public static DataSet GetData() 
           DataSet StudentTrakerDataSet = CreateStudentTrackerDataSet();
   StudentTrakerDataSet.EnforceConstraints = false;
           StudentDataAdapter.Fill(StudentTrakerDataSet.Tables["Student"]);  
           StudentTrakerDataSet.EnforceConstraints = true;
           return StudentTrakerDataSet;
And my SaveDatamethode is 
 DataSet addedDataSet = changesDataSet.GetChanges(DataRowState.Added);
           if (addedDataSet != null)
           {
               StudentDataAdapter.Update(addedDataSet.Tables["Student"]);
               changesDataSet.Merge(addedDataSet); 
           }
           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);

Button that will Add the Student Record in Datarow of Dataset is or you can say to Grid is Here i take a mistake I sure About it for Datatype So tell me where i take the Mistake Because My whole program is right but for the error above is due to the Data is not updated correctly in Datarow So Consider and tell me where i take the Mistake for the Datatype 

                             

private void button2_Click(object sender, EventArgs e)
           { string StudentID = textBox1.Text.ToString();
            string StudentFirstName = textBox2.Text.ToString();
            string StudentLastName = textBox4.Text.ToString();
            string Gender = textBox3.Text.ToString();
            string  GPA = textBox5.Text.ToString();
  Image  MyImage = pictureBox1.Image;
            DataTable table = StudentTrackerDataSet.Tables["Student"];
            if (currentRow == null)
            {
                currentRow = table.NewRow();
  currentRow["StudentID"] = textBox1.Text.ToString();
                table.Rows.Add(currentRow );
            }
            currentRow .BeginEdit();
            currentRow ["StudentID" ]=StudentID ;
            currentRow["FirstName"] = StudentFirstName;
            currentRow["LastName"] = StudentLastName;
            currentRow["Gender"] = Gender;
            currentRow["GPA"] = GPA;
            currentRow["MyImage"] = convertToByte(txtBrowseFile.Text);  
            currentRow.EndEdit();
            Close();}
        Please any one who can help me i will be very thank full to him Please help me for this error to solve I am new in C# So please Consider my Question It is big but. your few minutes can solve my Problem and i will pray him tell death So please please help  NOTE:- When i press the save button then Data is also overlapes means last when copy to whole other record Means if last is record have Fristname Raju then whole other record when i press the save button it will be Raju so Please please tell me how to fix it  


Peter Bromberg replied to Rajuje Abbasi on 12-Mar-12 08:25 PM
Apparently you have a database constraint that doesn't permit you to delete a row in the main table before the matching row is deleted in the child table. Either remove the constraint, or reformulate your delete statement.
Rajuje Abbasi replied to Peter Bromberg on 13-Mar-12 02:30 PM
Dear and respectable Friend i have no Database constrain and i am new in this field and my problem is simple i don't know how to fix it my problem is 

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  

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 .But But but When i press the Save Change button then other error is occure which is copy of last record to rest of other record as .

 When i try to Enter the Record with new ID mean suppose i have been enter the StudentID from 1 to 11 then for New ID i must use the ID 12 for new record and when i enter the record with StudentID=12. FirstName =Ramo,LastName=Abbasi and again Enter other record likeStudentID=13 , Firstname =Mukash=LastName=Chandra . In this situation when i press the Save changebutton my last record 13 with FirstName LastName overlap the Firstname lastname of record 12 . means the Grid after pressing the Save Change button have the Record like this

StudentID=12 FirstName=Mukash LastName= Chandra

StudentID=13 FirstName=Mukash, LastName=Chandra

So I don't know how to fix it but yes it is like i challenge for every programmer to fix that i think for Refreshing of Database may be possible but i am new in this field So please tell me and also consider the Datatype of my record in above my be i use the wrong Datatype during the C# programing actually i am self learner i am so poor can't afford the tutor So help me please i consider my above Datatype also

I have record in my Database which have following Datatype as

StudentID=INT,

FirstName=VarChar,

LastName=VarChar,

Gender=Varchar,

GPA=Float,

MyImage=Vabinary (MAX)

     Or may be Datarow fail to Delete the record or Update the record i don't know please tell me and thanks to replay me and i am waiting for your replay in future thanks to replay me please replay in future i am waiting for your replay