j'ai reussi enfin a inserer et modifier avec une methode ou je fais tous a la main
car avec une requette union que j'avais qui ne ma pas laisser de utuliser le builder ppour fiare directement : je l'ai mis dans une methode init et je l'appelle aprres chaque maj
mais maintenant un probleme dans la suppression : voila le code de la methode :
private static void MergeIdentityColumns(OleDbConnection connection, int choix)
{
using (connection)
{
// Create a DataAdapter based on a SELECT query.
daCompte = new OleDbDataAdapter(
"SELECT id_compte, titulaire, solde FROM Compte",
connection);
switch (choix)
{
case 0:
{
// Create the INSERT command for the new category.
daCompte.InsertCommand = new OleDbCommand(
"INSERT INTO Compte (titulaire, solde) Values(?,?)", connection);
daCompte.InsertCommand.CommandType = CommandType.Text;
// Add the parameter for the titulaire.
daCompte.InsertCommand.Parameters.Add(
"@titulaire", OleDbType.VarWChar, 15, "titulaire");
daCompte.InsertCommand.Parameters.Add(
"@titulaire", OleDbType.Integer, 15, "solde");
daCompte.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
// Create a DataTable
DataTable comptes = new DataTable();
// Create the id_compte column and set its auto
// incrementing properties to decrement from zero.
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id_compte";
column.AutoIncrement = true;
column.AutoIncrementSeed = 0;
column.AutoIncrementStep = -1;
comptes.Columns.Add(column);
// Create the titulaire column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "titulaire";
comptes.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "solde";
comptes.Columns.Add(column);
// Set the primary key on id_compte.
DataColumn[] pKey = new DataColumn[1];
pKey[0] = comptes.Columns["id_compte"];
comptes.PrimaryKey = pKey;
// Fetch the data and fill the DataTable
daCompte.Fill(comptes);
// Add a new row.
DataRow newRow = comptes.NewRow();
newRow["titulaire"] = "New Category";
newRow["solde"] = "8894";
comptes.Rows.Add(newRow);
// Add another new row.
DataRow newRow2 = comptes.NewRow();
newRow2["titulaire"] = "Another New Category";
newRow2["solde"] = "2324";
comptes.Rows.Add(newRow2);
// Add changed rows to a new DataTable that will be
// used to post the inserts to the database.
DataTable dataChanges = comptes.GetChanges();
// Include an event to fill in the Autonumber value.
daCompte.RowUpdated +=
new OleDbRowUpdatedEventHandler(OnRowUpdated);
// Update the database, inserting the new rows.
daCompte.Update(dataChanges);
// Merge the two DataTables.
comptes.Merge(dataChanges);
// Commit the changes.
comptes.AcceptChanges();
//dsCompte.Tables.Remove(dsCompte.Tables[0]);
//dsCompte.Tables.Add(comptes);
} break;
case 1:
{
// Create the INSERT command for the new category.
daCompte.UpdateCommand = new OleDbCommand(
"UPDATE Compte set titulaire = ? , solde = ? where id_compte = 90 ", connection);
daCompte.UpdateCommand.CommandType = CommandType.Text;
daCompte.UpdateCommand.Parameters.Add(
"@titulaire", OleDbType.VarWChar, 15, "titulaire");
daCompte.UpdateCommand.Parameters.Add(
"@solde", OleDbType.Integer, 15, "solde");
daCompte.UpdateCommand.UpdatedRowSource = UpdateRowSource.Both;
// Create a DataTable
DataTable comptes = new DataTable();
// Create the id_compte column and set its auto
// incrementing properties to decrement from zero.
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id_compte";
column.AutoIncrement = true;
column.AutoIncrementSeed = 0;
column.AutoIncrementStep = -1;
comptes.Columns.Add(column);
// Create the titulaire column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "titulaire";
comptes.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "solde";
comptes.Columns.Add(column);
// Set the primary key on id_compte.
DataColumn[] pKey = new DataColumn[1];
pKey[0] = comptes.Columns["id_compte"];
comptes.PrimaryKey = pKey;
// Fetch the data and fill the DataTable
daCompte.Fill(comptes);
DataRow contactRow = comptes.Rows.Find("91");
//On signale le début de l'édition de la ligne
contactRow.BeginEdit();
contactRow["titulaire"] = "encoreencore";
contactRow["solde"] = "800";
//Fin de l'édition du contactRow
contactRow.EndEdit();
DataTable dataChanges = comptes.GetChanges();
// Include an event to fill in the Autonumber value.
daCompte.RowUpdated +=
new OleDbRowUpdatedEventHandler(OnRowUpdated);
// Update the database, inserting the new rows.
daCompte.Update(dataChanges);
// Merge the two DataTables.
comptes.Merge(dataChanges);
// Commit the changes.
comptes.AcceptChanges();
//dsCompte.Tables.Remove(dsCompte.Tables[0]);
//dsCompte.Tables.Add(comptes);
} break;
case 2:
{
// Create the INSERT command for the new category.
daCompte.DeleteCommand = new OleDbCommand(
"DELETE FROM Compte WHERE id_compte = 90", connection);
/* daCompte.DeleteCommand.Parameters.Add("@id_compte",
OleDbType.Char, 5, "id_compte").SourceVersion =
DataRowVersion.Original;
daCompte.DeleteCommand.CommandType = CommandType.Text;
*/
daCompte.DeleteCommand.UpdatedRowSource = UpdateRowSource.Both;
// Create a DataTable
DataTable comptes = new DataTable();
// Create the id_compte column and set its auto
// incrementing properties to decrement from zero.
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id_compte";
column.AutoIncrement = true;
column.AutoIncrementSeed = 0;
column.AutoIncrementStep = -1;
comptes.Columns.Add(column);
// Create the titulaire column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "titulaire";
comptes.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "solde";
comptes.Columns.Add(column);
// Set the primary key on id_compte.
DataColumn[] pKey = new DataColumn[1];
pKey[0] = comptes.Columns["id_compte"];
comptes.PrimaryKey = pKey;
// Fetch the data and fill the DataTable
daCompte.Fill(comptes);
DataRow contactRow = comptes.Rows.Find(90);
comptes.Rows.Remove(contactRow);
DataTable dataChanges = comptes.GetChanges();
// Include an event to fill in the Autonumber value.
daCompte.RowUpdated +=
new OleDbRowUpdatedEventHandler(OnRowUpdated);
// Update the database, inserting the new rows.
daCompte.Update(comptes);
// Merge the two DataTables.
comptes.Merge(dataChanges);
// Commit the changes.
comptes.AcceptChanges();
//dsCompte.Tables.Remove(dsCompte.Tables[0]);
//dsCompte.Tables.Add(comptes);
}break;
}
}
}
et le probleme c'est dans cette ligne : daCompte.Update(dataChanges)
il me dis :
L'argument 'table' ne peut pas être null.
Nom du paramètre : table
sachant que j'ai modifier la table en supprimant une ligne
avez vous une idée ou une rectification du code ou ...
merci de bien expliquer ce que tu as dis : j'ai pas compris
nouredine7584
:/ fais pas attention à ce qu'il dit il aime pas aider. Je sais pas ce qu'il fous sur ce forum.
J'ai un peu de mal à voir où est l'erreur mais il ne te fais pas d'erreur sur : daCompte.Update(comptes);
?