Tuesday, 19 July 2011

Store data with Hashtable using ASP.NET 2.0 and C#


This tutorial will show you how to store data with Hashtable using ASP.NET 2.0 and C#.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. 

using System.Collections

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

In order to run the example, we will use the btn_get_Click to trigger the task. The code as following:

protected void btn_get_Click(object sender, EventArgs e)
{
int key;
string name = string.Empty;
string str = string.Empty;

Hashtable 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);

foreach (DictionaryEntry de in hashtable)
{
str = str + "key=" + de.Key + " " + " value=" + de.Value.ToString() + "<br>";
}
this.Label1.Text = str;
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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

<div align="left" style="text-align: center">
&nbsp;<table>
<tr>
<td colspan="2" style="width: 402px">
<asp:Label ID="Label1" runat="server"></asp:Label>
&nbsp;</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>
&nbsp;&nbsp;
</div>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The flow for the code behind page is as follows

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com
// Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials
}
protected void btn_get_Click(object sender, EventArgs e)
{
int key;
string name = string.Empty;
string str = string.Empty;

Hashtable 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);

foreach (DictionaryEntry de in hashtable)
{
str = str + "key=" + de.Key + " " + " value=" + de.Value.ToString() + "<br>";
}
this.Label1.Text = str;
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment