0

I am getting "System.ArgumentException: An item with the same key has already been added." in ELMAH Log. The application works fine on dev without any exception. I am sure all the fields in the entity have unique names. CODE:

  [HttpPost]
        public ActionResult Create(Candidate candidate)
        {

            if (ModelState.IsValid)
            {


                candidate.CreatedOn=candidate.UpdatedOn = DateTime.Now;

                db.Candidates.Add(candidate);
                db.SaveChanges();



                //save original image & thumbnail
                if (Request.Files.Count > 0)
                {
                    candidate.PhotoPath = PersistImage(string.Concat(candidate.Name.Split(' ')[0], "-", candidate.Id.ToString()));
                    candidate.ResumePath = PersistResume(string.Concat(candidate.Name.Split(' ')[0], "-", candidate.Id.ToString()));
                    db.SaveChanges();
                }
                MembershipUser user = Membership.GetUser(candidate.UserId);

                if (!user.IsApproved)
                {
                    user.IsApproved = true;
                    Membership.UpdateUser(user);
                }
                TempData.Add("Info", UserMessages.Success);
                FormsAuthentication.SetAuthCookie(candidate.UserId, createPersistentCookie: false);
                return RedirectToAction("Listing", "Job");
            }
            TempData.Add("Info", UserMessages.ValidationFailure);
            ViewBag.CategoryId = CacheManager.GetCategorySelectList(Convert.ToString(Convert.ToString(candidate.CategoryId)));
           // ViewBag.SubCategoryId = new SelectList(db.SubCategories, "Id", "Name", candidate.SubCategoryId);
            ViewBag.QualificationId = new SelectList(CacheManager.GetQualifications(), "Id", "Name", candidate.QualificationId);
            ViewBag.CountryId = new SelectList(CacheManager.GetCountries(), "Id", "Name", candidate.CountryId);

            return View(candidate);
        }

Stack Trace:

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Ointerns.Controllers.CandidateController.Create(Candidate candidate)
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)

I have checked the below thread but of no avail

An item with the same key has already been added

Community
  • 1
  • 1
Tepu
  • 1,602
  • 2
  • 20
  • 28
  • The linked question is not related. The exception in your case is thrown by you action method and not by the model binder like in the linked question. Is the code you have shown the complete method `Create`? Because i can't see a dictionary there. – Jan May 15 '13 at 07:35
  • Are you sure this is the relevant code snippet? I can't see any access to a dictionary. On which line is it raising the exception? – Kenneth May 15 '13 at 07:37
  • what scope is `db` it looks like a shared context.. – Aron May 15 '13 at 08:12
  • i have updated code..included complete action method now..thank you for the pointerns... – Tepu May 15 '13 at 09:27
  • Perhaps multiple users trigger the ActionMethod at the same time and the code where you add something to TempData throws the error. – Hanno May 15 '13 at 11:45

0 Answers0