ASP.NET
  Home arrow ASP.NET arrow Page 3 - ASP.NET Dropdown List Control: Eight Ways ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Dropdown List Control: Eight Ways to Bind Data
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 80
    2006-01-04

    Table of Contents:
  • ASP.NET Dropdown List Control: Eight Ways to Bind Data
  • Binding to a Simple Array
  • Binding to a Hashtable
  • Binding to a set of objects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Dropdown List Control: Eight Ways to Bind Data - Binding to a Hashtable


    (Page 3 of 4 )

    Coming to the concept of “Hashtable”, it is very similar to an “ArrayList.”  But, it stores a “pair” of information (something like a key and a value).  This gives us the opportunity to have two sets of data!

    Let us go through the following code.

    Private Sub btnHastable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHastable.Click

            Me.DropDownList1.Items.Clear()
     
            Dim ht As New Hashtable
            ht.Add("1001", "jag1")
            ht.Add("1002", "jag2")
     
            Me.DropDownList1.DataSource = ht
            Me.DropDownList1.DataTextField = "value"
            Me.DropDownList1.DataValueField = "key"
            Me.DropDownList1.DataBind()
        End Sub

    Let me explain part by part.  Let us first consider the following statements:

            Dim ht As New Hashtable
            ht.Add("1001", "jag1")

    I am creating a new “hashtable” and adding “items” to it.  Each “item” needs to have a “key” and a “value.”  From the above statement, I am adding a single student (item) with his regdno (key) and name (value).  So, you now understand the concept of two sets of data. 

    Further proceeding, we have:

    Me.DropDownList1.DataSource = ht
            Me.DropDownList1.DataTextField = "value"
            Me.DropDownList1.DataValueField = "key"
            Me.DropDownList1.DataBind()
     
    You can understand that the “text” (visible to the user) would be “value” (nothing but “name”) and “value” (hidden from the user) would be “key” (nothing but “regdno”).  That’s it.

    Binding to data table, data set and data view

    At the moment, for this article, I am trying to fetch the information from the database and then fill either the data table or data set.  Actually, you can even create your own “offline” table structure and add rows to a “data table” (or even a “data set”).  I leave that concept to the programmers to investigate those techniques, because it is beyond the scope of this article.

    Now, let us connect to the database, fetch the information into the data table (or data set) and then bind it to the “dropdownlist.”

    Private Sub btnDataTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataTable.Click
            Me.DropDownList1.Items.Clear()
            Dim da As New SqlDataAdapter("select productID,ProductName from Products", "data source=.;initial catalog=northwind;user id=sa")
            Dim dt As New DataTable
            da.Fill(dt)
            da.Dispose()
            Me.DropDownList1.DataSource = dt
            Me.DropDownList1.DataTextField = "ProductName"
            Me.DropDownList1.DataValueField = "ProductID"
            Me.DropDownList1.DataBind()
        End Sub
     
        Private Sub btnDataSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataSet.Click
            Me.DropDownList1.Items.Clear()
            Dim da As New SqlDataAdapter("select productID,ProductName from Products", "data source=.;initial catalog=northwind;user id=sa")
            Dim ds As New DataSet
            da.Fill(ds, "Products")
            da.Dispose()
            Me.DropDownList1.DataSource = ds.Tables("Products")
            Me.DropDownList1.DataTextField = "ProductName"
            Me.DropDownList1.DataValueField = "ProductID"
            Me.DropDownList1.DataBind()
        End Sub

    I think the above two methods should be quite familiar to every programmer.  Working with “data view” would also be very similar to that of “data table” or “data set,” except that we can “sort” or “filter” the data.  Let us look into that.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Me.DropDownList1.Items.Clear()
            Dim da As New SqlDataAdapter("select productID,ProductName from Products", "data source=.;initial catalog=northwind;user id=sa")
            Dim ds As New DataSet
            da.Fill(ds, "Products")
            da.Dispose()
            Dim dv As DataView = ds.Tables("Products").DefaultView
            dv.Sort = "ProductName desc"
            Me.DropDownList1.DataSource = dv
            Me.DropDownList1.DataTextField = "ProductName"
            Me.DropDownList1.DataValueField = "ProductID"
            Me.DropDownList1.DataBind()
        End Sub

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · We hope you found this article educational and useful. Please feel free to let the...
       · Hello guys! I am ready for any discussion on this topic. Any doubts, do post them. ...
       · hai, i am new to ajax so please help mei would like to know what is the...
       · hi,i am new here.its really great.this article helped me to explor more.plz tell...
       · thanks for taking time to write this up , i found it really useful and...
       · please give this article in c#.
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT