Solved

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

Posted on 2010-09-16
10
4,448 Views
Last Modified: 2012-05-10
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
Comment
Question by:jhacharya
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 500 total points
Comment Utility
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
 
LVL 13

Expert Comment

by:Naman Goel
Comment Utility
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
 
LVL 2

Author Comment

by:jhacharya
Comment Utility
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
 
LVL 13

Expert Comment

by:Naman Goel
Comment Utility
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
 
LVL 2

Author Comment

by:jhacharya
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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
 
LVL 13

Expert Comment

by:Naman Goel
Comment Utility
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
 
LVL 13

Expert Comment

by:Naman Goel
Comment Utility
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
 
LVL 2

Author Comment

by:jhacharya
Comment Utility
.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Citrix XenDesktop 7.6 Citrix Policies Graphics
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now