"An entry with the same key already exists" appears when compiled under .NET 4
Asked Answered
R

3

12

I am trying to upgrade an ASP.NET application to .NET 4, but one page on my site contains an exception:

Argument Exception: an entry with the same key already exists".

What is different about ASP.NET 4 that might cause this problem?

enter image description here

One Solution

Not sure why but setting clientIDMode="Predictable" rather than Static seems to have avoided this exception message.

Ribbentrop answered 29/12, 2010 at 12:39 Comment(3)
Where does the error occur? Debug your code and show us the relevant source code.Societal
To me it looks like the exception is thrown from Dictionary or similar collection...Beanstalk
if the problem still exists please post code and full stack trace of the error.Whimper
K
7

I had the same issue and fixed it.

I went through my entire ASPX page and found ASP.NET control that had the same ID as another.

I also tested this fix, and found that any control that conflicts with another control on the page will cause this error.

<asp:Label ID="FailureText" runat="server" EnableViewState="False" ClientIDMode="Static" />

<asp:Label ID="FailureText" runat="server" EnableViewState="False" ClientIDMode="Static" />

It happens when you Copy/Paste elements on the same page.

Kaceykachina answered 20/7, 2012 at 19:1 Comment(4)
how is this possible! unless visual studio does not let it to compile and point to duplicates id. this error is freaking me outCuisine
@imanabidi when you copy/paste in VS sometimes it changes the ID, other times, it does not. You need to check to make sure each element on the page has a unique ID.Kaceykachina
but in VS 2012 it just does not let me to do that even with copy/paste.however my problem was setting multiple Items'Selected property equal to true in DropDownList items CollectionCuisine
:thank you very much. I mean I solved my problem with resetting all previously selected items with setting selectedIndex=-1 .Cuisine
G
0

This in your web.config may also cause the error by allowing duplicate IDs

<pages clientIDMode="Static">
Glenda answered 12/5, 2014 at 14:32 Comment(0)
S
0

I have this error too and not resolve with this

<pages clientIDMode="Static">

my datagrid work fine this :

I have gridview :

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" CancelText="لغو" DeleteText="حذف" EditText="ويرايش" UpdateText="بروزرساني">
                            <ControlStyle CssClass="btn btn-xs btn-default" />
                            <ItemStyle Width="143px" />
                        </asp:CommandField>



                        <asp:BoundField DataField="ID" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                        <asp:BoundField DataField="Title" HeaderText="عنوان" SortExpression="Title" />
                        <asp:BoundField DataField="ParentID" HeaderText="پدر" SortExpression="ParentID" />
                        <asp:BoundField DataField="Url" HeaderText="آدرس" SortExpression="Url">
                            <ItemStyle CssClass="ltr"></ItemStyle>
                        </asp:BoundField>

                        <asp:BoundField DataField="Icon" HeaderText="آيکون" SortExpression="Icon" />

                        <asp:BoundField DataField="Order" HeaderText="اولويت" SortExpression="Order" />

                    </Columns>
                </asp:GridView>

but after add image field i see this error

      <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" CancelText="لغو" DeleteText="حذف" EditText="ويرايش" UpdateText="بروزرساني">
                            <ControlStyle CssClass="btn btn-xs btn-default" />
                            <ItemStyle Width="143px" />
                        </asp:CommandField>


                        <asp:ImageField  ReadOnly="true"  SortExpression="Icon" DataImageUrlField="icon">
                        </asp:ImageField>

                        <asp:BoundField DataField="ID" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                        <asp:BoundField DataField="Title" HeaderText="عنوان" SortExpression="Title" />
                        <asp:BoundField DataField="ParentID" HeaderText="پدر" SortExpression="ParentID" />
                        <asp:BoundField DataField="Url" HeaderText="آدرس" SortExpression="Url">
                            <ItemStyle CssClass="ltr"></ItemStyle>
                        </asp:BoundField>

                        <asp:BoundField DataField="Icon" HeaderText="آيکون" SortExpression="Icon" />

                        <asp:BoundField DataField="Order" HeaderText="اولويت" SortExpression="Order" />

                    </Columns>
                </asp:GridView>
Suite answered 6/1, 2015 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.