12 Replies - 516 Views - Last Post: 10 January 2019 - 10:00 PM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Getting error when showing data in list view

Posted 05 January 2019 - 10:29 PM

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)

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:

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)
                              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

This post has been edited by andrewsw: 08 January 2019 - 01:28 AM
Reason for edit:: removed "please help me" fluff from title


Is This A Good Question/Topic? 0
  • +

Replies To: Getting error when showing data in list view

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,718
  • Joined: 12-June 08

Re: Getting error when showing data in list view

Posted 06 January 2019 - 01:11 AM

What is your key column?
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Re: Getting error when showing data in list view

Posted 06 January 2019 - 05:33 AM

View Postmodi123_1, on 06 January 2019 - 01:41 PM, said:

What is your key column?


listitem.SubItems.Add(dr("SJID").ToString())

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,718
  • Joined: 12-June 08

Re: Getting error when showing data in list view

Posted 06 January 2019 - 12:12 PM

Is that the line it is happening on as well?
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Re: Getting error when showing data in list view

Posted 06 January 2019 - 10:30 PM

View Postmodi123_1, on 07 January 2019 - 12:42 AM, said:

Is that the line it is happening on as well?


No sir
error showing when loop run 2nd time when debug this line -
ListView1.Items.Add(listitem)

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,718
  • Joined: 12-June 08

Re: Getting error when showing data in list view

Posted 06 January 2019 - 10:33 PM

You need to move that line out of the for loop so the same instantiated object in memory is not being added.
Was This Post Helpful? 0
  • +
  • -

#7 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Re: Getting error when showing data in list view

Posted 06 January 2019 - 10:44 PM

View Postmodi123_1, on 07 January 2019 - 11:03 AM, said:

You need to move that line out of the for loop so the same instantiated object in memory is not being added.

if i do this show only one item not all means i have 3(three) item in the middle loop (means which is under the 1st loop)
please help me sir
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1530
  • View blog
  • Posts: 3,856
  • Joined: 01-February 13

Re: Getting error when showing data in list view

Posted 07 January 2019 - 04:13 PM

The problem you are having is because you are trying to add the same ListViewItem to the ListView several times inside your inner loop, then again after the inner loop is exited. I don't think you are grasping the idea that each row in the ListView needs to have a separate New ListViewItem assigned to it. As you create the New ListViewItem for each row, you would assign the SubItems for just that row.

Anyways, you can try the following changes to your code and see if it helps. I commented the changes I made to help understand it better. I did not test it but, i would bet it fixes your problem.
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())

                   ListView1.Items.Add(listitem) 'add the first ListViewItem (row) to the ListView

                   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 lvi as New ListViewItem 'create a New ListViewItem for the current row

                              'ListViewItem(dr1("itemnmDES").ToString())  'this line is not doing anything ???

                               lvi.SubItems.Add(dr1("itemnmDES").ToString())
                               lvi.SubItems.Add(dr1("SJdqty").ToString())
                               lvi.SubItems.Add(dr1("SJdPCS").ToString())
                               lvi.SubItems.Add(dr1("SJdRate").ToString())
                               lvi.SubItems.Add(dr1("SJdamt").ToString())
                               lvi.SubItems.Add(dr1("SJDstock").ToString())
                               lvi.SubItems.Add(dr1("SJid").ToString())

                               ListView1.Items.Add(lvi) 'add the ListViewItem for this row to the ListView

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

               Next
           End With


Was This Post Helpful? 1
  • +
  • -

#9 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Re: Getting error when showing data in list view

Posted 07 January 2019 - 08:45 PM

Thanks sir, you are great. We are Indian. When we are give the ultimate respect to , we bow to their feet with hands. Please feel it.

Sir,
It is working fine. But it shows on the billow of the 1st loop. How can I shift it particular row column.
I add some blank subitem at first of the 2nd loop that for it is right position (I want), but not right cell.
I shift right but I con't shift UP. How it is possible.
please help me
 Dim lvi As New ListViewItem 'create a New ListViewItem for the current row

                            'ListViewItem(dr1("itemnmDES").ToString())  'this line is not doing anything ???
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add("")
                            lvi.SubItems.Add(dr1("itemnmDES").ToString())
                            lvi.SubItems.Add(dr1("SJdqty").ToString())
                            lvi.SubItems.Add(dr1("SJdPCS").ToString())
                            lvi.SubItems.Add(dr1("SJdRate").ToString())
                            lvi.SubItems.Add(dr1("SJdamt").ToString())
                            lvi.SubItems.Add(dr1("SJDstock").ToString())
                            lvi.SubItems.Add(dr1("SJid").ToString())

Please See the picture.

https://ibb.co/MGyH0zz
https://ibb.co/tDtJbj4
https://ibb.co/RHzDKxH

This post has been edited by andrewsw: 08 January 2019 - 01:27 AM
Reason for edit:: removed redundant previous quote, use the REPLY button

Was This Post Helpful? 0
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1530
  • View blog
  • Posts: 3,856
  • Joined: 01-February 13

Re: Getting error when showing data in list view

Posted 08 January 2019 - 04:39 PM

Looking at your image links, it appears that you want to add the first DataRow values in your inner For Next loop to the end of (listitem), and the rest of the DataRows to new ListViewItems. That could be done by using a simple If Then Else statement inside the inner loop to check if the loop value i1 = 0, indicating it is the first row. For Example...
                           For i1 As Integer = 0 To dt2.Rows.Count - 1
                               Dim dr1 As DataRow = dt2.Rows(i1)

                               Dim lvi as ListViewItem = Nothing

                               If i1 = 0 Then 'if this is the first DataRow, then add the subitems to the (listitem) so they appear in the first row of the listview
                                   lvi = listitem 'set (lvi) to reference (listitem)
                               Else 'this is not the first row so...
                                   lvi = ListView1.Items.Add("") 'add a new ListViewItem to the ListView for the next row in the listview
                                   lvi.SubItems.AddRange(New String() {"", "", "", "", "", "", ""}) 'add your 7 leading subitems with empty Text to this new ListViewItem
                               End If

                               'now add your subitems to the end
                               lvi.SubItems.Add(dr1("itemnmDES").ToString())
                               lvi.SubItems.Add(dr1("SJdqty").ToString())
                               lvi.SubItems.Add(dr1("SJdPCS").ToString())
                               lvi.SubItems.Add(dr1("SJdRate").ToString())
                               lvi.SubItems.Add(dr1("SJdamt").ToString())
                               lvi.SubItems.Add(dr1("SJDstock").ToString())
                               lvi.SubItems.Add(dr1("SJid").ToString())

                           Next i1


Was This Post Helpful? 1
  • +
  • -

#11 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Re: Getting error when showing data in list view

Posted 09 January 2019 - 09:54 PM

Thanks Sir, It is done. Just I want thanks sir.

This post has been edited by andrewsw: 10 January 2019 - 01:28 AM
Reason for edit:: removed previous quote, use the Reply button

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: Getting error when showing data in list view

Posted 10 January 2019 - 01:27 AM

Please do not quote the previous post in full, use the Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#13 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 16-May 17

Re: Getting error when showing data in list view

Posted 10 January 2019 - 10:00 PM

Okay Sir,Thanks. I'll remember it.

This post has been edited by [email protected]: 10 January 2019 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1