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

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2FsdmluIEtE?=, Nov 8, 2005.

  1. 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
     
    =?Utf-8?B?Q2FsdmluIEtE?=, Nov 8, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?Q2FsdmluIEtE?=

    Daren Hawes Guest

    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" <> wrote in message
    news:...
    > 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
     
    Daren Hawes, Nov 8, 2005
    #2
    1. Advertising

  3. 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" wrote:

    > 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" <> wrote in message
    > news:...
    > > 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

    >
    >
    >
     
    =?Utf-8?B?Q2FsdmluIEtE?=, Nov 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. queen
    Replies:
    3
    Views:
    6,601
    queen
    Jul 15, 2004
  2. Luke Airig
    Replies:
    0
    Views:
    796
    Luke Airig
    Dec 31, 2003
  3. KK
    Replies:
    2
    Views:
    596
    Big Brian
    Oct 14, 2003
  4. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    467
    Ray at
    May 10, 2004
  5. Replies:
    3
    Views:
    667
    Anthony Jones
    Nov 2, 2006
Loading...

Share This Page