Hi, can you help me please how can i populate data from my listview using sqldatareader.

by the way can you please explain me why is it that listview in first column we call item but the other column we call it subitems.can you please enligthen my mind on this.I am confuse in this listview.

this is what i have got but it will not display properly did i missed something???

Private Sub DisplayRecords(ByVal lvw As ListView)



    mysqlcommand = New SqlCommand("select * from myTable", con)

    Dim reader As SqlDataReader = mysqlcommand.ExecuteReader





    While reader.Read()

        lvw.Items.Add(reader.Item(0))
        lvw.Items.Add(lvw.Items.Count - 1).SubItems.Add(reader.Item(1))
        lvw.Items.Add(lvw.Items.Count - 1).SubItems.Add(reader.Item(2))


    End While




End Sub

Recommended Answers

All 3 Replies

Here's an example using the Microsoft PUBS sample database.

ListView1.Items.Clear()

Dim con As New SqlConnection("Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;")
Dim cmd As New SqlCommand

con.Open()

cmd.Connection = con
cmd.CommandText = "SELECT au_lname,au_fname,zip FROM authors"

Dim rdr As SqlDataReader = cmd.ExecuteReader

If rdr.HasRows Then
    Do While rdr.Read()

        Dim item As New ListViewItem

        item.Text = rdr("au_lname")
        item.SubItems.Add(rdr("au_fname"))
        item.SubItems.Add(rdr("zip"))

        ListView1.Items.Add(item)

    Loop
Else
    MsgBox("no records")
End If

rdr.Close()
con.Close()

Hi, It's working can you share me the link of this....by the way is there another way that it will not use the column name of our table? like au_lname,au_fname and zip.

why is it first item using this

item.Text = rdr("au_lname")

item.text ?

can you share me the link of this

The link to what?

You can substitute names that are appropriate for your table. I just copied some sample code that I had from my system. You can also index by field number (zero relative) although this is not recommended because a reorder of the table (insertion of a new column) can change the numbers.

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.