FtKarras Posted August 30, 2005 Posted August 30, 2005 (edited) I can NOT update the table, have a DBConcurrency exception. The code I put is a form with a dataGrid filled with the table data of 'categorias' but when I leave the dataGrid to force a DGValidated I always obtain the same exception.... ... can anyone help me? public class clsFrmClientes : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dG; private MySqlDataAdapter myAdapter; private DataTable myData; public clsFrmClientes() { InitializeComponent(); myAdapter = new MySqlDataAdapter(); myData = new DataTable(); MySqlCommand selCommand = new MySqlCommand(); string selSQL; MySqlCommand updCommand = new MySqlCommand(); string updSQL; selSQL = "SELECT * FROM plq.categorias"; updSQL = "UPDATE plq.categorias SET categoria = @1, descripcion = @2 WHERE cod = @0"; try { // Select Command selCommand.Connection = clsGlobal.dbCon; selCommand.CommandText = selSQL; myAdapter.SelectCommand = selCommand; // Update Command updCommand.Connection = clsGlobal.dbCon; updCommand.CommandText = updSQL; MySqlParameter vParam1 = updCommand.Parameters.Add( "@1", MySqlDbType.VarChar, 1 ); vParam1.SourceColumn = "categoria"; vParam1.SourceVersion = DataRowVersion.Current; MySqlParameter vParam2 = updCommand.Parameters.Add( "@2", MySqlDbType.VarChar, 100 ); vParam2.SourceVersion = DataRowVersion.Current; vParam2.SourceColumn = "descripcion"; MySqlParameter vParam3 = updCommand.Parameters.Add( "@0", MySqlDbType.VarChar, 5 ); vParam3.SourceVersion = DataRowVersion.Original; vParam3.SourceColumn = "cod"; myAdapter.UpdateCommand = updCommand; myAdapter.Fill(myData); dG.DataSource = myData; } catch ( MySqlException ex ) { MessageBox.Show( "Hay un error leyendo de la base de datos: " + ex.Message ); } } [i] --> Here comes some Windows Forms Designer code and then... --> the InitializeComponent() code [/i] void DGValidated(object sender, System.EventArgs e) { try { myAdapter.Update( myData ); } catch (DBConcurrencyException ex) { MessageBox.Show( ex.Message ); } } } ... Thanks in advance Edited August 31, 2005 by FtKarras Quote
Afraits Posted August 31, 2005 Posted August 31, 2005 DBConcurrency exception will usually mean that a row has changed in the underlying DB between you populating the grid and trying to update it. Is that scenario possible? Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
FtKarras Posted August 31, 2005 Author Posted August 31, 2005 (edited) I think it is not possible. I am working on my laptop with local DB. There is no other access than mine. I have read in this forum that DBConcurrency raise when there is no row updated too. So I think my problem is at the bind of DataGrid column and table comumns making the UpdateComand parameters returns no row with the WHERE clause (bad @0 parameter???) but I have read my code one hundred times and can't find the problem.... I am NEW on dot net Edited August 31, 2005 by FtKarras Quote
Machaira Posted September 1, 2005 Posted September 1, 2005 How about using the CommandBuilder to generate the update command: myAdapter = new MySqlDataAdapter(); myData = new DataTable(); MySqlCommandBuilder cb; selSQL = "SELECT * FROM plq.categorias"; try { myAdapter.SelectCommand = new MySqlCommand(selSQL, clsGlobal.dbCon); cb = new MySqlCommandBuilder(myAdapter); myAdapter.UpdateCommand = cb.GetUpdateCommand(); myAdapter.Fill(myData); dG.DataSource = myData; } Quote Here's what I'm up to.
FtKarras Posted September 2, 2005 Author Posted September 2, 2005 Greattttttttt... I didn't know about any MySqlCommandBuilder... it works good Realy thank Machaira Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.