way of finding out if a row already exists in a dataset

fguihen

Well-known member
Joined
Nov 10, 2003
Messages
248
Location
Eire
this is the way i find out if i have added a row to a dataset or not. there is a boolean value and after the row is added for the first time i set the value to false, so the next time the row is edited rather than added again. i can do this as the dataset is empty when i start, and i am only adding one row. im sure there is a more professional and better way to do this. can anyone tell me how they do it?


Code:
if(workRecordCreated == false)
{
workRow = ds.Tables["work_details"].NewRow();
workRow["patient_id"] = guid;
workRow["sitting"] = this.chkSit.Enabled;
workRow["details"] = this.details.text;
}

else
{

ds.Tables["work_details"].Rows[ds.Tables["work_details"].Rows.Count-1].BeginEdit();
ds.Tables["work_details"].Rows[ds.Tables["work_details"].Rows.Count-1]["sitting"] = this.chkSit.Enabled;
ds.Tables["work_details"].Rows[ds.Tables["work_details"].Rows.Count-1]["details"] = this.details.text;
ds.Tables["work_details"].Rows[ds.Tables["work_details"].Rows.Count-1].EndEdit();
}
 
1. DataTable has got property called PrimaryKey and then use DataTable.Rows.Find()

2. DataTable.Select() has two very usefull overloads and then count on the Rows>0

Hope this helps
 
Back
Top