Solved

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

Posted on 2010-09-16
10
4,638 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
ID: 33693436
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
ID: 33693735
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
ID: 33694903
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 13

Expert Comment

by:Naman Goel
ID: 33694940
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
ID: 33696691
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33696813
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
ID: 33698456
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
ID: 33698551
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
ID: 33698561
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
ID: 33706575
.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

726 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