Click here to Skip to main content
15,664,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am using Visual Studio 2010 and sql server 2005. when i am trying to debug the solution file in one datbase say x it was working fine but when i run the same exe with y datbase settings then it is failing with "The given key was not present in the dictionary" exception at the below function

As soon as the cursor is pointed to the string POS line it goes to Catch exception and throw the above error.

Could anyone please let me know what should be done inorder to overcome this issue
   public void SaveClaimInformation(NSFBatch bat, DateTime? EntryDate, string MPIN, string ProvSpec, string PatID, string InsuredID, DateTime? DOB)
           LogMessage log = new LogMessage();
               using (ClaimsEntities context = new ClaimsEntities())
                   // Save to QeClaimHeader
                   QeClaimHeader qh = new QeClaimHeader();
                   string POS = CommonMethods.GetPlaceOfService(bat.Claims.ElementAt(0).Value.ServiceLineRoot[1].PlaceOfServices.Trim().ToString());
                   qh.ClaimNum = bat.Claims.ElementAt(0).Value.AcnClaimId;
                   qh.AcnPatID = bat.Claims.ElementAt(0).Value.AcnPatID.ToString();
                   qh.AcnProvID = bat.AcnProvID.ToString();
                   qh.TinNum = bat.Header1.ProviderTaxId.Trim().ToString();
                   qh.DateReceived = EntryDate;


public static string GetPlaceOfService(string POS)
           int ret;
           bool result = Int32.TryParse(POS, out ret);
           if (result == true)
               return ret.ToString();
               return "11";
Suvabrata Roy 26-Jul-12 5:59am    
Please produce the whole class file
sjelen 26-Jul-12 6:46am    
From your code we can't see what is the dictionary that is causing the problem.
Use F11 (step into) in debuger on 'string POS...' line and repeat that until you find the cause.
Also check the exception stack trace - it should point you to the real cause.
Probably you're missing some data in database y.

1 solution


Actually this exception occur when you are accessing Dictionary for Incorrect value.

Here i have created one code snippet for reproducing the same error.

Dictionary<string, int> mydic = new Dictionary<string, int>();
mydic.Add("First", 1);
mydic.Add("Second", 2);
mydic.Add("Third", 3);
mydic.Add("Fourth", 4);

var result = mydic["Fifth"].ToString();

Check if this is the case in your code. Here Fifth itme is not as part of Dictionary. That cause "The given key was not present in the dictionary"
Share this answer
anupama962010 26-Jul-12 9:02am    
do u mean the parameter i am passing to GetPlaceofService (bat.Claims.ElementAt(0).Value.ServiceLineRoot[1].PlaceOfServices.Trim().ToString()) is null or empty
anupama962010 26-Jul-12 10:29am    
Thank you very much.The above solution gave me the idea. The parameter it is trying to access was not present hence it is failing
AmitGajjar 26-Jul-12 10:32am    
your welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900