Bonjour,

J'ai développé un formulaire dans lequel l'utilisateur sélectionne un produit dans une DropDownList. Une fois le produit sélectionné, je souhaiterai afficher son prix correspondant dans un label mais je n'y arrive pas.

Voilà ce que j'ai fait mais ça ne marche pas
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
 
/* Affichage du prix du produit sélectionné */
    public void prixProduit()
    {
        /* Connexion à la base de données SQL Server */
        string myConnectString = "user id=sa;password=stri;initial catalog=master;data source=PC-DE-LENOU;Connect Timeout=30";
        SqlConnection myConnection = new SqlConnection(myConnectString);
        myConnection.Open();
 
        /* Rechercher si produit en promotion ou non */
        SqlCommand promprod = new System.Data.SqlClient.SqlCommand("select Prom_prod from produit where id_prod='" + DropDownList1.SelectedValue + "'", myConnection);
        System.Data.SqlClient.SqlDataReader readerprom = promprod.ExecuteReader();
        readerprom.Read();
        //DropDownList1.Text = readerprom["Prom_prod"].ToString();
        Boolean b = readerprom.GetBoolean(0);
 
        /* Produit hors promotion */
        if (!b)
        {
            readerprom.Close();
            SqlCommand commande = new System.Data.SqlClient.SqlCommand("select Prixht_prod from produit where id_prod='" + DropDownList1.SelectedValue + "'", myConnection);
            System.Data.SqlClient.SqlDataReader reader = commande.ExecuteReader();
            reader.Read();
            this.Label20.Text = reader.GetValue(0).ToString();
            //Label3.Text = DropDownList1.SelectedItem.Value.ToString();
            reader.GetValue(0).ToString();
        }
        /* Produit en promotion */
        else
        {
            readerprom.Close();
            SqlCommand commande = new System.Data.SqlClient.SqlCommand("select Prixhtprom_prod from produit where id_prod='" + DropDownList1.SelectedValue + "'", myConnection);
            System.Data.SqlClient.SqlDataReader reader = commande.ExecuteReader();
            reader.Read();
            //Label3.Text = DropDownList1.SelectedItem.Value.ToString();
            Label20.Text = reader.GetValue(0).ToString();
        }
    }

Une erreur apparait au niveau : Boolean b = readerprom.GetBoolean(0);
Tentative non valide de lecture lorsque aucune donnée n'est présente.