Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
on ajax form in mvc when i click the button then

Error :

1. "An item with the same key has already been added."

2. Console shows "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".

I tried to search 1st error but did not get solution yet.Again i tried to search 2nd error also but still i have no solution.

Thanks.
Posted
Updated 16-Aug-15 20:42pm
v3

We can't help you here: we don't have access to your system, your code, or your data.
So start by looking at the error message:
An item with the same key has already been added.
That's a pretty obvious problem: your code is trying to insert a row with a value that matches an existing value but the column is marked as being unique.

There could be a huge number of reasons for this: from "getting the next value" in advance and assuming you can use that in a multiuser system, to doing an INSERT operation when you should be doing an UPDATE.
We can't tell. So start with the debugger, and look at the code that generates the error. Look at the data it is using, and look in your DB as well. Then try to see what value it complains about, and why you are trying to create a duplicate.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Error code 500 on a webserver indicates a script error and is something of the most general type of description which can be given. Just says some page failed to display because something trying to execute failed.
Your item with the same key has already been added is also an utra general error, but typically it can be trying to set the same key in a database (or forgetting to set a key with no default value, hence for instance if an int, then all rows would be 0, de default) or a dictionary.

Perhaps try to run through the code on the page which fails, this part must be obivous and then if there are reads into dictionaries or database operations with inserts or anyting such, add a check for existence.

Utimately adding an error handler which will give the the callstack whould provide you with a line number of the error, if you deploy a debug version of your .net code.

ifyou can debug the code of cause go to debug => exceptions => check the checbox under managed code exceptions 'Thrown' and your execution should stop on the very line that cause the error - Or at least 2.nd time the insert is attempted or where the update fails.
 
Share this answer
 
May happen with C# code, where adding another key value pair to the list.

If there is a javascript or jQuery code been implemented which adds the objects. Ensure that the object is not getting overwritten. It has to be initialized and addition has to happen in another loop with increment.
 
Share this answer
 
In addition to solutions 1 and 2 that are quite good, you can also adjust the information returned by the aite when an error occurs so that it display a detailled error.

Usually, you want to hide error in a production site so the trick above should only be used temporarily on a site that is not intensively used.

Otherwise, better to run the application local'y and reproduce the problem and use a debugger to find where the exception is throw .

Alternatively, you can write information in a file when the problem occurs and look into that file to find the reason it occurs.
 
Share this answer
 
Because You are Refering Mutiple Table As reference.i has faced same issues.resolved,Mutiple TABLE REFERING
 
Share this answer
 
v2
Comments
OriginalGriff 6-Jun-22 7:21am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 7 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

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



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