Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Database - Hash table using ASP.NET 2.0 and VB
Hash table using ASP.NET 2.0 and VB


ASP.NET Database Tutorial

This example illustrates how to create and use a hash table. Each element of hash table is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

This example illustrates how to create and use a hash table. Each element of hash table is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

First, you will need to import the using System.Collections  and System.IO namespace.

Imports System.Collection
Imports System.IO

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.
The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

The following example shows how to determine whether the Hashtable contains a specific element.  We can search a hash table either by the keys, or by the associated values; But search by key will make an excellent search efficiency. We use the btnID_Click and btnName_click event to do the work. We then call  the Hashtable.Contains Method to use the collection’s objects’ Equals and CompareTo methods on item to determine whether item exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection.

Public Sub btnID_Click([source] As [Object], e As EventArgs)
output.Text = "<PRE>" + HashTableSample.FindEntry(empID.Text, Nothing) + "</PRE>"
btnClear.Visible = True
End Sub

Public Sub btnName_Click([source] As [Object], e As EventArgs)
output.Text = "<PRE>" + HashTableSample.FindEntry(0, empName.Text) + "</PRE>"
btnClear.Visible = True
End Sub

Sub ResetList()
HashTableSample.table.Clear()

HashTableSample.table.Add(5123, "Jay")
HashTableSample.table.Add(2569, "Brad")
HashTableSample.table.Add(1254, "Brian")
HashTableSample.table.Add(6839, "Seth")
HashTableSample.table.Add(3948, "Rajesh")
HashTableSample.table.Add(1930, "Lakshan")
HashTableSample.table.Add(9341, "Kristian")
HashTableSample.table.Add(7921, "Stephen")
HashTableSample.table.Add(2839, "Tom")
HashTableSample.table.Add(1829, "Kit")

MakeList()
End Sub

Public Sub MakeList()
lstNames.Items.Clear()
Dim o As [Object]
For Each o In HashTableSample.table.Keys
lstNames.Items.Add((o.ToString() + ": " + HashTableSample.table(o)))
Next o
End Sub

Class HashTableSample

Public Shared table As New Hashtable()

Public Shared Function FindEntry(inKey As [Object], value As [Object]) As String
Dim strWriter As New StringWriter()
Console.SetOut(strWriter)
Console.WriteLine()
If inKey Is Nothing OrElse inKey = "" Then
inKey = 0
End If
Dim key As Int32
Try
key = Int32.Parse(inKey.ToString)
Catch generatedExceptionVariable0 As Exception
key = 0
End Try
If Not (value Is Nothing) Then
If table.ContainsValue(value) Then
Console.WriteLine("Yes, Value '{0}' was found in the list!", value)
Else
Console.WriteLine("Sorry, Employee '{0}' could not be found.", value)
End If
Else
If table.Contains(key) Then
Console.WriteLine("Yes, ID '{0}' was found in the list!", key)
Else
Console.WriteLine("Sorry, Employee ID '{0}' could not be found.", key)
End If
End If

Console.WriteLine()
Console.WriteLine("Now we will print out the entire list of employees.")
Console.WriteLine("ID" + ControlChars.Tab + "Name")
Console.WriteLine("----" + ControlChars.Tab + "-----------")

Dim d As DictionaryEntry
For Each d In table
Console.WriteLine("{0}" + ControlChars.Tab + "{1}", d.Key, d.Value)
Next d
Return strWriter.ToString()
End Function

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The front end  HashtableDemoCsharp.aspx page looks something like this:

<table width=800 align="center">
<tr><td width=350><b>Find an existing Entry</b></td>
<td width=150></td>
<td width=150></td>

<td><b>Hashtable Entries</b></td>
<td style="width: 147px"><b>New Entry</b></td>
</tr>

<tr><td width=400>Enter a value (employee name) to find:</td>
<td align=right><asp:textbox width="60px" id="empName" runat="server"/></td>
<td width=200><asp:button width="80px" Text="Find Value" id="btnName" onclick="btnName_Click" runat="server"/>
</td>

<td rowspan=3><asp:ListBox id="lstNames" width="150px" rows="7" runat="server"/></td>

<td align=right style="width: 147px">Key:<asp:textbox width="100px" id="txtAddKey" runat="server"/></td>
</tr>

<tr><td>Enter a key (employee id) to find:</td>
<td align=right><asp:textbox width="60px" id="empID" runat="server"/></td>
<td><asp:button width="80px" Text="Find Key" id="btnID" onclick="btnID_Click" runat="server"/></td>
<td align=right style="width: 147px">Value:<asp:textbox width="100px" id="txtAddVal" runat="server"/></td>
</tr>

