none
Data not being saved to SQL CE with strongly typed dataset. RRS feed

  • Question

  • Greetings,

     

    Problem:

    I am having this problem that I have been pulling my hair out for a week.  I am going against a SQL CE database and I have this method that passes in a ClipRow in which is from the master table.

    The MstrDb is a singleton object that allows access to the main form Datasets, adapters, etc and has a method that simply calls the TableAdapterManager.UpdateAll(with the dataset).

    I haven't changed the default INSERT command for the SubClip adapter and I have checked to make sure it is indeed there during debugging.

    The application is single threaded and I have disabled all timers before calling this method.

    UpdateAll returns zero currently but I remember it did return one.  In both cases the underlying database wasn't updated.

    The RowState of cr is modified.  I don't know if that makes a difference or not.

    What am I missing?

    Suggestions?

     

    -thanks

    -Darryl

    Environment:  XP SP3 VS 2010

        public void Insert(TalkJunkyDataSet.ClipsRow cr)
            {
                int rc;
                try
                {
                    MstrDb mdb = MstrDb.Create();
                    TalkJunkyDataSet.SubClipsDataTable dt = mdb.SubClipDT;

                    TalkJunkyDataSet.SubClipsRow dr = (TalkJunkyDataSet.SubClipsRow)dt.NewRow();
                    dr.BeginEdit();
                    dr.ClipFk = cr.Hash;
                    dr.StartPos = Convert.ToDecimal(StartPos);
                    dr.EndPos = Convert.ToDecimal(EndPos);
                    dr.Url = Url;
                    dr.Description = Description;
                    dr.RecUpdate = DateTime.Now;
                    dr.EndEdit();
                    dr.SetParentRow(cr);
                    dt.AddSubClipsRow(dr);

                    mdb.TjDataSet.AcceptChanges();
                    //dt.AcceptChanges();
                    rc = mdb.UpdateAll();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Subclip.insert: " + e.Message);
                }

            }

    Monday, September 20, 2010 6:22 PM

Answers

  • Darryl, the AcceptChanges (http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges.aspx) method is probably what's causing the behavior you're seeing. The DataSet (and DataTable) classes keep track of the changes that are made to their rows, so they know if something was added, modified or deleted. The adapters apply changes back by looking at this information and figuring out whether they need to insert, update or delete rows in the database.

    Invoking AcceptChanges is a way of saying 'all the changes you have are already persisted, so you can forget about them and behave as if the rows were fresh'. See AcceptChanges on the DataRow (http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx) for an example of how this actually happens.

    I don't know what UpdateAll() does exactly in your code, but my guess is that if you remove the call to AcceptChanges, then it will be able to determine that a new row has been inserted and persist that change to the database.

    • Proposed as answer by BonnieBMVP Wednesday, September 22, 2010 4:53 AM
    • Marked as answer by liurong luo Monday, September 27, 2010 6:55 AM
    Monday, September 20, 2010 6:41 PM

All replies

  • Darryl, the AcceptChanges (http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges.aspx) method is probably what's causing the behavior you're seeing. The DataSet (and DataTable) classes keep track of the changes that are made to their rows, so they know if something was added, modified or deleted. The adapters apply changes back by looking at this information and figuring out whether they need to insert, update or delete rows in the database.

    Invoking AcceptChanges is a way of saying 'all the changes you have are already persisted, so you can forget about them and behave as if the rows were fresh'. See AcceptChanges on the DataRow (http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx) for an example of how this actually happens.

    I don't know what UpdateAll() does exactly in your code, but my guess is that if you remove the call to AcceptChanges, then it will be able to determine that a new row has been inserted and persist that change to the database.

    • Proposed as answer by BonnieBMVP Wednesday, September 22, 2010 4:53 AM
    • Marked as answer by liurong luo Monday, September 27, 2010 6:55 AM
    Monday, September 20, 2010 6:41 PM
  • Thank you so much!  That was exactly what the problem was.

    You have made me week!

     

    -Darryl

    Friday, September 24, 2010 2:04 PM