Wrox Programmer Forums
|
BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5
This is the forum to discuss the Wrox book Beginning Visual Basic 2005 Databases by Thearon Willis; ISBN: 9780764588945
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 10th, 2006, 09:05 AM
Authorized User
 
Join Date: Aug 2005
Posts: 88
Thanks: 4
Thanked 0 Times in 0 Posts
Default 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


 
Old August 14th, 2006, 08:35 AM
Authorized User
 
Join Date: Aug 2005
Posts: 88
Thanks: 4
Thanked 0 Times in 0 Posts
Default

I got the ListView to show the items but, each of the items also looks like this "ListViewItem: {Text from the field}"

I can't figure out how to strip the extraneous text or change my code so that it doesn't show. Here's my code:

    Private Sub LoadSearch()
        'Declare variables...
        Dim objListViewItem As New ListViewItem

        'Initialize a new instance of the data access base class...
        Using objData As New WDABase
            'Get all employees into a DataReader...
            objData.SQL = "usp_SearchName"
            objData.InitializeCommand()
            objData.OpenConnection()
            objData.DataReader = objData.Command.ExecuteReader
            'See if any data exists before continuing...
            If objData.DataReader.HasRows Then
                'Clear previous items...
                lvwSearch.Items.Clear()
                'Process all rows...
                While objData.DataReader.Read
                    'Create new listview item...
                    objListViewItem = New ListViewItem
                    'Add the data to the listview item...
                    objListViewItem.Text = objData.DataReader.Item("DisplayName")
                    objListViewItem.Tag = objData.DataReader.Item("DisplayName")
                    'Add the ListView item to the ListView control...
                    lvwSearch.Items.Add(objListViewItem)
                End While
            End If
            objData.DataReader.Close()
        End Using
        'Cleanup...
        objListViewItem = Nothing
    End Sub

 
Old August 14th, 2006, 08:56 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

Set a breakpoint on this line of code:

objListViewItem.Text = objData.DataReader.Item("DisplayName")

and query the results of objData.DataReader.Item("DisplayName") to see what it displays.
 
Old August 14th, 2006, 01:43 PM
Authorized User
 
Join Date: Aug 2005
Posts: 88
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thearon:

Thanks for the reply.

I follwed your suggestion and objListViewItem showed correctly. I filled a Label control and it came out exactly as on the control. The result should be "John J Jones" and thr ListViewItems as well as the label show as "ListViewItem: {John J Jones}".

I'm at a loss.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with ListView Control in C# jiggsroger C# 2005 7 April 7th, 2008 04:25 AM
About Listview control yuetqu BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 August 15th, 2006 04:56 AM
ListView Control JelfMaria VB How-To 4 May 5th, 2005 12:39 AM
ListView Control jlr27613 C++ Programming 1 September 14th, 2004 12:58 PM
listview control jakeone Beginning VB 6 1 March 9th, 2004 08:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.