Hashtable in ASP.net

Posted by .Netlearner under ASP.NET on 12/5/2012 | Points: 10 | Views : 985 | Status : [Member] | Replies : 5
Hello All,

Am having a hashtable in which am adding values dynamically,While Inserting i want to find that whether the pair(key with value) already exists . If the pair is not present already i must be able to insert it .

I Googled in net but i got Conditions that checks only whether the key is present already or whether the value is present already.I didnt find condition to check as a pair.Can anyone please guide me.Thanks in advance.




Responses

Posted by: Somu475 on: 12/5/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

As there is no direct method to check for the existence of Key/Value pair,
You can use some simple logic as below...

Dim myHashTable As New Hashtable


myHashTable.Add("Key1", "Value1")
myHashTable.Add("Key2", "Value2")

Dim key As String = Me.TxtKey.Text
Dim value As String = Me.TxtValue.Text

If myHashTable.ContainsKey(key) And myHashTable.ContainsValue(value) Then
MsgBox("Key/Value pair already exists!")
ElseIf myHashTable.ContainsKey(key) Then
MsgBox("Key already exists!")
ElseIf myHashTable.ContainsValue(value) Then
MsgBox("Value already exists!")
Else
myHashTable.Add(key, value)
End If

Mark as Answer if it helps you...

-Somu

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/5/2012 [Member] Bronze | Points: 25

Up
0
Down
Hashtable H1 = new Hashtable();

H1.Add("Key1", "Value1");
H1.Add("Key2", "Value2");

if (!H1.ContainsKey("Key3") && !H1.ContainsValue("Value3"))
{
H1.Add("Key3", "Value3");
}

Thanks
Pavan Kumar
Mark Answer if this fits the need

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 12/5/2012 [Member] Starter | Points: 25

Up
0
Down
i want to insert as follows

24-->a
24-->b
24-->c
25-->a
25-->d
25-->c

while inserting in hashtable the second row it displays the error that the key is already present.I Dont Know whether am wrong but i thought i can insert same keys with different values, and so thought to check whether particular key with particular value (Ex.24-->a) is present or not.If not i want to insert that in hashtable. whether is is possible ?

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Somu475 on: 12/5/2012 [Member] Starter | Points: 25

Up
0
Down
I think we can't Insert same keys repeatedly in Hashtable.

If you want to Insert multiple Values for the same key, put all those values in a List and then add that List as a Value in Hashtable.

List<string> list = new List<string>();

list.Add("a");
list.Add("b");
list.Add("c");

HashTable obj = new HashTable();
obj.Add("24", list);


-Somu

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
 protected void Page_Load(object sender, EventArgs e)

{
Hashtable hshTable = new Hashtable();

hshTable.Add("FirstName", "S");
hshTable.Add("LastName", "B");

if (!hshTable.ContainsKey("MiddleName") && !hshTable.ContainsValue("P"))
{
hshTable.Add("MiddleName", "P");
}
GetHash(hshTable);


}

private void GetHash(Hashtable H1)
{
IDictionaryEnumerator ide = H1.GetEnumerator();
while (ide.MoveNext())
{
Response.Write(ide.Key + "" + ide.Value + "<br/>" + "the value can be" + H1[ide.Key].ToString() + "<br/>");
}
}


.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response