Home All Groups Group Topic Archive Search About

Item has already been added. Key in dictionary

Author
12 Dec 2006 1:58 PM
avital
Hi,
When inserting a new item i get the following error: Item has already
been added. Key in dictionary: 'Cellular'  Key being added: 'Cellular'

As far as I know you get that error if you enter the parameter more
than once, however i can't find anywhere in my code where the value is
entered more than once. What could be the problem?

Thanks in advance for the help :-)

Details.aspx

<asp:objectdatasource id="ObjectDataSource1" runat="server"
insertmethod="InsertMomDetails"


oninserted="Formview2_Inserted" oninserting="Numbers_Inserting"
selectmethod="GetSingleMom"

typename="Classes">

<selectparameters>

<asp:sessionparameter name="mId" sessionfield="mid" type="String" />

</selectparameters>

<insertparameters>

<asp:parameter name="Id" type="Int32" />

<asp:parameter name="Mid" type="String" />

<asp:parameter name="FamilyName" type="String" />

<asp:parameter name="FirstName" type="String" />

<asp:parameter name="Flat" type="String" />

<asp:parameter name="House" type="String" />

<asp:parameter name="Street" type="String" />

<asp:parameter name="Mikud" type="String" />

<asp:parameter name="PartnerName" type="String" />

<asp:parameter name="Cityid" type="Int32" />

<asp:parameter name="TimesParticipated" type="Int32" />

<asp:parameter name="LastMenstrualDate" type="DateTime" />

<asp:parameter name="Birthdate" type="DateTime" />

<asp:parameter name="ScheduleDate" type="DateTime" />

<asp:parameter name="PregnancyWeek" type="Int32" />

</insertparameters>

</asp:objectdatasource>
Protected Sub Numbers_Inserting(ByVal sender As Object, ByVal e As
ObjectDataSourceMethodEventArgs)


Dim Telephone As String

Dim Cellular As String


Dim cellPre As DropDownList = FormView2.FindControl("ddl_iCell")

Dim cPre As String = cellPre.SelectedValue

Dim cell As TextBox = FormView2.FindControl("i_Cellular")
Cellular = cPre &

"-" & cell.Text

Dim ddl_iPhone As DropDownList = FormView2.FindControl("ddl_iPhone")

Dim i_phone As TextBox = FormView2.FindControl("i_Telephone")
Telephone = ddl_iPhone.SelectedValue &

"-" & i_phone.Text
e.InputParameters.Add(

"Cellular", Cellular) -error line
e.InputParameters.Add(

"Telephone", Telephone)

End Sub
Classes.vb

Public Function InsertMomDetails(ByVal Mid As String, _

ByVal FamilyName As String, _

ByVal FirstName As String, _

ByVal Flat As String, _

ByVal House As String, _

ByVal Street As String, _

ByVal Mikud As String, _

ByVal Telephone As String, _

ByVal Cellular As String, _

ByVal PartnerName As String, _

ByVal Cityid As Integer, _

ByVal TimesParticipated As Integer, _

ByVal LastMenstrualDate As DateTime, _

ByVal Birthdate As DateTime, _

ByVal ScheduleDate As DateTime, _

ByVal PregnancyWeek As Integer)

Dim myCommand As New SqlCommand("InsertMomDetails", myConnection)

myCommand.CommandType = CommandType.StoredProcedure

myCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.Int))

myCommand.Parameters("@Id").Direction = ParameterDirection.Output

myCommand.Parameters("@Id").SourceColumn = "Id"

myCommand.Parameters.AddWithValue(

"@Mid", Mid)
myCommand.Parameters.AddWithValue(

"@FamilyName", FamilyName)
myCommand.Parameters.AddWithValue(

"@FirstName", FirstName)
myCommand.Parameters.AddWithValue(

"@Flat", Flat)
myCommand.Parameters.AddWithValue(

"@House", House)
myCommand.Parameters.AddWithValue(

"@Street", Street)
myCommand.Parameters.AddWithValue(

"@Mikud", Mikud)
myCommand.Parameters.AddWithValue(

"@Telephone", Telephone)
myCommand.Parameters.AddWithValue(

"@Cellular", Cellular)
myCommand.Parameters.AddWithValue(

"@PartnerName", PartnerName)
myCommand.Parameters.AddWithValue(

"@Cityid", Cityid)
myCommand.Parameters.AddWithValue(

"@TimesParticipated", TimesParticipated)
myCommand.Parameters.AddWithValue(

"@LastMenstrualDate", LastMenstrualDate)
myCommand.Parameters.AddWithValue(

"@Birthdate", Birthdate)
myCommand.Parameters.AddWithValue(

"@Scheduledate", ScheduleDate)
myCommand.Parameters.AddWithValue(

"@PregnancyWeek", PregnancyWeek)
myConnection.Open()


Dim affectedRows As Integer = myCommand.ExecuteNonQuery()

''close connection
myConnection.Close()


Dim returnValue As Integer

If (affectedRows > 0) Then
returnValue = myCommand.Parameters(

"@Id").Value

End If

Return returnValue

End Function

Author
19 Dec 2006 3:37 PM
Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
It seems to me that this error would happen if your Numbers_Inserting
function is called more than once.
Perhaps you should check the collection for the existence of that key before
attempting to add it.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net


