Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using the code below to populate my listview. the 'count' integer value shows some value but the listview remains empty. what am i doing wrong?

Public Sub populate_lvw()
       If con.State = ConnectionState.Open Then
           con.Close()
       End If

       lvw.Items.Clear()
       lvw.Scrollable = True
       ds.Clear()
       da.Dispose()

       str = "SELECT scholarship_id FROM Scholarships WHERE scholarship_category= '" & categoryCb.Text & "'"
       Dim cmd As SqlCommand = New SqlCommand(str, con)
       con.Open()
       Dim scholarship_id As Integer = CInt(cmd.ExecuteScalar())
       MsgBox(scholarship_id)

       Dim dr As SqlDataReader = Nothing
       cmd = New SqlCommand("Select * from Students where scholarship_id like @searchitem  order by student_slno", con)
       cmd.Parameters.AddWithValue("@searchitem", scholarship_id)
       dr = cmd.ExecuteReader

       Dim count As Integer = 0
       While dr.Read
           count = count + 1
       End While
       MsgBox(count)

       If dr.HasRows Then
           While dr.Read
               Dim li As New ListViewItem()
               li.Text = Convert.ToString(dr.Item("student_slno"))
               li.SubItems.Add(Convert.ToString(dr.Item("student_fullname")))
               ' li.SubItems.Add(Convert.ToString(dr.Item("student_dob")))
               li.SubItems.Add(CType(dr.Item("student_dob"), DateTime).ToShortDateString)
               li.SubItems.Add(Convert.ToString(dr.Item("student_branch")))
               li.SubItems.Add(Convert.ToString(dr.Item("student_scholarship")))
               li.SubItems.Add(Convert.ToString(dr.Item("student_accountno")))
               lvw.Items.Add(li)
           End While
       Else : MsgBox("Sorry, no records found!")
       End If
       dr.Close()
       con.Close()
   End Sub
Posted
Comments
Richard Deeming 22-Apr-15 7:33am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Check if the Listview.View = Details, then you need at least one column defined in the columns collection.
 
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