Update records in a Dataset... How? Please help

G

Guest

Hi everyone,
I know the solution to my problem is probably very simple but I just can't
seem to figure out how to do it. My problem is, I have an XML file as a
datastore, I read it into a dataset for data manipulation and update the
dataset with some edited data from the screen. However, I can't seem to make
the changes "stick" in the dataset.
Following is my code snippet:

DataSet dsCOTs = new DataSet();
dsCOTs.ReadXml(Server.MapPath("../COTS.xml"));
dsCOTs.Tables[0].PrimaryKey = new System.Data.DataColumn[]
{dsCOTs.Tables[0].Columns["cotid"]};
DataRow drCOT = dsCOTs.Tables[0].Rows.Find(this.txtCOTID.Text);
if (drCOT != null)
{
drCOT.BeginEdit();
drCOT["claimed"] = "Y";
drCOT.EndEdit();
}
dsCOTs.AcceptChanges();
System.Data.SqlClient.SqlDataAdapter da = new
System.Data.SqlClient.SqlDataAdapter();
da.Update(dsCOTs, "cot");

and the COTs.xml looks like:
<COTS>
<cot>
<id>1234</id>
<name>abcd</name>
<claimed>N</claimed>
</cot>
<cot>
<id>5678</id>
<name>efgh</name>
<claimed>N</claimed>
</cot>
</COTS>

Can someone show me how to update the "claimed" field to "Y" please. Any
suggestion is greatly appreciated, as always.

Calvin
 
D

Daren Hawes

You should NEVER call AcceptChanges before calling Update because if you do
then the changes you have made will not be committed back to the database.
When you call Update, all rows with a RowState of Deleted, Added or Modified
will be reconciled to the database. When you call AcceptChanges, ALL rows
have their RowState set back to Unchanged so no data will be reconciled with
the database. Every call to Update includes an implicit call to
AcceptChanges, so you shouldn't have to call it yourself. There are very
few circumstances where an explicit call to AcceptChanges would be required.

If you are getting concurrency violations it means that some of the data you
are trying to update or delete does not match the existing data in the
database. Chances are you have screwed up you SQL somewhere along the line,
or maybe added your parameters incorrectly.
 
G

Guest

Thanks so much Daren. I managed to get it to work by calling the
AcceptChanges method of the dataset and discard the Update method of the
dataadaptor altogether.
Thanks again.
Calvin

Daren Hawes said:
You should NEVER call AcceptChanges before calling Update because if you do
then the changes you have made will not be committed back to the database.
When you call Update, all rows with a RowState of Deleted, Added or Modified
will be reconciled to the database. When you call AcceptChanges, ALL rows
have their RowState set back to Unchanged so no data will be reconciled with
the database. Every call to Update includes an implicit call to
AcceptChanges, so you shouldn't have to call it yourself. There are very
few circumstances where an explicit call to AcceptChanges would be required.

If you are getting concurrency violations it means that some of the data you
are trying to update or delete does not match the existing data in the
database. Chances are you have screwed up you SQL somewhere along the line,
or maybe added your parameters incorrectly.


Calvin KD said:
Hi everyone,
I know the solution to my problem is probably very simple but I just can't
seem to figure out how to do it. My problem is, I have an XML file as a
datastore, I read it into a dataset for data manipulation and update the
dataset with some edited data from the screen. However, I can't seem to
make
the changes "stick" in the dataset.
Following is my code snippet:

DataSet dsCOTs = new DataSet();
dsCOTs.ReadXml(Server.MapPath("../COTS.xml"));
dsCOTs.Tables[0].PrimaryKey = new System.Data.DataColumn[]
{dsCOTs.Tables[0].Columns["cotid"]};
DataRow drCOT = dsCOTs.Tables[0].Rows.Find(this.txtCOTID.Text);
if (drCOT != null)
{
drCOT.BeginEdit();
drCOT["claimed"] = "Y";
drCOT.EndEdit();
}
dsCOTs.AcceptChanges();
System.Data.SqlClient.SqlDataAdapter da = new
System.Data.SqlClient.SqlDataAdapter();
da.Update(dsCOTs, "cot");

and the COTs.xml looks like:
<COTS>
<cot>
<id>1234</id>
<name>abcd</name>
<claimed>N</claimed>
</cot>
<cot>
<id>5678</id>
<name>efgh</name>
<claimed>N</claimed>
</cot>
</COTS>

Can someone show me how to update the "claimed" field to "Y" please. Any
suggestion is greatly appreciated, as always.

Calvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top