

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
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.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
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; }
|
The front end StoreDataWithHashtableCsharp.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
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!
|
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.DotNetTutorials.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!