NEWBIE: Concurrency violation after switching databases?

Dear Experts,

I'm a total newbie at VS2005 and C#.  I'm writing a quickie app which will allow an in-house user to switch between working with the "same" table in a test and a production database.  (The table has the same definition, with different data.)

When I open the app, I set the Database to "test".  Then, I let the user change it to "production".  So far, that works.  I see the "production" table come up.  However, when I try to Update the "production" table, I get this error:

  Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

There are definitely no other users of either table.  The Update on the original "test" table works fine.  

This is the code I use to perform the Update:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.myTableAdapter.Update(this.myDataSet.myTable);
        }

This is the code I use to switch databases:

        private void databasesToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            String curDb = e.ClickedItem.Text;
            this.myTableAdapter.Connection.Open();
            this.myTableAdapter.Connection.ChangeDatabase(curDb);
            this.myTableAdapter.Fill(this.fTG_Total_2DataSet.myTable);
            this.myTableAdapter.Connection.Close();
            this.dataGridView1.Update();
        }

I should emphasize that I am a complete idiot at this point, and I'm probably missing something obvious!

Thanks,
BrianMc1958
BrianMc1958Asked:
Who is Participating?
 
topdog770Connect With a Mentor Commented:
and then to top it all off... experts-exchange emails starting going to my junk folder.  sorry for delay.

I've done stuff like:  off top o' head.. syntax may not be 100%

DataTable NewTable = OldTable.Clone();  

for( int iRowCount=0; iRowCount < OldTable.Rows.Count; iRowCount++)
{
  NewTable.Row.ImportRow( OldTable.Rows[iRowCount];
}





0
 
topdog770Commented:
One thing that I've done to cause this it to call the AcceptChanges function prior to doing an update.

Also, now that I think about it.. no morning coffee, yet, is that by changing tables... the adapter doesn't know how to synch things up... I think I've used clone or merge to get around this issue.

Will take a quick look
0
 
BrianMc1958Author Commented:
I tried AcceptChanges (on "myTable") prior to the update, and it removed the error, but nothing was actually updated in the table.

And yes, I'm assuming I'm causing confusion by changing tables (and databases) in mid-stream.  PLEASE have your morning coffee!

--BrianMc1958
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.