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

ASP.NET Discussion :

Remplir une DropDownList depuis une base de données


Sujet :

ASP.NET

  1. #1
    Membre régulier
    Profil pro
    IT Développeur
    Inscrit en
    Mars 2009
    Messages
    274
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : IT Développeur

    Informations forums :
    Inscription : Mars 2009
    Messages : 274
    Points : 96
    Points
    96
    Par défaut Remplir une DropDownList depuis une base de données
    Bonjour j'essaie de remplir une DropDownList avec le contenu d'une table SQL, mais je n'y arrive pas.



    Voici mon code :

    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
     
     SqlConnection myConnection = new SqlConnection();
     
     
            myConnection.ConnectionString = "Data Source=PC-LAURENCE\\SQLEXPRESS; Initial Catalog=Librarie; Integrated Security=True;";
            myConnection.Open();
     
            // Requete SQL
            SqlCommand myCommande;
            myCommande = new SqlCommand("SELECT Titre, ISBN from Ouvrages", myConnection);
            SqlDataReader lecteur = myCommande.ExecuteReader();
     
     
     
            if (IsPostBack == false)
            {
                Book[] books ;
                // Déclaration d'un tableau books de Type Book
                while (lecteur.Read())
                {
     
                   books = new Book (lecteur["Titre"].ToString(), lecteur["ISBN"].ToString());
                    DdlBooks.DataSource = books; 
                    DdlBooks.DataTextField = "BookTitle";
                    DdlBooks.DataValueField = "Isbn";
                    DdlBooks.DataBind();
     
                }
            }
     
            lecteur.Close();
            myConnection.Close();
    Pourriez-vous m'indiquer comment procéder?



    Merci pour votre aide !

  2. #2
    Membre confirmé
    Avatar de malbaladejo
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    379
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Avril 2002
    Messages : 379
    Points : 527
    Points
    527
    Par défaut
    Ce code ne doit pas compiler.

    La variables books est un tableau de Book et tu instancie un objet Book

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Book[] books ;
     
    // La compilation doit planter ici
    books = new Book (lecteur["Titre"].ToString(), lecteur["ISBN"].ToString());
    Il faut faire :
    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
    SqlConnection myConnection = new SqlConnection();
    myConnection.ConnectionString = "Data Source=PC-LAURENCE\\SQLEXPRESS; Initial Catalog=Librarie; Integrated Security=True;";
    myConnection.Open();
     
    // Requete SQL
    SqlCommand myCommande;
    myCommande = new SqlCommand("SELECT Titre, ISBN from Ouvrages", myConnection);
    SqlDataReader lecteur = myCommande.ExecuteReader(); 
     
    if (IsPostBack == false)
    {
         List<Book> books = new List<Book>();
         // Déclaration d'un tableau books de Type Book
         while (lecteur.Read())
         {
                   books.Add(new Book (lecteur["Titre"].ToString(), lecteur["ISBN"].ToString()));
          }
          DdlBooks.DataSource = books; 
          DdlBooks.DataTextField = "BookTitle";
          DdlBooks.DataValueField = "Isbn";
          DdlBooks.DataBind();
     } 
     
     lecteur.Close();
    myConnection.Close();

  3. #3
    Membre actif Avatar de g_tarik0010
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2006
    Messages
    186
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2006
    Messages : 186
    Points : 284
    Points
    284
    Par défaut
    En fait il est plus judicieux de mettre le code de ton ouverture de connection à l'interieur du block if, sinon tu risque d'ouvrir un connection pour rien lors d'un postback.
    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
     
     
            if (IsPostBack == false)
            {
                 SqlConnection myConnection = new SqlConnection();
     
     
                 myConnection.ConnectionString = "Data Source=PC-LAURENCE\\SQLEXPRESS; Initial Catalog=Librarie; Integrated Security=True;";
                 myConnection.Open();
     
                // Requete SQL
                SqlCommand myCommande;
                myCommande = new SqlCommand("SELECT Titre, ISBN from Ouvrages", myConnection);
                SqlDataReader lecteur = myCommande.ExecuteReader();
     
                Book[] books ;
                // Déclaration d'un tableau books de Type Book
                while (lecteur.Read())
                {
     
                   books = new Book (lecteur["Titre"].ToString(), lecteur["ISBN"].ToString());
                    DdlBooks.DataSource = books; 
                    DdlBooks.DataTextField = "BookTitle";
                    DdlBooks.DataValueField = "Isbn";
                    DdlBooks.DataBind();
     
                }
                lecteur.Close();
                myConnection.Close();
            }

  4. #4
    Membre régulier
    Profil pro
    IT Développeur
    Inscrit en
    Mars 2009
    Messages
    274
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : IT Développeur

    Informations forums :
    Inscription : Mars 2009
    Messages : 274
    Points : 96
    Points
    96
    Par défaut
    Bonjour,

    merci à tous les 2 pour votre aide. Cela fonctionne effectivement mieux !

    Merci pour votre aide.

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

Discussions similaires

  1. Afficher une image depuis la base de données
    Par Gunny dans le forum ASP.NET
    Réponses: 9
    Dernier message: 07/01/2010, 13h21
  2. recharger une DropDownList depuis une autre page
    Par ricil78 dans le forum ASP.NET
    Réponses: 4
    Dernier message: 16/06/2009, 10h59
  3. Réponses: 3
    Dernier message: 10/11/2008, 11h58
  4. remplir une dropdownlist depuis une base sql
    Par Flamby38 dans le forum ASP.NET
    Réponses: 3
    Dernier message: 30/01/2008, 19h30
  5. [RCP] Remplir un TableViewer d'une Vue depuis une Action
    Par DarkHope dans le forum Eclipse Platform
    Réponses: 16
    Dernier message: 26/06/2006, 15h06

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