Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Web Form Listbox population
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00761158
Message ID:
00761360
Views:
28
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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform