est-t'il possible de remplacer dataset_commande par la requête update pour tester ?
Code:
1
2 adapter_commande.Update(dataset_commande, "commande");
Version imprimable
est-t'il possible de remplacer dataset_commande par la requête update pour tester ?
Code:
1
2 adapter_commande.Update(dataset_commande, "commande");
Salut
Dans le cas present le
Fait référence a une colone de datatableCode:WHERE clé = @clé
Qui peut bien entendu contenir plusieurs valeurs différentes
Donc cette requete doit etre pré-processée par l'adapteur avant d'étre balancée au serveur !
Et c'est la que je pense qu'il y a un bingz si la table n'a pas de primary key
table commande: (ptit extrait)
ou alors au lieu d'éxécuter la requête on peut pas l'afficher ? pour voir s'il y a des erreurs ?Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 CREATE TABLE `commande` ( `co_ref` varchar(20) NOT NULL, `co_pro` int(11) NOT NULL, `co_for` int(11) NOT NULL, `co_gra` int(11) NOT NULL, `co_fich` varchar(30) NOT NULL, `co_del` int(2) NOT NULL, `co_imp` varchar(15) NOT NULL, `co_qte` int(5) NOT NULL, `co_com` varchar(255) default NULL, `co_fac` varchar(75) default NULL, `co_dat` datetime NOT NULL, `co_sta` int(1) NOT NULL default '0', PRIMARY KEY (`co_ref`), KEY `FK.co_pro.pro_id` (`co_pro`), KEY `FK.co_for.for_id` (`co_for`), KEY `FK.co_gra.gra_id` (`co_gra`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ne l'as-tu pas déjà fait quand tu l'as envoyée à l'analyseur de requetes comme tu l'as dit avant?
bah non car j'ai 3 commandes actuellement
2 en attente et 1 terminé
et les 2 en attentes j'aimerai les passer en terminé avec mon application pour bien savoir si sa fonctionne mais quand je clique dessus tu connais la suite.
8O:Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public MySqlConnection maconnexion; public DataSet dataset_commande = new DataSet(); private MySqlDataAdapter adapter_commande; public MySqlCommand cmd; private void liste_Load(object sender, EventArgs e) { timer_reload.Enabled = true; // Connexion à la base de données string connexion = "Server=IP;Port=3306;Database=db_commande;Uid=PSEUDO;Pwd=MDP;"; maconnexion = new MySqlConnection(connexion); maconnexion.Open(); // Ouverture adapter_commande = new MySqlDataAdapter("SELECT co_ref, co_fich, co_dat, co_qte, co_del, co_com, co_sta, co_fac, co_imp, for_nom, gra_nom, pro_nom FROM commande,forma,grammage,produit WHERE co_for=for_id AND co_gra=gra_id AND co_pro=pro_id ORDER BY co_dat DESC", maconnexion); cmd = maconnexion.CreateCommand(); cmd.CommandText = "UPDATE commande SET co_sta=@co_sta WHERE co_ref=@co_ref"; cmd.Parameters.Add("@co_sta", MySqlDbType.Int32, 0, "co_sta"); cmd.Parameters.Add("@co_ref", MySqlDbType.String, 0, "co_ref"); adapter_commande.UpdateCommand = cmd; adapter_commande.Fill(dataset_commande, "commande"); dataset_commande.Tables["commande"].PrimaryKey = new DataColumn[] { dataset_commande.Tables["commande"].Columns["co_ref"] }; DataView dataView_attente = new DataView(dataset_commande.Tables["commande"], "co_sta = 0", null, DataViewRowState.CurrentRows); dataGridView_attente.DataSource = dataView_attente; DataView dataView_termine = new DataView(dataset_commande.Tables["commande"], "co_sta = 1", null, DataViewRowState.CurrentRows); dataGridView_termine.DataSource = dataView_termine; } private void dataGridView_attente_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex == details.Index) { adapter_commande = new MySqlDataAdapter("SELECT co_ref, co_fich, co_dat, co_qte, co_del, co_com, co_fac, co_imp, for_nom, gra_nom, pro_nom, co_sta FROM commande,forma,grammage,produit WHERE co_for=for_id AND co_gra=gra_id AND co_pro=pro_id ORDER BY co_dat ASC", maconnexion); adapter_commande.Fill(dataset_commande, "commande"); // obtenir la DataRow correspondant à la ligne courante du DataGridView DataRowView drv = dataGridView_attente.CurrentRow.DataBoundItem as DataRowView; adapter_commande.UpdateCommand.Parameters["@co_sta"].Value = 1; adapter_commande.UpdateCommand.Parameters["@co_ref"].Value = drv["co_ref"]; adapter_commande.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("Commande terminée !", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
http://ups.imagup.com/02/1235581237_erreur.jpg
OK, je viens de comprendre... pourquoi tu redéfinis adapter_commande dans l'évènement CellClick ??!! tu l'as déjà défini dans le Load... En faisant ça tu remplaces adapter_commande par un adapter qui n'a pas d'UpdateCommand, d'où l'erreur...
Et pourquoi tu refais un Fill ??!! la DataTable est déjà remplie :roll:
Enlève ces deux lignes dans CellClick :
et remets le adapter_commande.Update comme avant, ça devrait marcherCode:
1
2 adapter_commande = new MySqlDataAdapter("SELECT co_ref, co_fich, co_dat, co_qte, co_del, co_com, co_fac, co_imp, for_nom, gra_nom, pro_nom, co_sta FROM commande,forma,grammage,produit WHERE co_for=for_id AND co_gra=gra_id AND co_pro=pro_id ORDER BY co_dat ASC", maconnexion); adapter_commande.Fill(dataset_commande, "commande");
si je fais comme tu m'as dit je tombe sur sa:
Violation de l'accès concurrentiel : UpdateCommand a affecté 0 des enregistrements 1 attendus.
Bien vu TomLev :king:
Et pourtant c'etait gros comme une maison !
pour noobyyy
Comme on est déja a la 6eme page de ce sujet, ce serait peut etre bien que tu reposte le dernier code employé
Je pense qu'il doit manquer un acceptchanges ou un truc du genre
Verifie si ce probléme ne vient pas dans un deuxieme appel de ton event
N.B. : Il est toujours preferable de mettre les methodes de traitement en dehors des handler d'event et garder ceux ci le plus nu possible !
actuellement:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public MySqlConnection maconnexion; public DataSet dataset_commande = new DataSet(); private MySqlDataAdapter adapter_commande; public MySqlCommand cmd; private void liste_Load(object sender, EventArgs e) { timer_reload.Enabled = true; // Connexion à la base de données string connexion = "Server=IP;Port=3306;Database=db_commande;Uid=PSEUDO;Pwd=MDP;"; maconnexion = new MySqlConnection(connexion); maconnexion.Open(); // Ouverture adapter_commande = new MySqlDataAdapter("SELECT co_ref, co_fich, co_dat, co_qte, co_del, co_com, co_sta, co_fac, co_imp, for_nom, gra_nom, pro_nom FROM commande,forma,grammage,produit WHERE co_for=for_id AND co_gra=gra_id AND co_pro=pro_id ORDER BY co_dat DESC", maconnexion); cmd = maconnexion.CreateCommand(); cmd.CommandText = "UPDATE commande SET co_sta=@co_sta WHERE co_ref=@co_ref"; cmd.Parameters.Add("@co_sta", MySqlDbType.Int32, 0, "co_sta"); cmd.Parameters.Add("@co_ref", MySqlDbType.String, 0, "co_ref"); adapter_commande.UpdateCommand = cmd; adapter_commande.Fill(dataset_commande, "commande"); dataset_commande.Tables["commande"].PrimaryKey = new DataColumn[] { dataset_commande.Tables["commande"].Columns["co_ref"] }; DataView dataView_attente = new DataView(dataset_commande.Tables["commande"], "co_sta = 0", null, DataViewRowState.CurrentRows); dataGridView_attente.DataSource = dataView_attente; DataView dataView_termine = new DataView(dataset_commande.Tables["commande"], "co_sta = 1", null, DataViewRowState.CurrentRows); dataGridView_termine.DataSource = dataView_termine; } private void dataGridView_attente_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex == details.Index) { // obtenir la DataRow correspondant à la ligne courante du DataGridView DataRowView drv = dataGridView_attente.CurrentRow.DataBoundItem as DataRowView; drv.Row["co_sta"] = 1; adapter_commande.Update(dataset_commande, "commande"); MessageBox.Show("Commande terminée !", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
8O
Et m***e, moi qui croyait qu'on allait enfin clore ce sujet :aie:
Bon, on va essayer de voir ce qui se passe...
- Crée une nouvelle méthode dans ta classe :
Code:
1
2
3
4
5
6 private void adapter_commande_RowUpdating(object sender, MySqlRowUpdatingEventArgs e) { MessageBox.Show(e.Command.CommandText); MessageBox.Show(e.Command.Parameters["@co_ref"].Value; MessageBox.Show(e.Command.Parameters["@co_sta"].Value; }
- Après la création du DataAdapter (dans liste_Load), ajoute ça :
Code:adapter_commande.RowUpdating += adapter_commande_RowUpdating;
Et dis nous ce qui s'affiche quand tu fais la mise à jour
non, c'est automatiquement appelé par la méthode Update après une mise à jour réussie (à moins d'avoir mis à false la propriété AcceptChangesDuringUpdate du DataAdapter)
je veux bien tester mais quand je rajoute cette méthode j'obtiens ceci:
Erreur 3 La méthode surchargée correspondant le mieux à 'System.Windows.Forms.MessageBox.Show(string)' possède des arguments non valides
au temps pour moi...
Mais il faudrait que tu apprennes à comprendre et corriger toi-même ce genre d'erreur si tu veux progresser... Ici, le message d'erreur indique simplement que le paramètre n'est pas du type attendu (string) : en effet la propriété Value renvoie un object et non un stringCode:
1
2
3
4
5
6 private void adapter_commande_RowUpdating(object sender, MySqlRowUpdatingEventArgs e) { MessageBox.Show(e.Command.CommandText); MessageBox.Show(e.Command.Parameters["@co_ref"].Value.ToString()); MessageBox.Show(e.Command.Parameters["@co_sta"].Value.ToString()); }
Je clique les 3 messages s'affichent:
http://ups.imagup.com/02/1235590619_erreur.jpg
puis comme j'ai rien touché en bas je retombe sur mon message d'erreur
( Violation de l'accès concurrentiel : UpdateCommand a affecté 0 des enregistrements 1 attendus. )
je précise que la requête marche et que ma commande passe en terminé. Reste à trouver comment l'écrire dans la base de données.
Pourtant je connais ce problème, il est classique quand on appele deux fois le meme update sur le meme sur le meme dataset, mais je ne me souviens plus dans quel projet j'avais contourné ca ni comment mais je pense bien que c'etait un acceptchanges !Citation:
non, c'est automatiquement appelé par la méthode Update après une mise à jour réussie (à moins d'avoir mis à false la propriété AcceptChangesDuringUpdate du DataAdapter)
En revoyant le codde de nooby, je me dit que toute cette mecanique pourrait faire appel a un simple update en executenonquery puisque de toute facon c'est appelé pour un row a la fois dans un cell event !
Et je déplacerait d'ailleur le code de l'eventhandler dans une methode plus generique
Exactement !Citation:
tu veux dire qu'il faudrait que je crée une methode juste avec les traitements dedant et je l'appellerai dans le cellclick c'est sa ?
Si tu prends cette habitude dés le début tu verra ca devient vite plus confortable !
ouai bon c'est pas gagner je cherche...