Bonjour,
J'ai une base de donnée MySql qui contient une table: table1, contenant trois champs: idtab1(int autoincement), attiribut1(varchar), attribut2(varchar).
et une forme, contenant, textbox1, pour le idtab, textbox2, pour attribut1, et textbox3 pour attribut3.
Je veux insérer une ligne à travers ces textbox dans ma table, mais, il y a un problème, que je ne sai pas comment résoudre, svp, aidez moi à le résoudre,
l'erreur, est la suivante: la référence d'objet n'est pas définie à une instance d'un objet.
et voici mon code:
Merci infiniment, je commence à en avoir honte , je pose trop de questions sur ce forum. parce que je suis entrain d'apprendre.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 try { cnx.ConnectionString = strConn; cnx.Open(); MessageBox.Show("Connection réussie"); string MySQLCmd1 = "SELECT * FROM table1"; string MySQLCmd2 = "INSERT INTO table1(idtab1,attribut1,attribut2) VALUES('','?attribut1','?attribut2')"; MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd1, cnx); DataRow tmp = this.ds.Tables["table1"].NewRow(); tmp["idtab1"] = int.Parse(textBox1.Text); tmp["attribut1"] = textBox2.Text; tmp["attribut3"] = textBox3.Text; this.ds.Tables["table1"].Rows.Add(tmp); this.ds.Tables["table1"].AcceptChanges(); MyAdapter.InsertCommand = new MySqlCommand(MySQLCmd2, cnx); this.MyAdapter.InsertCommand.Parameters.Add("attribut1", MySqlDbType.VarChar); this.MyAdapter.InsertCommand.Parameters.Add("attribut2", MySqlDbType.VarChar); this.MyAdapter.InsertCommand.Parameters["attribut1"].SourceColumn = "attribut1"; this.MyAdapter.InsertCommand.Parameters["attribut2"].SourceColumn = "attribut2"; this.MyAdapter.Update(this.ds.Tables["table1"]); cnx.Close(); } catch (Exception ex) { MessageBox.Show("Problème de connection " + ex.Message); }
Partager