Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB10.0 VB.NET
i got all the values of all columns in 1st column and remaining are empty, everytime i load the listview its number of rows and entries increases automatically ,
what should i do,
 
Imports System.Data.SqlClient
Imports System.IO
Imports System.Windows.Forms.TabControl
Public Class IssueWorkOrder
Inherits System.Windows.Forms.Form
Dim cn As System.Data.SqlClient.SqlConnection
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String
Sub connect()
cn = New System.Data.SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=Fleet Maintainance;Integrated Security=True")
End Sub
Sub listView2load()
       Me.ListView2.View = View.Details
       Me.ListView2.GridLines = True
       Dim conn As New SqlConnection("Data Source=localhost;Initial Catalog=Fleet Maintainance;Integrated Security=True")
       conn.Open()
       Dim clm1 As String
       Dim clm2 As String
       Dim clm3 As String
       Dim clm4 As String
       ListView2.Columns.Clear()
       Dim cmd As New SqlCommand("select * from MaintenanceTask", conn)
       cmd.CommandType = CommandType.Text
       Dim reader As SqlDataReader = cmd.ExecuteReader()
       Me.ListView2.Columns.Add("PartNumber")
       Me.ListView2.Columns.Add("Name")
       Me.ListView2.Columns.Add("QuantityUsed")
       Me.ListView2.Columns.Add("UnitCost")
       While reader.Read()
           clm1 = reader("PartNumber").ToString
           ListView2.Items.Add(clm1)
           clm2 = reader("Name").ToString
           ListView2.Items.Add(clm2)
           clm3 = reader("QuantityUsed").ToString
           ListView2.Items.Add(clm3)
           clm4 = reader("UnitCost").ToString
           ListView2.Items.Add(clm4)
       End While
       reader.Close()
       conn.Close()
 

   End Sub
Posted 17-Mar-13 21:34pm
Comments
GuyThiebaut at 19-Mar-13 7:35am
   
In answer to why the number of rows increases, you will need to add this before you populate your listview:
 
ListView2.Items.Clear()
Zubair Lohani at 19-Mar-13 7:54am
   
Thanks alot, now tell me what should i do to populate all columns from the relevant fields from database, because i got all the data in a single column, i think its disorder of code, but i cannot fetch the line where i notice that here is disorder of code, plz help me,
BindassFrind at 5-Apr-13 8:03am
   
Hello, Buddy
In your coding you don't refresh your listview
If you don't empty your ListView so it's added items are stored in that and when list view is load that every time that added items are repeat.
So, for that reason rows and entries increases automatically of ListWiew.
When you adding item its first clear your ListView
 
Happy Coding Buddy
:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Copy and replace this where you have your while call
 
While reader.Read()
    
  Dim nItem As New ListViewItem = ListView2.Items.Add(reader.item(0).tostring)
  nItem.Subitems.add(reader.item(1).tostring)
  nItem.Subitems.add(reader.item(2).tostring)
  nItem.Subitems.add(reader.item(3).tostring)
End While
 
This will put your database columns data in order into the corresponding Listview columns
 
Let me know if this worked for you
  Permalink  
Comments
Zubair Lohani at 22-Mar-13 2:47am
   
Error 1 End of statement expected.
Error 2 'nItem' is not declared. It may be inaccessible due to its protection level.
I got these errors
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Replace your hole su code for loading the data into the list with this
 
Sub listView2load()
       Me.ListView2.View = View.Details
       Me.ListView2.GridLines = True
       Dim conn As New SqlConnection("Data Source=localhost;Initial Catalog=Fleet Maintainance;Integrated Security=True")
       conn.Open()
       Dim clm1 As String
       Dim clm2 As String
       Dim clm3 As String
       Dim clm4 As String
       ListView2.Columns.Clear()
       Dim cmd As New SqlCommand("select * from MaintenanceTask", conn)
       cmd.CommandType = CommandType.Text
       Dim reader As SqlDataReader = cmd.ExecuteReader()
       Me.ListView2.Columns.Add("PartNumber")
       Me.ListView2.Columns.Add("Name")
       Me.ListView2.Columns.Add("QuantityUsed")
       Me.ListView2.Columns.Add("UnitCost")
       While reader.Read()
            Dim nItem As New ListViewItem
            nItem = listview2.items.add(reader.item(0).tostring)
            nItem.SubItems.Add(reader.item(1).tostring)
            nItem.SubItems.Add(reader.item(2).tostring)
            nItem.SubItems.Add(reader.item(3).tostring)
       End While
       reader.Close()
       conn.Close()
 

   End Sub
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello, Buddy
 
In your coding you don't refresh your listview
If you don't empty your ListView so it's added items are stored in that and when list view is load that every time that added items are repeat.
So, for that reason rows and entries increases automatically of ListWiew.
When you adding item its first clear your ListView
 
you only write this line before you add your items in your listview2
 
ListView2.Clear()
 
If its not working you fill free comment on my answer.
 
Happy Coding Buddy
Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100