Concurrency violation: UpdateCommand affected 0 expected 1 records

I am new to C # and trying to bind datagridview to mssql database in visual studio 2010. Data tracking is fine and everything works. Except for a few strange errors:

I get an error message in the topic: updating the same line 2 times, deleting a new inserted row, after updating the row when deleting another row (the word changes to DeleteCommand)

None of the solutions I found on Google work for me. Hope someone can help me. Here is the te code:

    private void fillDatagrid()
        {
            //fill datagrid ADO.NET
            conn = new SqlConnection(TestApp.Properties.Settings.Default.TestdatabaseConnectionString);
            cmd = conn.CreateCommand();
            conn.Open();
            cmd.CommandText = "SelectFrom";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@table", SqlDbType.NVarChar, 50).Value = "Countries";
            cmd.Parameters.Add("@filters", SqlDbType.NVarChar, 300).Value = "";

            adapt = new SqlDataAdapter(cmd);
            dt = new DataTable();
            adapt.Fill(dt);
            dt.TableName = "Countries";

            conn.Close();

            BindingSource src = new BindingSource();
            src.DataSource = dt;
            dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);

            dgDatabaseGrid.DataSource = src;
            dgDatabaseGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            //dgDatabaseGrid.RowValidating += new DataGridViewCellCancelEventHandler(dgDatabaseGrid_RowValidating);

            //disable columns:
            dgDatabaseGrid.Columns[0].Visible = false;
            dgDatabaseGrid.Columns["date_insert"].Visible = false;
            dgDatabaseGrid.Columns["user_insert"].Visible = false;
            dgDatabaseGrid.Columns["date_change"].Visible = false;
            dgDatabaseGrid.Columns["user_change"].Visible = false;
            dgDatabaseGrid.Columns["deleted"].Visible = false;

            //auto size last column
            dgDatabaseGrid.Columns["remarks"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


            SqlCommandBuilder cb = new SqlCommandBuilder(adapt);
        }

        void dt_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            try
            {
                adapt.Update(dt);
            }
            catch (SqlException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

private void dgDatabaseGrid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (!e.Row.IsNewRow)
            {


                DialogResult response = MessageBox.Show("Are you sure?", "Delete row?",
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question,
                                     MessageBoxDefaultButton.Button2);

                if (response == DialogResult.Yes)
                {

                    //ipv delete --> deleted=1
                    conn.Open();
                    cmd = conn.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "DeleteFrom";
                    cmd.Parameters.Add("@table", SqlDbType.NVarChar, 50).Value = "Countries";
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = e.Row.Cells[0].Value;
                    cmd.ExecuteNonQuery();
                    conn.Close();


                    //delete from datagrid:
                    dt.Rows[dgDatabaseGrid.SelectedCells[0].RowIndex].Delete();

                }

                //always cancel!
                e.Cancel = true;

            }
        }
+3
source share
5 answers

after updating the same line 2 times

Timestamp ( , / Db)?

, , Db. SP SelectCmd (), .

,

( DeleteCommand)

.
"", adapt.Update()? , ?

+1

, , , -.

:

try
{
    adapt.Update(dt);

    Me.yourTableAdapter.Update(Me.yourDataSet.yourTable)
    Me.yourDataSet.youTable.AcceptChanges()
    Me.yourTableAdapter.Fill(Me.yourDataSet.yourTable)

, , .

}
catch (SqlException ex)
{
    Debug.WriteLine(ex.Message);
}
+6

! .

...

textboxes, comboboxes .., . .

EndEdit Update dbconcurrency.

, ; , - . , , , - , Update.

, MDF, , , . , . "" .

, - .

+1

.

. , .

Recalc .

SqlCommandBuilder.ConflictOption = ConflictOption.OveriteChanges, .

, , .

0

Source: https://habr.com/ru/post/1757654/


All Articles