Search code examples
c#hashtablenamevaluecollection

Reading key value pair from hash table


I am storing below in my web config.

<add key="* Cancellation" value="Allow"/>

Now i am loading this into hast table

 NameValueCollection tempCollection = (NameValueCollection)ConfigurationManager.GetSection(CONFIG_SECTION);
 Hashtable localCollection = new Hashtable();
 localCollection.Add(Collection.GetKey(index), tempCollection );

now i am trying to read the value from hash table by providing key.

My key can have anything like Pending, canceled, Endorsed or declined before Cancellation so i kept * in key. * means it can have anything.

value = tempCollection["Pending Cancellation"]

but my value is not populating correctly. Value is coming as null


Solution

  • If you want to do that, you have to look for all keys that are like the string that you're looking for. Something similar to this:

        foreach (DictionaryEntry entry in hashtable)
        {
            if(DictionaryEntry.Key.Contains(" Cancellation")){value = DictionaryEntry.Value;}
        }