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
Next: Binding to a set of objects >>
More ASP.NET Articles
More By Jagadish Chaterjee