Link to home
Start Free TrialLog in
Avatar of Jayesh Acharya
Jayesh AcharyaFlag for United States of America

asked on

A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

My application in C# fails when I try to run this in Citrix, but as a thick client i dont get any problems with the application,

But when trying to go through citrix i get a crash and the error i get is :

A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

in Windows whne I check the debug I can also see this exception,

What I need to know is what do I need to do to resolve this issue.

The application uses a lot of dictionaries, and it also changes the behaviour of a grid based on the values in the dictionary

Any help would be greatly apprecieted.

ASKER CERTIFIED SOLUTION
Avatar of Gururaj Badam
Gururaj Badam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Naman Goel
Yes I agree with novice_novice.

The exception clearly shows that it is not able to find some key that application want to access (see the attached code of Indexer property of Dictionary). Please give code snippet for this to verify it.

on the other side you can use

var Value=null;
if(dic.TryGetValue(_key, out Value) == false)
{
     //Key not found
}

public TValue this[TKey key]
{
    get
    {
        int index = this.FindEntry(key);
        if (index >= 0)
        {
            return this.entries[index].value;
        }
        ThrowHelper.ThrowKeyNotFoundException();
        return default(TValue);
    }
    set
    {
        this.Insert(key, value, false);
    }
}

Open in new window

Avatar of Jayesh Acharya

ASKER

ok I am ploughing through the code and checking each procedure

can i get away with just using

try { -- put all the existing code }
catch (exception err)
{ -- print the error}

or do I have to check each dictionary key every time?
you can use that try catch block.

but use dic.TryGetValue(_key, out Value)

where you are accessing value from key that will handle this automatically.
ok need a bit of help  ...

my code was

string AuthorizedFlg;
Dictionary<string, string> lv_Stock_check_cols

AuthorizedFlg = lv_Stock_check_cols["Authorized"];     //old

i changed it to

AuthorizedFlg = lv_Stock_check_cols.TryGetValue("Authorized", out AuthorizedFlg);


i get compile errors saying it can not convert the out to sting ... need some help
i have tried several combinations .... not sure how i get past this one ...
TryGetValue returns a boolean value, not a string, so you can't assign it's return value to AuthorizedFlg - if TryGetValue returns true, then the value of the dictionary item will be stored in AuthorizedFlg.
if (!lv_Stock_check_cols.TryGetValue("Authorized", out AuthorizedFlg))
	Console.WriteLine("An error occurred finding the Authorized flag.");
else
	Console.WriteLine("The authorized flag is: " + AuthorizedFlg);

Open in new window

Firstly, regarding using try/catch - it's not a problem but in general terms having more try/catch hierarchy heads the performance. Not recommended much.

Secondly using TryGetValue - In this approach you're basically getting the value but before using it you're just verifying whether the fetch was successful or not. This is to avoid the usage of try/catch.

I personally recommend second approach and you can use tgerbert code snippet above for the same.
Yes, please go ahead with the code suggested by Novice_Novice as TryGetValue will return bool value to check whether fetch was successfull or not. Its lot more better than using Try Catch block
Please refer to TryGetValue implementation
 
the attached code for TryGetValue
public bool TryGetValue(TKey key, out TValue value)
{
    int index = this.FindEntry(key);
    if (index >= 0)
    {
        value = this.entries[index].value;
        return true;
    }
    value = default(TValue);
    return false;
}

Open in new window