Questions tagged [keynotfoundexception]

For issues relating to the .NET KeyNotFoundException.

The KeyNotFoundException is an Exception of the .NET that is thrown when the key specified for accessing an element in a collection does not match any key in the collection.

KeyNotFoundException uses the HRESULT COR_E_KEYNOTFOUND, which has the value 0x80131577.

44 questions
82
votes
6 answers

Best way to handle a KeyNotFoundException

I am using a dictionary to perform lookups for a program I am working on. I run a bunch of keys through the dictionary, and I expect some keys to not have a value. I catch the KeyNotFoundException right where it occurs, and absorb it. All other…
Dan McClain
  • 11,780
  • 9
  • 47
  • 67
38
votes
8 answers

When KeyNotFoundException is thrown, how do I see which key wasn't found?

A System.Collections.Generic.Dictionary is throwing KeyNotFoundException, but I can't see which key is supposedly missing. How do I determine this?
aknuds1
  • 65,625
  • 67
  • 195
  • 317
17
votes
3 answers

Which mechanism is a better way to extend Dictionary to deal with missing keys and why?

There is a minor annoyance I find myself with a lot - I have a Dictionary that contains values that may or may not be there. So normal behaviour would be to use the indexer, like this: object result = myDictionary["key"]; However,…
Keith
  • 150,284
  • 78
  • 298
  • 434
8
votes
4 answers

KeyNotFoundException with using HtmlEntity.DeEntitize() method

I am currently working on a scraper written in C# 4.0. I use variety of tools, including the built-in WebClient and RegEx features of .NET. For a part of my scraper I am parsing a HTML document using HtmlAgilityPack. I got everything to work as I…
Sebastian Brandes
  • 726
  • 1
  • 6
  • 15
5
votes
4 answers

Having trouble getting NUnit's Assert.Throws to work properly

I could have sworn that I've used NUnit's Assert.Throws to determine whether or not a particular exception gets thrown from a method, but my memory has failed me before. I read this post here on SO, but it didn't answer my question, as I know the…
Dave
  • 14,618
  • 13
  • 91
  • 145
3
votes
7 answers

C# dictionary<> missing key

When i do val = dict["nonexistent key"] i get System.Collections.Generic.KeyNotFoundException Is there a way i have my dictionary call a member function with the key as a param to generate a value? -edit- Maybe i should of been more specific. I want…
user34537
3
votes
1 answer

Dictionary.GetKey Returns False Even Though Key Is Present - GetHashCode/Equals Already Overridden

This is in C#. I have a problem whereby Dictionary.ContainsKey returns false even though I know the key to be in there. I don't have any code to show unfortunately. The code is not easy to pull together; it is spread across multiple classes and…
ck.
  • 1,056
  • 1
  • 14
  • 25
3
votes
1 answer

Xamarin Studio creates files with wrong namespace

i'm currently using Xamarin Studio and i'm developing an app with the MvvmCross framework. My issue is that, when I try to create a new file in my folder named "Views", the namespace does not contain the ".Views". I have Xamarin.Client.iOS instead…
Alexandre D.
  • 711
  • 1
  • 9
  • 28
3
votes
3 answers

C# Dictionary throws KeyNotFoundException when key exists

I am storing a 2D array that represents a distance matrix between vectors as a Dictionary. My implementation of DistanceCell has two string fields representing the vectors being compared. class DistanceCell { public string…
2
votes
4 answers

CRM 2011 KeyNotFoundException exception

I am new to CRM development. I have a Custom Entity "customer". This Entity has a Field called "defaultcustomer", which can be TRUE or FALSE. I am working on a Plug-In where i need to set the "defaultcustomer" to FALSE for all the "Customers". I am…
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
2
votes
2 answers

How to add files to archive using SevenZipSharp

First of all what I want to do: I have a list of files I'd like to add to the same archive. The folder structure to this files should be included to the archive. The problem I have is that I can not add files to an existing archive. When I use…
wol
  • 21
  • 1
  • 3
2
votes
1 answer

KeyNotFoundException : The given key was not present in the dictionary

I am receiving KeyNotFoundException whenever I try to access the key in dictionary. I have a PricingRules class which has a dictionary: public class PricingRules { Dictionary pricing_rules; public PricingRules() { …
2
votes
2 answers

Can't connect to MySQL database due to KeyNotFoundException

Not sure why I get an error every single time i try to connect. I tried so many things. When i type the wrong password while connecting manually it says access not granted or whatever so I know it connects but when it connects I get this weird…
user5977154
2
votes
3 answers

'The given key was not present in the dictionary' - but the key exists

I am currently developing a MS Dynamics CRM 2013 - Plugin. When I try to assign a string-value to a key of a field of an entity it gives me the 'keynotfound'-exception. This leaves me clueless, because I can verify the key is existing. The key I…
2
votes
1 answer

KeyNotFoundException at dictionary with string key

I failed to get value with string key ... Dictionary dateDic = new Dictionary(); ... public void GetDataList(string _code, int _startDate, int _limit, out List _list) { _list = (from data in…
user2523112
  • 191
  • 1
  • 16
1
2 3