Skip to content

Need help with update command for a MySQL database using C#

Peter

Asked Mar 30, 2011, 11:39 PM EDT5 answers
Share
LinkedInX
Hi,

I'm using Visual Studio to make a program in C# that needs to connect to a remote MySQL Database. I have everything working pretty well except the update command. This is the exact problem I am having: I can add a new row to a DataViewGrid and then add a value to one of the columns and then click my save/update button and have the MySQL server receive and save the added row and value. However if I add a row, then click save, then add the value to the row and then click save again I get the following error message: "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."

Here is the code I am using to connect to the database (my conn string will not be the actual one):

Java
        private String connectionString = null;
        private MySqlConnection sqlConnection = null;
        private MySqlDataAdapter sqlDataAdapter = null;
        private MySqlCommandBuilder sqlCommandBuilder = null;
        private DataTable dataTable = null;
        private String selectQueryString = null;

C#
            connectionString = ***My conn string***
            sqlConnection = new MySqlConnection(connectionString);


            try
            {
                sqlConnection.Open();
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
            }


            selectQueryString = "SELECT * FROM myTable";
            sqlDataAdapter = new MySqlDataAdapter(selectQueryString, sqlConnection);
            sqlCommandBuilder = new MySqlCommandBuilder(sqlDataAdapter);




            dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);
            bindingSource1.DataSource = dataTable;


            databaseEditor_DataGridView.DataSource = bindingSource1;
            databaseEditor_DataGridView.Columns[0].Visible = false;



This is the code I use for the update:

            this.Validate();
            this.bindingSource1.EndEdit();

            try
            {
                sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
            }



Any and all help is appreciated.

Thanks,
Peter

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Okay well I deleted the duplicates of this post... sorry. But I still don't understand what exactly is ADO.net. If you would be able to just glance over my code to see if I did anything wrong that would be great.
    0
  • Suthish Nair
    0
    Your code itself using ADO.Net. did you read the articles i posted.
    0
  • 0
    I did but I'm pretty new to programming so they didn't seem to help a lot. None of the examples were using MySQL anyway. I just don't understand what is wrong with my code.
    0
  • 1 short reply
    • Suthish Nair
      0
      ok, upload your pages
      0