Filling A ListView Control
I am trying to fill a ListView control following the Groups example in chapter 7 (pages 140 and 141) and keep getting the following error message:
"Object reference is not set to an instance of the object'
The ListView control will only contains one field which will be used to fill the parameter to obtain and view a specific record. Since I use only one field, I do not use the code regarding sub items. My code is as shown below:
Private objDataSetStaff As DataSet (Declared at the form level)
Private Sub FillSearchList()
'Declare variables...
Dim objListViewItem As New ListViewItem
'Initialize a new instance of the data access base class...
Using objData As New WDABase
Try
objData.SQL = "usp_SearchName"
objDataSetStaff = New DataSet
objData.FillDataSet(objDataSetStaff, "Staff")
'Clear previous items...
lstSearch.Items.Clear()
'Process all rows...
For intIndex = 0 To objDataSetStaff.Tables
("tStaff").Rows.Count - 1
'Create new listview item...
objListViewItem = New ListViewItem
'Add the data to the listview item...
objListViewItem.Text = objDataSetStaff.Tables("tStaff").Rows
(intIndex).Item("DisplayName")
'Add the ListView items to the listview control...
lstSearch.Items.Add(objListViewItem)
Next
'Reset the selected index...
lstSearch.SelectedIndex = -1
Catch ex As Exception
MessageBox.Show(ex.Message, "Employee Database")
End Try
End Using
objListViewItem = Nothing
End Sub
|