Store data with Hashtable using ASP.NET 2.0 and VB.NET
This tutorial will show you how to store data with Hashtable using ASP.NET 2.0 and VB.NET.
At first, you need to import the namespace from System.Collections.
The System.Collections namespace contains classe Hashtable which represents a collection of key/value pairs that are organized based on the hash code of the key.
| imports System.Collections |
In order to run the example, we will use the btn_get_Click to trigger the task. The code as following:
|
Protected Sub btn_get_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_get.Click
Dim key As Integer
Dim name As String = String.Empty
Dim str As String = String.Empty
Dim hashtable As Hashtable = New Hashtable()
key = 1
name = "Jake"
Hashtable.Add(key, name)
key = 2
name = "peter"
Hashtable.Add(key, name)
key = 3
name = "lily"
Hashtable.Add(key, name)
For Each de As DictionaryEntry In hashtable
str = str + "key=" + CType(de.Key, String) + " " + " value=" + de.Value.ToString() + "<br>"
Next de
Me.Label1.Text = str
End Sub
|
The front end StoreDataWithHashtableVB.aspx page looks something like this:
|
<div align="left" style='text-align: center'>
<table>
<tr>
<td colspan="2" style='width: 402px'>
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style='height: 26px; width: 402px;'>
<asp:Button ID="btn_get" runat="server" Text="Get data from hashtable" OnClick="btn_get_Click"/></td>
</tr>
</table>
</div>
|
The flow for the code behind page is as follows
|
Imports System.Collections
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btn_get_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_get.Click
Dim key As Integer
Dim name As String = String.Empty
Dim str As String = String.Empty
Dim hashtable As Hashtable = New Hashtable()
key = 1
name = "Jake"
Hashtable.Add(key, name)
key = 2
name = "peter"
Hashtable.Add(key, name)
key = 3
name = "lily"
Hashtable.Add(key, name)
For Each de As DictionaryEntry In hashtable
str = str + "key=" + CType(de.Key, String) + " " + " value=" + de.Value.ToString() + "<br>"
Next de
Me.Label1.Text = str
End Sub
End Class
|