i have codes for adding data to listview from database but there's a problem ..

     Public Sub showmyrecords()
    Dim lvi As ListViewItem
    Dim dt As New DataTable
    Dim ds As New DataSet
    ' Dim intCount As Integer

    ds.Tables.Add(dt)

    Dim da As New OleDbDataAdapter("Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "' And Sections = '" & ComboBox1.Text & "' ", asconn)
    da.Fill(dt)
    Dim acscmd As New OleDb.OleDbCommand

    acscmd.CommandText = strsql
    acscmd.Connection = asconn
    dr = acscmd.ExecuteReader()

    For Each myrow__1 As DataRow In dt.Rows
        lvi = New ListViewItem()
        lvi.Text = myrow__1(5)
        lvi.SubItems.Add(myrow__1(6))
        lvi.SubItems.Add(myrow__1(7))
        lvi.SubItems.Add(myrow__1(8))
        lvi.SubItems.Add(myrow__1(9))
        lvi.SubItems.Add(myrow__1(10))
        ListView1.Items.Add(lvi)
    Next
End Sub

it produces the rigth data but it produces the same data as many as records that satisfies my query...
please.. help me . . i've been searching it in hours but there's no correct output. thank youuuuu in advance. :)

Use a list box instead of list view

before the code clear the list box every time else the value will be repeated...

use a distinct function in ur select query if the query is returning duplicate values

after the executer use a debug.Print("Query: "+ acscmd.CommandText) it will help u to understand the query

With ListBox1
            .DataSource = ds.Tables("TableName")
            .DisplayMember = "ColumnName"


        End With

poojav.. how could i make listbox with columns? i'm a newbie. kindly teach me how tosolve my problem?

listbox1.Items.Add("Item you want to add 1" & vbtab & "Item you want to add 2")

pgmer > i want the item from my database.. and why is it it produce two rows?

DbCon = New SqlConnection("data source=.; integrated security=true; database=kehkashan;")
DbCon.Open()

    Da = New SqlDataAdapter()
    Ds = New DataSet
    iRow = 0
    Da = New SqlDataAdapter("select * from tblSupplier order by ID", DbCon)
    Da.Fill(Ds)
    While iRow <> Ds.Tables(0).Rows.Count
        cboSupplier.Items.Add(Ds.Tables(0).Rows(iRow).Item(2))
        iRow = iRow + 1
    End While


    it is a combo box filling method .. try it in Listview box ... 
    ..




    .
    Me.ListView1.Clear()
    Me.ListView1.LargeImageList = Me.ImageList1
    Me.ListView1.View = View.LargeIcon
    Me.ListView1.Items.Add("Sale", "Sales", 0)
    Me.ListView1.Items.Add("Pur", "Purchases", 1)
    Me.ListView1.Items.Add("Exp", "Expences", 2)
    Me.ListView1.Items.Add("SR", "Sale Return", 3)
    Me.ListView1.Items.Add("Cat", "Catogory", 4)
    Me.ListView1.Items.Add("Cust", "Customer Managment", 5)
    Me.ListView1.Items.Add("Sup", "Supplier Managment", 6)
    Me.ListView1.Items.Add("Emp", "Staff Managment", 7)

@saleem items produce in one column only.. and repeated 8 times.

i figure out already it alreadyyyyy with the help of my friend . :D

here's the final code.
just want to share.

Public Sub showmyrecords()
    Dim lvi As ListViewItem
    Dim dt As New DataTable
    Dim ds As New DataSet
    ' Dim intCount As Integer

    ds.Tables.Add(dt)

    Dim da As New OleDbDataAdapter("Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "' And Sections = '" & ComboBox1.Text & "' ", asconn)
    da.Fill(dt)
    Dim acscmd As New OleDb.OleDbCommand

    acscmd.CommandText = strsql
    acscmd.Connection = asconn
    ListView1.Items.Clear()
    For Each myrow__1 As DataRow In dt.Rows
        lvi = New ListViewItem()
        lvi.Text = myrow__1(5)
        lvi.SubItems.Add(myrow__1(6))
        '6 is 7th column in datatable!!!
        lvi.SubItems.Add(myrow__1(7))
        'and so on...
        lvi.SubItems.Add(myrow__1(8))
        lvi.SubItems.Add(myrow__1(9))
        lvi.SubItems.Add(myrow__1(10))
        ListView1.Items.Add(lvi)

    Next


End Sub

thank youuuuuuu to those who replied. :) it helps me. :) godbless. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.