<tr><td></td>
<td></td>
<td></td>
<td align=right style="width: 147px">
<asp:button width="120px" Text="Add Entry" id="btnAdd" onclick="btnAdd_Click" runat="server"/>
</td>
</tr>

<tr><td></td>
<td></td>
<td></td>
<td>
<asp:button width="120px" Text="Reset List" id="btnReset" onclick="btnReset_Click" runat="server"/>
</td>
</tr>
</table>

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The flow for the code behind page is as follows.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections
Imports System.IO

Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
ResetList()
End If
End Sub 'Page_Load

Sub ResetList()
HashTableSample.table.Clear()

HashTableSample.table.Add(5123, "Jay")
HashTableSample.table.Add(2569, "Brad")
HashTableSample.table.Add(1254, "Brian")
HashTableSample.table.Add(6839, "Seth")
HashTableSample.table.Add(3948, "Rajesh")
HashTableSample.table.Add(1930, "Lakshan")
HashTableSample.table.Add(9341, "Kristian")
HashTableSample.table.Add(7921, "Stephen")
HashTableSample.table.Add(2839, "Tom")
HashTableSample.table.Add(1829, "Kit")

MakeList()
End Sub

Public Sub MakeList()
lstNames.Items.Clear()
Dim o As [Object]
For Each o In HashTableSample.table.Keys
lstNames.Items.Add((o.ToString() + ": " + HashTableSample.table(o)))
Next o
End Sub

Class HashTableSample
Public Shared table As New Hashtable()

Public Shared Function FindEntry(inKey As [Object], value As [Object]) As String
Dim strWriter As New StringWriter()
Console.SetOut(strWriter)
Console.WriteLine()

If inKey Is Nothing OrElse inKey = "" Then
inKey = 0
End If
Dim key As Int32
Try
key = Int32.Parse(inKey.ToString)
Catch generatedExceptionVariable0 As Exception
key = 0
End Try

If Not (value Is Nothing) Then
If table.ContainsValue(value) Then
Console.WriteLine("Yes, Value '{0}' was found in the list!", value)
Else
Console.WriteLine("Sorry, Employee '{0}' could not be found.", value)
End If
Else
If table.Contains(key) Then
Console.WriteLine("Yes, ID '{0}' was found in the list!", key)
Else
Console.WriteLine("Sorry, Employee ID '{0}' could not be found.", key)
End If
End If

Console.WriteLine()
Console.WriteLine("Now we will print out the entire list of employees.")
Console.WriteLine("ID" + ControlChars.Tab + "Name")
Console.WriteLine("----" + ControlChars.Tab + "-----------")

Dim d As DictionaryEntry
For Each d In table
Console.WriteLine("{0}" + ControlChars.Tab + "{1}", d.Key, d.Value)
Next d
Return strWriter.ToString()
End Function

Public Shared Function AddEntry(key As String, value As String) As Boolean
Try
Dim i As Int32 = Convert.ToInt32(key)
table.Add(i, value)
Return True
Catch generatedExceptionVariable0 As Exception
Return False
End Try
End Function
End Class

Public Sub btnID_Click([source] As [Object], e As EventArgs)
output.Text = "<PRE>" + HashTableSample.FindEntry(empID.Text, Nothing) + "</PRE>"
btnClear.Visible = True
End Sub

Public Sub btnName_Click([source] As [Object], e As EventArgs)
output.Text = "<PRE>" + HashTableSample.FindEntry(0, empName.Text) + "</PRE>"
btnClear.Visible = True
End Sub

Public Sub btnClear_Click([source] As [Object], e As EventArgs)
output.Text = ""
empName.Text = ""
empID.Text = ""
btnClear.Visible = False
End Sub

Public Sub btnReset_Click([source] As [Object], e As EventArgs)
ResetList()
End Sub

Public Sub btnAdd_Click([source] As [Object], e As EventArgs)
If txtAddKey.Text = "" OrElse txtAddVal.Text = "" Then
output.Text = "ADD FAILED: Please ensure you enter both a key, and value entry"
btnClear.Visible = True
Else
If HashTableSample.AddEntry(txtAddKey.Text, txtAddVal.Text) Then
MakeList()
btnClear_Click(Nothing, Nothing)
Else
output.Text = "ADD FAILED: Please ensure the following:<br>" + "1) The key is a valid integer<br>" + "2) The key does not already exist in the list"
btnClear.Visible = True
End If
End If
End Sub
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks