Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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
Comments
GuyThiebaut 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 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,
[no name] 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
:)

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
 
Share this answer
 
Comments
Zubair Lohani 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
Replace your hole su code for loading the data into the list with this

VB
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
 
Share this answer
 
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

HTML
ListView2.Clear()


If its not working you fill free comment on my answer.

Happy Coding Buddy
:)
 
Share this answer
 

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