How do I use my Data Table with 5 columns to populate my Listview with 5 columns?

  • Thread starter Thread starter Max27tech
  • Start date Start date
M

Max27tech

Guest
My code below is not working, its producing this error "Object reference not set to an instance of an object." What it should do is retrieve values from my datatable and populate my listview. Any ideas what is wrong?

Sub fetch_DATABASE(ByVal getclass As String)
load database into datatable into listview

Dim loadThisClass As String
loadThisclass = getclass

load class data from MS access db
Dim cnn As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim dataAdapter As New OleDb.OleDbDataAdapter
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=QuizDB.accdb"
cnn.Open()

With cmd
.Connection = cnn
.CommandText = "SELECT * FROM " & loadThisClass
End With

create datatable
Dim MyDataTable As New DataTable
dataAdapter.SelectCommand = cmd

use dataAdapter values to fill the DataTable
dataAdapter.Fill(MyDataTable)

loop through each row of the datatable and add to ListItem
Dim MyDataRow As DataRow
For i = 0 To MyDataTable.Rows.Count
MyDataRow = MyDataTable.Rows(i)
LVITEMS.SubItems.Add(MyDataRow("UserName").ToString())
LVITEMS.SubItems.Add(MyDataRow("Score1").ToString())
LVITEMS.SubItems.Add(MyDataRow("Score2").ToString())
LVITEMS.SubItems.Add(MyDataRow("Score3").ToString())
LVITEMS.SubItems.Add(MyDataRow("AverageScore").ToString())
Next i

display in listview
User_listView.Items.Add(LVITEMS)

End Sub

Continue reading...
 
Back
Top