Filling An ArrayList From A Database Table

Some ASP.Net server controls do not have a sort property, among other things that might be needed. Therefore, it becomes necessary to use an ArrayList, or the like, so that you can work with the ArrayList itself and then bind it to the server control, like a DropDownList, which is used in this sample. This is a very simple example of reading through the records of a table, using SQL Server and the Northwind Database (Categories table), adding the items to the ArrayList, and then, binding the ArrayList to the DropDownList
  1. <%@ Import Namespace="System.Data" %>

  2. <%@ Import Namespace="System.Data.SQLClient" %>

  3. <html>

  4. <head>

  5.         <meta name="GENERATOR" Content="ASP Express 2.1">

  6.         <title>Untitled</title>

  7. <script language="VB" runat="server">

  8. Dim MyArrayList as ArrayList    

  9. Dim sItem as String

  10. Sub Page_Load(Source as Object, E as EventArgs)

  11. MyArrayList=New Arraylist

  12. if not Page.IsPostBack then

  13. Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=NorthWind"

  14.         Dim MySQL as string = "Select CategoryName from Categories"

  15.         Dim MyConn as New SQLConnection(strConn)

  16.         Dim objDR as SQLDataReader

  17.         Dim Cmd as New SQLCommand(MySQL, MyConn)

  18.         MyConn.Open()

  19.         objDR=Cmd.ExecuteReader(

  20.         While objDR.Read()

  21.                 MyArrayList.add(objDR("CategoryName"))

  22.         End While       

  23. end if

  24. ddl1.datasource=MyArrayList

  25. ddl1.databind

  26. End Sub

  27. </script>

  28. </head>

  29. <body>


  31. <Form id="form1" runat="server">

  32. <asp:DropDownList id="ddl1" runat="server" />

  33. </Form>

  34. </body>

  35. </html>

Copy to Clipboard      Download code as Text Format
  Date entered : 25th Aug 2007
  Rating : No Rating
  Accessed  :  2026
  Submitted by :  loopsnlogic
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
                 Click each image to add
this page to each site.
Comments Available

    No comment available at the moment.Be the first one to make a comment

Related ASP.NET Source Codes
  • Search from DropDown List - Display with Datagrid
  • Search from TextBox - Display with Datagrid
  • Show DB Tables in DropDownList - Select from Table
  • Populating a Form's TextBoxes from a DB Query
  • DataReader RowCount Check - Before Display

  • Copyright © 2015 VisualBuilder. All rights reserved