Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Web Form Listbox population
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00761158
Message ID:
00761360
Vues:
26
You can use a DataReader to fetch the records and populate the listbox. You want to only fetch the records the first time the page is rendered since the data won't change. Here is some sample code:
//Only run the code if this is the first time the page is rendered
if (Page.IsPostBack == false)
{
   SqlConnection  cnn = new SqlConnection(connStr);
   string  strSQL = "SELECT * FROM Authors";
   SqlCommand  cmd = new SqlCommand(strSQL, cnn);
   SqlDataReader dr;

   cnn.Open();
   dr = cmd.ExecuteReader(	CommandBehavior.CloseConnection);

   while ( dr.Read() )
   {
	lstDemo.Items.Add(string .Format("{0}: {1}, {2}",dr("Au_Id"), dr("Au_lname"), dr("Au_fname")));
   }

   dr.Close();
}
>I have a a page where the listbox is a Select distinct row from a table used in a SQLServer Data connection.
>
>How do I get the thing populated on load of the form? Should I create a special reader?
>
>TIA
>
>__Stephen
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform