Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by FtKarras
Posted
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?

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted (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 by FtKarras
Posted

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;
}

Here's what I'm up to.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...