• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5163
  • Last Modified:

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.

0
Jayesh Acharya
Asked:
Jayesh Acharya
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Gururaj BadamCommented:
I don't think it's the issue with running in citrix or not

Post some code snippet where the error occurs.

If not have a check to see if the exists before fetching the value like

if(dict.ContainsKeys(_key))
val = dict[_key];
0
 
Naman GoelSoftware engineer 1Commented:
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

0
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
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?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Naman GoelSoftware engineer 1Commented:
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.
0
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
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 ...
0
 
Todd GerbertIT ConsultantCommented:
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

0
 
Gururaj BadamCommented:
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.
0
 
Naman GoelSoftware engineer 1Commented:
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
 
0
 
Naman GoelSoftware engineer 1Commented:
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

0
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now