IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Récupérer des infos sur une table pour l’insérer dans une autre


Sujet :

C#

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 12
    Points : 3
    Points
    3
    Par défaut Récupérer des infos sur une table pour l’insérer dans une autre
    bonsoir messieurs j'ai un problème pour récupérer des infos sur une table pour l’insérer dans une autre.
    j'ai trois tables qui sont en relation
    1. produit (ID_article, design, prix_U, Qte)
    2. commande (num_com, date_com, montant_com)
    3. detail_commande (num_det, num_com, ID_article, qute_det)
    et une autre table qui n'est pas en relation
    4. facture (ref_article, qute_det, design, prixU, PrixT)
    je n'arrive pas à remplir la table detail_commande. Il faut dire que je suis perdu. Pouvez-vous me mettre sur la voie s'il vous plait?
    j'ai peut-être fait de graves erreurs dans mes codes.

    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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
     private void BtnValider_Click(object sender, EventArgs e)
            {
                DialogResult dialogResult = MessageBox.Show("Voulez vous valider la facture?", "VALIDATION DE LA FACTURE", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    try
                    {
     
                        setConnection();
                        sql_con.Open();
     
                        using (OleDbCommand cmd1 = sql_con.CreateCommand())
                        {
     
                            cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
                            cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
                            //sql_cmd = new OleDbCommand(CommandText, sql_con);
                            cmd1.ExecuteNonQuery();
                            //ADODB.Recordset dbrst = new ADODB.Recordset();
                        }
     
                        long Qte_prod, numCmd;
                        string ref_prod;
     
     
                        using (OleDbCommand cmd2 = sql_con.CreateCommand())
                        {
                            //setConnection();
     
                            cmd2.CommandText = "SELECT MAX(num_com) AS dernier_num FROM Commandes";
                            // cmd2.Connection.Open();
                           // sql_con.Open();
                            object result = cmd2.ExecuteScalar();
                            if (result is null || Convert.IsDBNull(result))
                            {
                                numCmd = 0;
                            }
                            else
                            {
                                numCmd = Convert.ToInt64(result);
                                TxtNunCmd.Text = numCmd.ToString();
     
     
                                //LoadDB();
                                //object result1 = cmd3.ExecuteScalar();
                                for (int i = 0; i < dataGridView1.Rows.Count; i++) //Car tu fais une infériorité stricte
                                    {
                                        ref_prod = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
                                        Qte_prod = Convert.ToInt64(dataGridView1.Rows[i].Cells[1].Value);
     
                                        using (OleDbCommand cmd3 = sql_con.CreateCommand())
                                        {
     
                                            cmd3.CommandText = "INSERT INTO * Detail_commandes (com_det, ref_det, qute_det) VALUES (@com_det,@ref_det,@qute_det)";
                                            cmd3.Parameters.AddWithValue("@com_det", TxtNunCmd.Text);
                                            cmd3.Parameters.AddWithValue("@ref_det", ref_prod);
                                            cmd3.Parameters.AddWithValue("@qute_det", Qte_prod);
                                            cmd3.ExecuteNonQuery();
     
                                        }
     
                                        using (OleDbCommand cmd4 = sql_con.CreateCommand())
                                        {
                                            setConnection();
                                            cmd4.CommandText = "UPDATE Catalogue SET Quantite = Quantite - (@Qte_cmd) WHERE code_article = (@code_article)";
                                            cmd4.Parameters.AddWithValue("@Qte_cmd", Qte_prod);
                                            cmd4.Parameters.AddWithValue("@code_article", TxtRefProduit.Text);
                                            cmd4.ExecuteNonQuery();
     
                                        }
                                    }
                                sql_con.Close();
                                SupDetail_Temp();
                                //vider();
                                TxtTotalCmd.Text = "";
     
     
     
                            }
                        }        
     
                    }
     
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        sql_con.Close();
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                    //do something else
                }
     
            }
    Images attachées Images attachées  

  2. #2
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 744
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 744
    Points : 5 434
    Points
    5 434
    Par défaut
    Le message d'erreur est pourtant clair.
    Retire l'étoile dans ta commande INSERT INTO * Detail_commandes

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 12
    Points : 3
    Points
    3
    Par défaut
    Citation Envoyé par popo Voir le message
    Le message d'erreur est pourtant clair.
    Retire l'étoile dans ta commande INSERT INTO * Detail_commandes
    Merci bien monsieur franchement où j'avais la tête? toutes mes excuses... j'enlève et je relance voir la suite

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 12
    Points : 3
    Points
    3
    Par défaut
    Merci monsieur cela fonctionne mais j'ai un autre soucis je n'arrive pas à faire une deuxième fois la commande. J'ai comme l'impression que ma connexion reste ouverte.
    Le but c'est d'ajouter des produits et de valider. La première tentative passe mais pas la deuxième. J'ai une erreur au niveau du bouton ajouter

    Nom : erreur 2 c#.png
Affichages : 116
Taille : 201,8 Ko

    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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
     private void BtnAjouter_Click(object sender, EventArgs e)
            {
                int total;
                decimal total_achat;
                if (TxtDesignation.Text == "" || TxtPrixUnitaire.Text == "" || TxtQteCmd.Text == "" || TxtQteStock.Text == "" || TxtRefProduit.Text == "")
                {
                    MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
     
                    if (Int32.TryParse(TxtQteCmd.Text, out int value) && TxtRefProduit.Text != "")
                    {
     
                        int a = 0;
                        int b = 0;
                        int c;
     
     
                        int.TryParse(TxtQteCmd.Text.Trim(), out a);
                        int.TryParse(TxtQteStock.Text.Trim(), out b);
                        int.TryParse(TxtPrixUnitaire.Text.Trim(), out c);
     
                        if (a <= b)
                        {
                            total = a * c;
                            total_achat = 0;
     
                            try
                            {
     
                                setConnection();
                                sql_con.Open();
                                using (OleDbCommand cmd = sql_con.CreateCommand())
                                {
                                    cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
                                    cmd.Parameters.AddWithValue("@ref_det", TxtRefProduit.Text);
                                    cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
                                    cmd.Parameters.AddWithValue("@Designation", TxtDesignation.Text);
                                    cmd.Parameters.AddWithValue("@Prix_unitaire_HT", TxtPrixUnitaire.Text);
                                    cmd.Parameters.AddWithValue("@Prix_total_HT", total);
                                    //sql_cmd = new OleDbCommand(txtQuery, sql_con);
                                    cmd.ExecuteNonQuery();
                                    DS.Reset();
                                    DB.Fill(DS);
                                    DT = DS.Tables[0];
                                    dataGridView1.DataSource = DT;
                                    sql_con.Close();
     
     
                                }
                            }
                            catch(Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
     
                            //Création de la requête sql pour inserer des valeurs dans DB     
                           // string txtQuery = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det, @qute_det, @Designation, @Prix_unitaire_HT, @Prix_total_HT)";
     
                            //ExecuteQuery(txtQuery);
     
                            try
                            {
     
                                LoadDB();
     
                                //ADDITIONNER UNE COLONNE DU DATAGRIDVIEW
                                for (int i = 0; i < dataGridView1.Rows.Count; i++) //Car tu fais une infériorité stricte
                                {
                                    total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
     
                                }
                                TxtTotalCmd.Text = total_achat.ToString();//convertir total_achat en string^pour pouvoir l'afficher dans le textbox
                                vider();
                                //TxtTotalCmd.Text = "";
                                //SupDetail_Temp();
     
                            }
     
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                                sql_con.Close();
                            }
                            finally
                            {
                                sql_con.Close();
                            }
     
                        }
                        else
                        {
                            MessageBox.Show("Veuillez verifier le stock du produit!!!");
                        }
                    }
     
                }
     
            }

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 12
    Points : 3
    Points
    3
    Par défaut
    Bonjour à tous j'ai pu trouvé d'où venait l'erreur. j'ai appelé le dadaset, le datateble et dataadpter que j'avais deja fait dans le form_load pour pouvoir afficher les donnée de ma DB dans le datagridview
    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
    using (OleDbCommand cmd = sql_con.CreateCommand())
                                {
                                    cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
                                    cmd.Parameters.AddWithValue("@ref_det", TxtRefProduit.Text);
                                    cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
                                    cmd.Parameters.AddWithValue("@Designation", TxtDesignation.Text);
                                    cmd.Parameters.AddWithValue("@Prix_unitaire_HT", TxtPrixUnitaire.Text);
                                    cmd.Parameters.AddWithValue("@Prix_total_HT", total);
     
                                    cmd.ExecuteNonQuery();
     
                                    dataGridView1.DataSource = DT;
                                    sql_con.Close();
     
     
                                }

  6. #6
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 12
    Points : 3
    Points
    3
    Par défaut Problème de mise à jour
    J'ai un soucis pour mettre à jour mon stock après avoir effectué une commande. Je n'ai pas d'erreur quand j'enregistre mais le stock reste inchangé. Je suis sous access
    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
     for (int i = 0; i < dataGridView1.Rows.Count; i++) //Car tu fais une infériorité stricte
                                    {
                                        ref_prod = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
                                        Qte_prod = Convert.ToInt64(dataGridView1.Rows[i].Cells[1].Value);
     
                                        using (OleDbCommand cmd3 = sql_con.CreateCommand())
                                        {
     
                                            cmd3.CommandText = "INSERT INTO Detail_commandes (com_det, ref_det, qute_det) VALUES (@com_det,@ref_det,@qute_det)";
                                            cmd3.Parameters.AddWithValue("@com_det", TxtNunCmd.Text);
                                            cmd3.Parameters.AddWithValue("@ref_det", ref_prod);
                                            cmd3.Parameters.AddWithValue("@qute_det", Qte_prod);
                                            sql_con.Open();
                                            cmd3.ExecuteNonQuery();
                                            sql_con.Close();
     
                                        }
     
                                        using (OleDbCommand cmd4 = sql_con.CreateCommand())
                                        {
                                            //setConnection();
                                            OleDbCommand QteStock = sql_con.CreateCommand();
                                            QteStock.CommandText = "SELECT Quantite FROM Catalogue";
                                            sql_con.Open();
                                            int LastQteStock = Convert.ToInt32(QteStock.ExecuteScalar());
                                            sql_con.Close();
                                            cmd4.CommandText = "UPDATE Catalogue SET Quantite = @Quantite - @QteStc WHERE code_article = (@code_article)";
                                            cmd4.Parameters.AddWithValue("@Quantite", LastQteStock);
                                            cmd4.Parameters.AddWithValue("@QteStc", Qte_prod);
                                            cmd4.Parameters.AddWithValue("@code_article", TxtRefProduit.Text);
                                            sql_con.Open();
                                            cmd4.ExecuteNonQuery();
                                            sql_con.Close();
     
                                        }
                                }

Discussions similaires

  1. [RegEx] Récupérer des infos sur une page externe
    Par kevinf dans le forum Langage
    Réponses: 1
    Dernier message: 29/10/2008, 08h28
  2. récupérer des infos sur une application en cours d'exécution
    Par r0d dans le forum Administration système
    Réponses: 5
    Dernier message: 31/01/2008, 15h59
  3. Récupérer des infos sur une page externe
    Par Horrigan dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 20/07/2006, 14h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo