Need help using tableadapter update to save imported xml data back to sql server compact 3.5 database.

I need to be able to add import/export functionality to an application so that several users can keep their data "sync'ed" up.  The users will never be connected to the same network, and an "online" server is not a feasable option.  

I use a typed dataset for the data application and can write it out to xml.  I thought that perhaps I could use the dataset.ReadXml function and then do an update on the table adapters to get this data into another users database.  

New records are inserted just fine (based on primary keys).  However, how do I get the tableadapter to update the database on changed records.  

Here is the import code:

            loomixDataSet ds = new loomixDataSet();
            string _connstr = Properties.Settings.Default.connstr;
            //ds.EnforceConstraints = false;

            ds.ReadXml("c:\\loomix.xml", XmlReadMode.ReadSchema);

            ProductsTableAdapter prods = new ProductsTableAdapter();
            prods.Connection.ConnectionString = _connstr;
            foreach(DataRow dr in ds.Products)
            {
                try
                {
                    prods.Update(dr);
                }
                catch
                {
                 
                }
            }

I am looking for some code to insert into the catch block to force the table adapter to call the update command instead of an insert command.
LVL 1
stwokieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stwokieAuthor Commented:
I have a workaround but would welcome a better solution!
        private void cmdImport_Click(object sender, EventArgs e)
        {
            loomixDataSet ds = new loomixDataSet();
            string _connstr = Properties.Settings.Default.connstr;
            //ds.EnforceConstraints = false;
 
            ds.ReadXml("c:\\loomix.xml", XmlReadMode.ReadSchema);
 
            ProductsTableAdapter prods = new ProductsTableAdapter();
            prods.Connection.ConnectionString = _connstr;
            
            foreach(DataRow dr in ds.Products)
            {
                try
                {
                    prods.Update(dr);
                    //prods.Update(dr["Name"].ToString(), DateTime.Parse(dr["PriceStartDate"].ToString()), DateTime.Parse(dr["PriceEndDate"].ToString()), dr["Price"].ToString(), dr["Unit"].ToString(), dr["ProductType"].ToString(), Int32.Parse(dr["ProductID"].ToString()));
                }
                catch(System.Exception ex)
                {
                   
                    SqlCeConnection cn = prods.Connection;
                    cn.ConnectionString = _connstr;
                    cn.Open();
                    string cmdtxt = "UPDATE [Products] SET  [PriceStartDate] = '" + DateTime.Parse(dr["PriceStartDate"].ToString()) + "' , [PriceEndDate] = '" + DateTime.Parse(dr["PriceEndDate"].ToString()) +  "', [Price] = '" + dr["Price"].ToString() + "', [Unit] = '" + dr["Unit"].ToString() + "', [ProductType] = '" + dr["ProductType"].ToString() +"' WHERE (([Name] = '"+  dr["Name"].ToString() +"'))";
                    SqlCeCommand cm = new SqlCeCommand(cmdtxt, cn);                  
                    
                    try
                    {
                    cm.ExecuteNonQuery();
                    }
                    catch (System.Exception ex2)
                    {
                        MessageBox.Show(ex2.ToString());
                    }
                    cn.Close();
                }
            }
            textBox1.Text =  " Added to Products";
 
 
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.