Help on DataGrids and DataAdapter with ADO.NET

FtKarras

New member
Joined
Aug 30, 2005
Messages
3
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?



Code:
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
 
Last edited by a moderator:
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?
 
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 cant find the problem....

I am NEW on dot net
 
Last edited by a moderator:
How about using the CommandBuilder to generate the update command:
Code:
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;
}
 
Greattttttttt... I didnt know about any MySqlCommandBuilder... it works good

Realy thank Machaira
 
Back
Top