Show quoteHide quote
"avital" <avital.k***@clalit.org.il> wrote in message
news:1165931922.914440.65950@73g2000cwn.googlegroups.com...
> Hi,
> When inserting a new item i get the following error: Item has already
> been added. Key in dictionary: 'Cellular'  Key being added: 'Cellular'
>
> As far as I know you get that error if you enter the parameter more
> than once, however i can't find anywhere in my code where the value is
> entered more than once. What could be the problem?
>
> Thanks in advance for the help :-)
>
> Details.aspx
>
> <asp:objectdatasource id="ObjectDataSource1" runat="server"
> insertmethod="InsertMomDetails"
>
>
> oninserted="Formview2_Inserted" oninserting="Numbers_Inserting"
> selectmethod="GetSingleMom"
>
> typename="Classes">
>
> <selectparameters>
>
> <asp:sessionparameter name="mId" sessionfield="mid" type="String" />
>
> </selectparameters>
>
> <insertparameters>
>
> <asp:parameter name="Id" type="Int32" />
>
> <asp:parameter name="Mid" type="String" />
>
> <asp:parameter name="FamilyName" type="String" />
>
> <asp:parameter name="FirstName" type="String" />
>
> <asp:parameter name="Flat" type="String" />
>
> <asp:parameter name="House" type="String" />
>
> <asp:parameter name="Street" type="String" />
>
> <asp:parameter name="Mikud" type="String" />
>
> <asp:parameter name="PartnerName" type="String" />
>
> <asp:parameter name="Cityid" type="Int32" />
>
> <asp:parameter name="TimesParticipated" type="Int32" />
>
> <asp:parameter name="LastMenstrualDate" type="DateTime" />
>
> <asp:parameter name="Birthdate" type="DateTime" />
>
> <asp:parameter name="ScheduleDate" type="DateTime" />
>
> <asp:parameter name="PregnancyWeek" type="Int32" />
>
> </insertparameters>
>
> </asp:objectdatasource>
> Protected Sub Numbers_Inserting(ByVal sender As Object, ByVal e As
> ObjectDataSourceMethodEventArgs)
>
>
> Dim Telephone As String
>
> Dim Cellular As String
>
>
> Dim cellPre As DropDownList = FormView2.FindControl("ddl_iCell")
>
> Dim cPre As String = cellPre.SelectedValue
>
> Dim cell As TextBox = FormView2.FindControl("i_Cellular")
> Cellular = cPre &
>
> "-" & cell.Text
>
> Dim ddl_iPhone As DropDownList = FormView2.FindControl("ddl_iPhone")
>
> Dim i_phone As TextBox = FormView2.FindControl("i_Telephone")
> Telephone = ddl_iPhone.SelectedValue &
>
> "-" & i_phone.Text
> e.InputParameters.Add(
>
> "Cellular", Cellular) -error line
> e.InputParameters.Add(
>
> "Telephone", Telephone)
>
> End Sub
> Classes.vb
>
> Public Function InsertMomDetails(ByVal Mid As String, _
>
> ByVal FamilyName As String, _
>
> ByVal FirstName As String, _
>
> ByVal Flat As String, _
>
> ByVal House As String, _
>
> ByVal Street As String, _
>
> ByVal Mikud As String, _
>
> ByVal Telephone As String, _
>
> ByVal Cellular As String, _
>
> ByVal PartnerName As String, _
>
> ByVal Cityid As Integer, _
>
> ByVal TimesParticipated As Integer, _
>
> ByVal LastMenstrualDate As DateTime, _
>
> ByVal Birthdate As DateTime, _
>
> ByVal ScheduleDate As DateTime, _
>
> ByVal PregnancyWeek As Integer)
>
> Dim myCommand As New SqlCommand("InsertMomDetails", myConnection)
>
> myCommand.CommandType = CommandType.StoredProcedure
>
> myCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.Int))
>
> myCommand.Parameters("@Id").Direction = ParameterDirection.Output
>
> myCommand.Parameters("@Id").SourceColumn = "Id"
>
> myCommand.Parameters.AddWithValue(
>
> "@Mid", Mid)
> myCommand.Parameters.AddWithValue(
>
> "@FamilyName", FamilyName)
> myCommand.Parameters.AddWithValue(
>
> "@FirstName", FirstName)
> myCommand.Parameters.AddWithValue(
>
> "@Flat", Flat)
> myCommand.Parameters.AddWithValue(
>
> "@House", House)
> myCommand.Parameters.AddWithValue(
>
> "@Street", Street)
> myCommand.Parameters.AddWithValue(
>
> "@Mikud", Mikud)
> myCommand.Parameters.AddWithValue(
>
> "@Telephone", Telephone)
> myCommand.Parameters.AddWithValue(
>
> "@Cellular", Cellular)
> myCommand.Parameters.AddWithValue(
>
> "@PartnerName", PartnerName)
> myCommand.Parameters.AddWithValue(
>
> "@Cityid", Cityid)
> myCommand.Parameters.AddWithValue(
>
> "@TimesParticipated", TimesParticipated)
> myCommand.Parameters.AddWithValue(
>
> "@LastMenstrualDate", LastMenstrualDate)
> myCommand.Parameters.AddWithValue(
>
> "@Birthdate", Birthdate)
> myCommand.Parameters.AddWithValue(
>
> "@Scheduledate", ScheduleDate)
> myCommand.Parameters.AddWithValue(
>
> "@PregnancyWeek", PregnancyWeek)
> myConnection.Open()
>
>
> Dim affectedRows As Integer = myCommand.ExecuteNonQuery()
>
> ''close connection
> myConnection.Close()
>
>
> Dim returnValue As Integer
>
> If (affectedRows > 0) Then
> returnValue = myCommand.Parameters(
>
> "@Id").Value
>
> End If
>
> Return returnValue
>
> End Function
>