Voici le diagramme de ma BD :


Ce que j'aimerais faire, c'est de pouvoir remplir ces 3 tables, au moyen d'un DataGrid qui affiche la jointure suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
"SELECT code,libel,produits.prd_id,barcode,prix_ht,prix_ttc,prix_promo,stock,stock_mini,poids,tva,ventes,actif,delai " +
"from categories,produits,prd_categories " +
"where produits.prd_id= prd_categories.prd_id " +
"and code = code_cat " +
"and code_cat like '" + label3.Text + "__'"
label3.Text contient le code du produit (dans l'exemple : 0530)



Pour ce faire je remplis d'abord mon dataset ds

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
 
CmdSelect = new SqlCommand("SELECT id,code,libel FROM categories WHERE CODE LIKE '" + label3.Text + "__" + "'", MaConnect);
CmdSelect2 = new SqlCommand("SELECT code,libel,produits.prd_id,barcode,prix_ht,prix_ttc,prix_promo,stock,stock_mini,poids,tva,ventes,actif,delai " +
"from categories,produits,prd_categories " +
"where produits.prd_id= prd_categories.prd_id " +
"and code = code_cat " +
"and code_cat like '" + label3.Text + "__'",
MaConnect);
 
CmdSelect.CommandType = System.Data.CommandType.Text;
CmdSelect2.CommandType = System.Data.CommandType.Text;
ds.Clear();
SqlDataAdapter MonDataAdapter = new SqlDataAdapter(CmdSelect);
MonDataAdapter.Fill(ds.Tables["Liste_Categories"]);
MonDataAdapter = new SqlDataAdapter(CmdSelect2);
MonDataAdapter.Fill(ds.Tables["Liste_Produits"]);
 
dataGridView1.DataSource = ds.Tables["Liste_" + comboBoxTables.Text];
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
37
38
39
40
41
42
43
44
45
 
// Bouton Enregistrer modifications
private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Columns.Count > 0)
            {
                countModif = 0;
                labModif.Text = countModif.ToString();
                SqlCommandBuilder SqlCB = null;
                CmdSelect = new SqlCommand("SELECT produits.prd_id,barcode,libel,prix_ht,prix_ttc,prix_promo,stock,stock_mini,poids,tva,ventes,actif,delai " +
                  "from categories,produits,prd_categories " +
                  "where produits.prd_id= prd_categories.prd_id " +
                  "and code_cat like '" + label3.Text + "__'" +
                  "and code = code_cat", MaConnect);
 
// Je modifie les CmdSelect pour pouvoir bénéficier du très pratique SqlCommandBuiler  
 
                CmdSelect = new SqlCommand("SELECT prd_id,barcode,prix_ht,prix_ttc,prix_promo,stock,stock_mini,poids,tva,ventes,actif,delai " +
                     "from produits ", MaConnect);
                CmdSelect2 = new SqlCommand("SELECT code,libel FROM categories WHERE CODE LIKE '" + label3.Text + "__" + "'", MaConnect);
 
 MonDataAdapter = new SqlDataAdapter(CmdSelect2);
                SqlCB = new SqlCommandBuilder(MonDataAdapter);
 
                try
                {
                    MonDataAdapter.Update(ds.Tables["Liste_Produits"]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erreur lors de la mise à jour : " + ex.Message);
                }
 
MonDataAdapter = new SqlDataAdapter(CmdSelect);
                SqlCB = new SqlCommandBuilder(MonDataAdapter);
 
                try
                {
                    MonDataAdapter.Update(ds.Tables["Liste_Produits"]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erreur lors de la mise à jour : " + ex.Message);
                }
...
Voilà, le truc c'est que le premier MonDataAdapter.Update(ds.Tables["Liste_Produits"]);
fait son boulot (insertion d'un nouveau id/code/libel lors de l'encodage dans le DataGrid) mais le 2ème ne fait rien (pas d'erreur signalée)
Si maintenant je mets en commentaires le premier le 2ème fonctionne.

J'aimerais bien que les 2 puissent fonctionner l'un à la suite de l'autre et par la suite remplir encore ma table prd_categories à partir des enregistrements des 2 autres tables.