65.9K
CodeProject is changing. Read more.
Home

Setting and retrieving a Dataset in a Hashtable

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.15/5 (12 votes)

Aug 4, 2004

1 min read

viewsIcon

73291

downloadIcon

439

Beginner tutorial on settting and retrieving dataset values from a hashtable

Introduction

This is a kindergarten tutorial on how to add a dataset as a hashtable entry item and how to retrieve the values from the hashtable.

Background

I was looking out for a class in .NET that resembled the propertyBag. What a relief when I found this class on msdn.

Hashtable

Hashtable is a class in System.Collections. It is a collection of key-and-value pairs that are organized based on the hash code of the key.

The Code

The file in the source code contains a function GetHashTable() to set a dataset into a hashtable and return it to the calling function. First we create a new hashtable, like so
Dim htbl As System.Collections.Hashtable = New System.Collections.Hashtable()
The dataset is populated using a DataSet.ReadXml.
Dim ds As DataSet = New DataSet()
ds.ReadXml("SomeFile.xml")
Next we add the populated dataset onto the hashtable and return it. The syntax for the add is as follows
Public Overridable Sub Add(ByVal key As Object, ByVal value As Object) _
  Implements IDictionary.Add
The key is the element to add, and the value is the value of the key to add. So we add the dataset into the Hashtable and return the hashtable to the calling function.
htbl.Add("CustomerDetails", ds)
Return htbl
Now getting on to the calling function. We call GetHashTable() which will return a Hashtable. We now try to get the dataset back from the Hashtable. The Hashtable stores the key-value pairs based on the hash code of the key. This way, we can access the key value by doing a lookup of the key.
ds = htbl("CustomerDetails")
Now you got the dataset from the hashtable.

That was a very basic code!

The purpose of the article was to explain the concept behind adding in and retrieving objects from the hashtable. However the real purpose of using the hashtable goes much beyond this. This article is just to get u started up on Hashtables.