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 :

Mettre à jour une listbox par rapport à une autre C# [Débutant]


Sujet :

C#

  1. #1
    Futur Membre du Club
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mars 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : BTS IRIS

    Informations forums :
    Inscription : Mars 2013
    Messages : 5
    Points : 6
    Points
    6
    Par défaut Mettre à jour une listbox par rapport à une autre C#
    Bonjour à tous, je suis nouveau sur le forum et dans le cadre de mon stage je dois réaliser une application web en asp au langage C#. J'ai un petit problème avec deux listbox généré via une base de données SQL. J'ai une table Customer (Client en français) avec deux champs : ID_Customer et Name_Customer, j'arrive à les afficher dans mes deux listbox, mais le problème étant que je voudrais faire une "mise à jour" (je sais pas si on le dit comme ça) de mes listbox, je m'explique lorsque je sélectionne un client (exemple : peugeot), cela m'affiche l'id correspondant soit dans mon problème l'ID 6. Or je ne sais pas du tout comment faire, si vous avez une idée, je suis preneur. Merci à vous.

    Mon code Update.aspx.cs :

    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
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    public partial class Update : System.Web.UI.Page
    {
        SqlConnection myConnection;
        SqlCommand myCmdUpdate = new SqlCommand();
     
        protected void Page_Load(object sender, EventArgs e)
        {
            myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MARK IV\Documents\Non-Conformance.mdf;
            Integrated Security=True;Connect Timeout=30;User Instance=True");
            myCmdUpdate.Connection = myConnection;
     
            //Label caché
     
            lblUpdateCustomer.Text = string.Empty;
            lblError.Text = string.Empty;
            lblUpdateProductFamily.Text = string.Empty;
            lblErrorProductFamily.Text = string.Empty;
        }
     
        protected void btnUpdateCustomer_Click(object sender, EventArgs e)
        {
            if (txtCustomer.Text == string.Empty)
            {
                lblError.Text = "Please select a Update Customer !";
                return;
            }
     
            try
            {
     
                //Connexion à la bdd
                myConnection.Open();
     
                //Requête SQL :
                string requete = "UPDATE [Customer] SET Name_Customer = @Customer WHERE ID_Customer = @IDCustomer";
                using (SqlCommand myCmdUpdate = new SqlCommand(requete, myConnection))
                {
                    myCmdUpdate.Parameters.AddWithValue("@IDCustomer", lstIDCustomer.SelectedItem.Value);
                    myCmdUpdate.Parameters.AddWithValue("@Customer", this.txtCustomer.Text);
     
                    myCmdUpdate.ExecuteNonQuery();
     
     
                    lblUpdateCustomer.Text = "Modification réussie";                
                }
     
     
            }
            catch (SqlException se)
            {
                lblUpdateCustomer.Text = (se.ToString());
                lblUpdateCustomer.Text = "Modification échoué";
            }
            finally
            {
                //Ferme la connexion à la bdd
                myConnection.Close();
            }
        }
        protected void btnInsertCustomer_Click(object sender, EventArgs e)
        {
            if (txtCustomer.Text == string.Empty)
            {
                lblError.Text = "Please insert a new Customer !";
                return;
            }
     
            try
            {
     
                //Connexion à la bdd
                myConnection.Open();
     
                //Requête SQL :
                string requete = "INSERT INTO Customer (Name_Customer) VALUES (@NameCustomer)";
                using (SqlCommand myCmdUpdate = new SqlCommand(requete, myConnection))
                {
     
                    myCmdUpdate.Parameters.AddWithValue("@NameCustomer", this.txtCustomer.Text);
     
                    myCmdUpdate.ExecuteNonQuery();
     
     
                    lblUpdateCustomer.Text = "Insertion réussie";
                }
     
     
            }
            catch (SqlException se)
            {
                lblUpdateCustomer.Text = (se.ToString());
                lblUpdateCustomer.Text = "Insertion a échoué";
            }
            finally
            {
                //Ferme la connexion à la bdd
                myConnection.Close();
            }
        }
        protected void btnDeleteCustomer_Click(object sender, EventArgs e)
        {
            try
            {
     
                //Connexion à la bdd
                myConnection.Open();
     
                //Requête SQL :
                string requete = "DELETE FROM Customer WHERE ID_Customer = @IDCustomer";
                using (SqlCommand myCmdUpdate = new SqlCommand(requete, myConnection))
                {
     
                    myCmdUpdate.Parameters.AddWithValue("@IDCustomer", lstIDCustomer.SelectedItem.Value);
     
                    myCmdUpdate.ExecuteNonQuery();
     
     
                    lblUpdateCustomer.Text = "Suppression réussie";                
                }
     
     
            }
            catch (SqlException se)
            {
                lblUpdateCustomer.Text = (se.ToString());
                lblUpdateCustomer.Text = "Suppression a échoué";
            }
            finally
            {
                //Ferme la connexion à la bdd
                myConnection.Close();
            }
        }
        protected void btnUpdateProductFamily_Click(object sender, EventArgs e)
        {
            if (txtUpdateProductFamily.Text == string.Empty)
            {
                lblErrorProductFamily.Text = "Please select a Update Product Family !";
                return;
            }
     
            try
            {
     
                //Connexion à la bdd
                myConnection.Open();
     
                //Requête SQL :
                string requete = "UPDATE [ProductFamily] SET Name_ProductFamily = @productFamily WHERE ID_ProductFamily = @IDProductFamily";
                using (SqlCommand myCmdUpdate = new SqlCommand(requete, myConnection))
                {
                    myCmdUpdate.Parameters.AddWithValue("@IDProductFamily", lstIDProductFamily.SelectedItem.Value);
                    myCmdUpdate.Parameters.AddWithValue("@productFamily", this.txtUpdateProductFamily.Text);
     
                    myCmdUpdate.ExecuteNonQuery();
     
     
                    lblUpdateProductFamily.Text = "Modification réussie";                
                }
     
     
            }
            catch (SqlException se)
            {
                lblUpdateProductFamily.Text = (se.ToString());
                lblUpdateProductFamily.Text = "Modification échoué";
            }
            finally
            {
                //Ferme la connexion à la bdd
                myConnection.Close();
            }
        }
     
        protected void btnInsertProductFamily_Click(object sender, EventArgs e)
        {
            {
                if (txtUpdateProductFamily.Text == string.Empty)
                {
                    lblErrorProductFamily.Text = "Please insert a new Product Family";
                    return;
                }
     
                try
                {
     
                    //Connexion à la bdd
                    myConnection.Open();
     
                    //Requête SQL :
                    string requete = "INSERT INTO ProductFamily (Name_ProductFamily) VALUES (@NameProductFamily)";
                    using (SqlCommand myCmdUpdate = new SqlCommand(requete, myConnection))
                    {
     
                        myCmdUpdate.Parameters.AddWithValue("@NameProductFamily", this.txtUpdateProductFamily.Text);
     
                        myCmdUpdate.ExecuteNonQuery();
     
     
                        lblUpdateProductFamily.Text = "Insertion réussie";
                    }
     
     
                }
                catch (SqlException se)
                {
                    lblErrorProductFamily.Text = (se.ToString());
                    lblErrorProductFamily.Text = "Insertion a échoué";
                }
                finally
                {
                    //Ferme la connexion à la bdd
                    myConnection.Close();
                }
            }
        }
     
        protected void btnDeleteProductFamily_Click(object sender, EventArgs e)
        {
            try
            {
                //Connexion à la bdd
                myConnection.Open();
     
                //Requête SQL :
                string requete = "DELETE FROM ProductFamily WHERE ID_ProductFamily = @IDProductFamily";
                using (SqlCommand myCmdUpdate = new SqlCommand(requete, myConnection))
                {
     
                    myCmdUpdate.Parameters.AddWithValue("@IDProductFamily", lstIDProductFamily.SelectedItem.Value);
     
                    myCmdUpdate.ExecuteNonQuery();
     
     
                    lblUpdateProductFamily.Text = "Suppression réussie";                
                }
     
     
            }
            catch (SqlException se)
            {
                lblUpdateProductFamily.Text = (se.ToString());
                lblUpdateProductFamily.Text = "Suppression a échoué";
            }
            finally
            {
                //Ferme la connexion à la bdd
                myConnection.Close();
            }
        }
     
        protected void lstIDCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
     
        }
    }

  2. #2
    Futur Membre du Club
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mars 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : BTS IRIS

    Informations forums :
    Inscription : Mars 2013
    Messages : 5
    Points : 6
    Points
    6
    Par défaut
    Bonjour et merci à toi de ta réponse rapide, et bien malheureusement non je n'utilise pas de combobox mais une listbox, cela marche aussi avec une listbox ou non? Suis-je vraiment obligé de créer une classe ou non?

    Cordialement.

  3. #3
    Membre habitué Avatar de Razorflak
    Homme Profil pro
    Développeur Flex/AS3
    Inscrit en
    Juin 2013
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Flex/AS3
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 97
    Points : 192
    Points
    192
    Par défaut
    Oui j'ai retirer ma réponse de suite, après avoir vu que tu utilisait des listeBox. Les deux propriété (DataSource et DisplayedMember) existe pour les listBox aussi.
    Ensuite si tu veux utilisé cette méthode il te faut créer une classe. Car avec cette technique, se ne sont pas des string qui sont dans ta liste mais des objets et DisplayesMember indique quelle propriété de cette objet tu veux afficher.

    PS: je remet mon ancienne réponse pour que tout le monde puisse suivre

    Bonjour,
    si j'ai bien compris se que tu veux c'est que lors que tu choisi un client dans une combobox, l'ID de celui ci apparait dans l'autre comboBox?

    Si c'est le cas, tu peux faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    //On part du fait que tu as une classe Customer avec comme propriété Customer_ID et Customer_Name.
     
    List<Customer> listeCustomer; //liste des t'es objet Customer récuperé
     
    comboBoxName.DataSource = listeCustomer;
    comboBoxID.DataSource = listeCustomer;
     
    comboBoxName.DisplayMember = "Customer_Name",
    comboBoxID.DisplayMember = "Customer_ID";
    En espérant t'avoir aidé.
    Cordialement

  4. #4
    Futur Membre du Club
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mars 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : BTS IRIS

    Informations forums :
    Inscription : Mars 2013
    Messages : 5
    Points : 6
    Points
    6
    Par défaut
    Citation Envoyé par Razorflak Voir le message
    Oui j'ai retirer ma réponse de suite, après avoir vu que tu utilisait des listeBox. Les deux propriété (DataSource et DisplayedMember) existe pour les listBox aussi.
    Ensuite si tu veux utilisé cette méthode il te faut créer une classe. Car avec cette technique, se ne sont pas des string qui sont dans ta liste mais des objets et DisplayesMember indique quelle propriété de cette objet tu veux afficher.

    PS: je remet mon ancienne réponse pour que tout le monde puisse suivre
    Désole pour le retard, j'ai pas reçu de notification comme quoi tu avais répondu, en tout cas merci de ta réponse, je le testerais chez moi pour un projet futur personnel, pour ce qui est de mon projet pour le stage, on a opté avec mon tuteur une solution beaucoup plus ... facile à vrai dire.

    En tout cas merci de ta réponse, je vais me replonger avec les classes grâce à toi
    Merci beaucoup Razorflak !!

    Cordialement.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Toutes versions] Comparer 2 fichiers et mettre à jour un fichier par rapport à l'autre
    Par Pleyel dans le forum Macros et VBA Excel
    Réponses: 10
    Dernier message: 15/05/2020, 14h18
  2. Mettre à jour une table par rapport à une autre
    Par remsrock dans le forum Développement
    Réponses: 2
    Dernier message: 18/11/2008, 12h22
  3. Réponses: 16
    Dernier message: 19/02/2008, 14h10
  4. Réponses: 1
    Dernier message: 01/08/2006, 14h43
  5. Réponses: 7
    Dernier message: 12/07/2006, 17h34

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