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!!!
"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!!!