detailed view of my concurrency problem. really stuck on this

fguihen

Well-known member
Joined
Nov 10, 2003
Messages
248
Location
Eire
i keep having a concurrency violation when i try to update a row in my database. im adding the data via a form, and when the user clicks a button it is added to the database, but when i go back to the form to edit the data i get the concurrency violation. ive had it for a few days now and cant fix it. anyone have any ideas?

heres where i add a row to the dataset. the recordcreated is a boolean to tell if the record needs to be created, or if it exists and needs to be edited instead

Code:
if(recordCreated == true)
			{
				ds.Tables["Patient"].Rows[ds.Tables["Patient"].Rows.Count-1].BeginEdit();
				ds.Tables["Patient"].Rows[ds.Tables["Patient"].Rows.Count-1]["first_name"] = this.txtFirstName.Text;
				ds.Tables["Patient"].Rows[ds.Tables["Patient"].Rows.Count-1]["sir_name"]  = this.txtSirName.Text;
				ds.Tables["Patient"].Rows[ds.Tables["Patient"].Rows.Count-1]["dob"] =  										dateTimePicker1.Value.Day.ToString() + "/" + dateTimePicker1.Value.Month.ToString() + "/" + 							dateTimePicker1.Value.Year.ToString();


			}
			
			if(recordCreated == false)
			{
				patientRow = this.ds.Tables["Patient"].NewRow();
				patientRow["patient_id"] = guid;
				patientRow["first_name"] = this.txtFirstName.Text;
				patientRow["sir_name"] = this.txtSirName.Text;
				patientRow["dob"] =  dateTimePicker1.Value.Day.ToString() + "/" + dateTimePicker1.Value.Month.ToString() + "/" 				+ dateTimePicker1.Value.Year.ToString();
			
				if(this.radMale.Checked == true)
				{
					patientRow["gender"] = "Male";
				}
				else
				{
					patientRow["gender"] = "Female";
				}
				ds.Tables["Patient"].Rows.Add(patientRow);	
				recordCreated = true;

			
		}

then when the user wants to commit the data to the database they click on a button:
[code]
 this.dsF.UpdateChanges(ds);
	ds.AcceptChanges();

here is the code that commits the record to the database:
Code:
public void UpdateChanges(DataSet dataset)
		{
			ds = dataset;

			DataSet changes = ds.GetChanges(DataRowState.Modified);

			foreach(DataTable tt in ds.Tables)
			{
				foreach (DataRow rr in tt.Rows)
				{
					if(rr.RowState == DataRowState.Deleted)
					{
						rr.Delete();
					}
				}
			}
			try
			{
				this.daPatient.Update(ds,"Patient");
				
				ds.AcceptChanges();
			}
			catch (Exception exce)
			{
				System.Windows.Forms.MessageBox.Show(exce.Message);
			}
		}
 
First of all, why that foreach loop?
If RowState is deleted the rr.Delete() was already done.
Secondly, the DataAdapter calls AcceptChanges so you dont have to do it.

Just to give a more specific error-description set the ContinueOnError in the adapter and check what went wrong in every datarow, this way you get to the real problem.

The concurence-ex comes when the original data in the DataSet is not in the DB anymore.
 
Back
Top