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

  • Question

  • i'm sorry. i wouldn't post this if i weren't out of ideas. i've tried everything in the forums, but to no avail.

    so i have 2 tables. both are empty at the beginning. the first table is kind of like a customer masterlist where the user creates a new customer profile. here is the code:

                System.Data.SqlClient.SqlCommandBuilder cb;
                System.Data.SqlClient.SqlCommandBuilder readingcb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(da);
                readingcb = new System.Data.SqlClient.SqlCommandBuilder(readingda);

                DataRow dRow = custMaster.Tables["custMaster"].NewRow();
                DataRow readingdRow = reading.Tables["reading"].NewRow();
                dRow[1] = a_newCust.Text;
                readingdRow[9] = a_newCust.Text;
                dRow[2] = b_newCust.Text;
                readingdRow[10] = b_newCust.Text;
                dRow[3] = c_newCust.Text;
                readingdRow[11] = c_newCust.Text;
                dRow[4] = d_newCust.Text;
                readingdRow[0] = d_newCust.Text;
                dRow[5] = e_newCust.Text.ToString();
                readingdRow[2] = e_newCust.Text.ToString();
                dRow[6] = f_newCust.Text;
                readingdRow[1] = f_newCust.Text;
                dRow[7] = g_newCust.Text.ToString();
                if (radioButton1.Checked == true)
                    dRow[8] = radioButton1.Text;
                    readingdRow[3] = radioButton1.Text;
                else if (radioButton2.Checked == true)
                    dRow[8] = radioButton2.Text;
                    readingdRow[3] = radioButton2.Text;
                dRow[9] = "Active";
                readingdRow[12] = "Active";
                readingdRow[4] = 0;
                readingdRow[5] = 0;


                MaxRows = MaxRows + 1;
                readingMaxRows = readingMaxRows + 1;
                inc = MaxRows - 1;
                readinginc = readingMaxRows - 1;

                this.da.Update(custMaster, "custMaster");
                this.readingda.Update(reading, "reading");

                MessageBox.Show("Customer succesfully added!");

    when this part completes, my program will create a new record in the "custMaster" table, and also a new record in the "reading" table (but this record still has some null fields). when the user goes to the "reading" table, he sees the new record, and then updates that record by adding some column values (thereby filling in the null fields). here is the code:

                    System.Data.SqlClient.SqlCommandBuilder readingup;
                    System.Data.SqlClient.SqlCommandBuilder custpayadd;
                    readingup = new System.Data.SqlClient.SqlCommandBuilder(readingda);
                    custpayadd = new System.Data.SqlClient.SqlCommandBuilder(custpayda);
                    int test = int.Parse(f_reading.Text);
                    int prev = int.Parse(readingNinBox.Text);              

                    DataRow readingdRow = reading.Tables["reading"].Rows[test-1];                  

                    readingdRow[4] = prev;
                    readingdRow[5] = int.Parse(d_reading.Text);
                    readingdRow[13] = e_reading.Value.ToShortDateString();
                    readingdRow[6] = int.Parse(d_reading.Text) - prev;

                    if (g_reading.Text == "Residential")
                        DataRow resSearchdRow = water.Tables["water"].Rows[int.Parse(d_reading.Text) - prev];
                        readingdRow[7] = resSearchdRow[2];

                        DataRow commSearchdRow = commWater.Tables["commWater"].Rows[int.Parse(d_reading.Text) - prev];
                        readingdRow[7] = commSearchdRow[2];
                    this.readingda.Update(reading, "reading"); //>>>>>>>>>>this part is where i get the concurrency error.


     masterDataSet reading;

    System.Data.SqlClient.SqlDataAdapter readingda;

     int readingMaxRows = 0;

    int readinginc = 0;


    reading = new masterDataSet();

    string readingsql = "SELECT * From reading";

    readingda = new System.Data.SqlClient.SqlDataAdapter(readingsql, con);

    readingda.Fill(reading, "reading");

    readingMaxRows = reading.Tables["reading"].Rows.Count;

    readingda.Update(reading, "reading");

    i don't understand why i get this, since i use the same lines of updating codes in my other forms but they work well. any help would be greatly appreciated. thanks.

    • Moved by Bob Wu-MTModerator Tuesday, March 6, 2012 6:26 AM issue (From:Windows Forms Data Controls and Databinding)
    Thursday, March 1, 2012 4:41 PM

All replies

  • some additional information: i only get the error the first time a record is added to the "reading" table. when i close and reopen my program, i can update the record fine.
    Thursday, March 1, 2012 5:25 PM
  • Hi awesomesauce_broncosaurus,

    Welcome to MSDN Forum.

    I suggest you to modify the readingsql as "select col1, col2, col3,... from reading", don't use "select * from reading". Based on the issue, could you please upload the demo to SkyDrive and post the link here? This is, so I can help you more effectively.

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 6:36 AM
  • Hi awesomesauce_broncosaurus,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 9, 2012 1:47 AM