PDA

View Full Version : Error - "Item has already been added"??? *Key in Dictionary*


Punkcrib
09-06-2007, 10:06 PM
I am getting this error at runtime:

"Item has already been added. Key in dictionary: 'SubcategoryID' Key being added: 'SubcategoryID' "


Here is what I am trying to do:

This is supposed to be an "Admin" section of my current project, where the bossman can login to the site and insert/edit/delete entries in the database tables.

I have a GridView that is dynamic (the datasource changes depending on user selection - ie which table the Admin wants to see/modify). I also have a DetailsView that, when the user selects a row in the GridView, that item (row) is displayed in the DetailsView and can be modified or deleted.

Since I am creating my GridView on the fly, when I first tried to run this I received the above error. Of course! I forgot to specify a key so the DetailsView SELECT statement knows which ID to look for.

So I added this line to my code:

GridView.DataKeyNames = New String() {"xxxxxxx"},

where the xxx's represent 1 of 3 possibilities: CategoryID, SubcategoryID, or ItemID (depending on which table the user is currently looking at). These are also in IF statements. Here is the full code for this section of the app:


Public Sub CreateGrid(ByVal num As Integer)
Me.PlaceHolder1.Controls.Clear()
Me.PlaceHolder2.Controls.Clear()
Dim GridView1 As New System.Web.UI.WebControls.GridView
Dim DetailView1 As New System.Web.UI.WebControls.DetailsView
GridView1.AutoGenerateSelectButton = True

Me.Label1.Visible = False
Me.Label2.Visible = False
Me.DropDownList1.Visible = False
Me.Label3.Visible = False
Me.DropDownList2.Visible = False
If num = 1 Then
GridView1.DataSource = Me.dsDDLcat
GridView1.DataKeyNames = New String() {"CategoryID"}
Me.SqlDataSource1.SelectCommand = "SELECT * FROM tblCategories WHERE CategoryID = " & GridView1.SelectedValue
DetailView1.DataSource = Me.SqlDataSource1
Else
Me.Label1.Visible = True
Me.Label2.Visible = True
Me.DropDownList1.Visible = True
If num = 2 Then
GridView1.DataSource = Me.dsGVsubcat
GridView1.DataKeyNames = New String() {"SubcategoryID"}
Me.SqlDataSource2.SelectCommand = "SELECT * FROM tblSubcategories WHERE SubcategoryID = " & GridView1.SelectedValue
DetailView1.DataSource = Me.SqlDataSource2
Else
Me.Label3.Visible = True
Me.DropDownList2.Visible = True
GridView1.DataSource = Me.dsGVItems
GridView1.DataKeyNames = New String() {"ItemID"}
Me.SqlDataSource3.SelectCommand = "SELECT * FROM tblItemList WHERE ItemID = " & GridView1.SelectedValue
DetailView1.DataSource = Me.SqlDataSource3
End If

End If

GridView1.DataBind()

'add to the page
Me.PlaceHolder1.Controls.Add(GridView1)
Me.PlaceHolder2.Controls.Add(DetailView1)
GridView1.Visible = True
DetailView1.Visible = True

End Sub


So my question is WHY am I getting this error? And how can I fix it? Is there a better way to do what I am trying to do?

Thanks to all who can share!!!

nikkiH
09-06-2007, 11:55 PM
Well, if it were me, I'd have 3 sqldatasource components and simply switch between them as desired, if I understand what you're trying to do there.

Punkcrib
09-07-2007, 03:44 PM
Yes, that is exactly what I am doing. I have dsDDLcat, dsGVsubcat, and dsGVItems, for Category, Subcategory, and Items, respectively. If you look in the above code, I change GridView.DataSource depending on which table the user is viewing (1, 2, or 3 respectively).

But that doesn't effect the fact that the GridView still needs to know which field is the Key for each Datasource. I can set the Key to one (say, "CategoryID"), but then when I switch datasources to dsGVsubcat (Subcat), the "SubcategoryID" is now the Key, but the GridView will still think "CategoryID" is the Key (since that field also resides in the Subcategory table.)

So how can I switch the DataKeyNames each time I switch the datasource, and not get the error I was receiving? I can't find anyway to delete the existing key and replace it with a new one. The "GridView.DataKeyNames.Clear()" function does not help, as I've tried putting it in various places in the code with no luck.

Any Ideas?

Punkcrib
09-07-2007, 06:03 PM
Still haven't figured out the problem, but I've come across another one that suddenly developed. It wasnt happening before, but now I get this error when I try to view the "Items" table:

"Object reference not set to an instance of an object."

It points to the bold line of code below:

Public Sub CreateGrid(ByVal num As Integer)
Me.PlaceHolder1.Controls.Clear()

GridView1.AutoGenerateSelectButton = True
GridView1.AllowPaging = True
GridView1.AllowSorting = True
GridView1.AutoGenerateDeleteButton = True
GridView1.AutoGenerateEditButton = True
GridView1.CellPadding = 3


Me.Label1.Visible = False
Me.Label2.Visible = False
Me.DropDownList1.Visible = False
Me.Label3.Visible = False
Me.DropDownList2.Visible = False

If num = 1 Then
GridView1.DataSource = Me.dsDDLcat
Else
Me.Label1.Visible = True
Me.Label2.Visible = True
Me.DropDownList1.Visible = True
If num = 2 Then
GridView1.DataSource = Me.dsGVsubcat
End If
If num = 3 Then
Me.Label3.Visible = True
Me.DropDownList2.Visible = True
GridView1.DataSource = Me.dsGVItems
End If
End If

GridView1.DataBind()

'add datagrid to the page
Me.PlaceHolder1.Controls.Add(GridView1)
GridView1.Visible = True

End Sub

This was working fine yesterday. I have changed a few things, but nothing that I would think would cause this error. Both other cases (Category and Subcategory -- IF statements 1 & 2 above) work fine. there is nothing different from those and the 3rd IF statement, so why is it giving me this error?