?
Solved

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

Posted on 2010-09-16
10
Medium Priority
?
4,798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

If your vDisk VHD file gets deleted from the image store accidentally or on purpose, you won't be able to remove the vDisk from the PVS console. There is a known workaround that is solid.
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
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.
Suggested Courses

765 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