Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Getting error when showing data in list view, please help me.....
Error : Cannot add or insert the item '10-07-2018' in more than one place. You
must first remove it from its current location or clone it.
Parameter name: item

I use Vb.net 2010 visual basic
sql server 2008 r2
stored procedure
one listview

I want this type of structure (example)

VB
id     date              item_name         qty.          amount
1      1/1/2019            a               2             10
                           B               1             12
                           x               14            100
                           zz              4             52

2      4/1/2019            M               5             80
                           N               4             50


But when I showing to the data 1st item show on list view then getting the error.
It is possible please help me

What I have tried:

VB
With obj_S_J_RPT
    ListView1.Items.Clear()
    .Sdate = txtdtf.Text
    .EDate = txtdtt.Text
     dt1 = .GetStockJournalRPT()

    For i As Integer = 0 To dt1.Rows.Count - 1
        Dim dr As DataRow = dt1.Rows(i)

        Dim listitem As New ListViewItem(dr("SJDate").ToString())
        listitem.SubItems.Add(dr("itemnmsou").ToString())
        listitem.SubItems.Add(dr("SJqty").ToString())
        listitem.SubItems.Add(dr("SJPCS").ToString())
        listitem.SubItems.Add(dr("SJRate").ToString())
        listitem.SubItems.Add(dr("SJamt").ToString())
        listitem.SubItems.Add(dr("SJleft").ToString())
        listitem.SubItems.Add(dr("nrr").ToString())
        listitem.SubItems.Add(dr("SJID").ToString())

        Try
            With obj_S_J_RPT
                'ListView2.Items.Clear()
                .SJID = dr.Item(0)  'ListView1.Items(0).SubItems(8).Text
                dt2 = .GetStockJournalDetails() 'change

                For i1 As Integer = 0 To dt2.Rows.Count - 1
                    Dim dr1 As DataRow = dt2.Rows(i1)
                  Dim listitem_S As New
                   ListViewItem(dr1("itemnmDES").ToString())

                    listitem.SubItems.Add(dr1("itemnmDES").ToString())
                    listitem.SubItems.Add(dr1("SJdqty").ToString())
                    listitem.SubItems.Add(dr1("SJdPCS").ToString())
                    listitem.SubItems.Add(dr1("SJdRate").ToString())
                    listitem.SubItems.Add(dr1("SJdamt").ToString())
                    listitem.SubItems.Add(dr1("SJDstock").ToString())
                    listitem.SubItems.Add(dr1("SJid").ToString())
                    ListView1.Items.Add(listitem)

                Next i1
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

        ListView1.Items.Add(listitem)
    Next
End With
Posted
Updated 6-Jan-19 18:46pm
v5

1 solution

You can not add an item with the same key multiple times, which you do in the line:
Dim listitem As New ListViewItem(dr("SJDate").ToString())
Instead try to use an unique field, maybe the SJID field, or a combination of fields that is unique.
Also try to catch the error before it occurs with:
ListView1.Items.ContainsKey(listitem.Name)
See example in C# here: https://www.c-sharpcorner.com/article/how-do-i-check-for-duplicate-items-in-a-listview/[^]
For VB.NET that would be:
If Not listView1.Items.ContainsKey(listitem.Name) Then
    listView1.Items.Add(listitem)
Else
    MessageBox.Show("Duplicate Item!")
End If

You can use this online converter to convert C# to VB.NET: Code Converter C# to VB and VB to C# – Telerik[^]

In your FOR loop you need to change listitem to listitem_S:
Dim listitem_S As New ListViewItem(dr1("itemnmDES").ToString())
                    listitem.SubItems.Add(dr1("itemnmDES").ToString())
                    listitem.SubItems.Add(dr1("SJdqty").ToString())
                    listitem.SubItems.Add(dr1("SJdPCS").ToString())
                    listitem.SubItems.Add(dr1("SJdRate").ToString())
                    listitem.SubItems.Add(dr1("SJdamt").ToString())
                    listitem.SubItems.Add(dr1("SJDstock").ToString())
                    listitem.SubItems.Add(dr1("SJid").ToString())
                    ListView1.Items.Add(listitem)
 
Share this answer
 
v3
Comments
Jayanta Modak 6-Jan-19 8:04am    
thanks sir for replying
but how can i use this code your link is "C" i don't know "c" please help me...
RickZeeland 6-Jan-19 8:14am    
See the updated solution :)
Jayanta Modak 6-Jan-19 9:10am    
sorry sir it is not work
show same error
RickZeeland 6-Jan-19 9:36am    
Did you change the lines with ListView1.Items.Add(listitem) ?
I see you have two of these lines ...
Jayanta Modak 7-Jan-19 0:25am    
yes sir, error showing when loop run 2nd time when debug this line ----
ListView1.Items.Add(listitem)

or
If Not listView1.Items.ContainsKey(listitem.Name) Then
    listView1.Items.Add(listitem)
Else
    MessageBox.Show("Duplicate Item!")
End If

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