Problem with adding the data to the listview control

vivek_nz76

Member
Joined
Sep 20, 2005
Messages
8
Programming Experience
3-5
Fails saying that Object reference not set
The code:

Public Class User

Inherits System.Windows.Forms.Form

Dim userCol As CDbUsers



#
Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

userCol = New CDbUsers

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents lvwUser As System.Windows.Forms.ListView

<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()

Me.lvwUser = New System.Windows.Forms.ListView

Me.SuspendLayout()

'

'lvwUser

'

Me.lvwUser.Location = New System.Drawing.Point(8, 8)

Me.lvwUser.Name = "lvwUser"

Me.lvwUser.Size = New System.Drawing.Size(592, 224)

Me.lvwUser.TabIndex = 0

'

'User

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(616, 273)

Me.Controls.Add(Me.lvwUser)

Me.Name = "User"

Me.Text = "User"

Me.ResumeLayout(False)

End Sub

#End Region



Private Sub User_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim oUser As New CData

Dim _User As New CUser

'Header

Call HeaderlvwUser(lvwUser)

'Populating the Collection

userCol = oUser.GetUserDetail()

For Each _User In userCol --FAILS HERE

Call AddItemToListView(_User)

Next





End Sub

Private Sub HeaderlvwUser(ByVal lvwListView As ListView)



With lvwListView

.HeaderStyle = ColumnHeaderStyle.Clickable

.View = View.Details

'Clearing any existing list

.Items.Clear()

.MultiSelect =
False

'Add the column headers

.Columns.Add("UserID", 20, HorizontalAlignment.Center)

.Columns.Add("First Name", 100, HorizontalAlignment.Center)

.Columns.Add("Last Name", 100, HorizontalAlignment.Center)

.Columns.Add("Username", 100, HorizontalAlignment.Center)

.Columns.Add("Password", 100, HorizontalAlignment.Center)

.Sorting = SortOrder.Ascending

End With

End Sub

Private Sub AddItemToListView(ByVal oUser As CUser, Optional ByVal bSelectItem As Boolean = False)

Dim lvwItem As ListViewItem

lvwItem = lvwUser.Items.Add(oUser.UserID)

lvwItem.SubItems.Add(oUser.FName)

lvwItem.SubItems.Add(oUser.LName)

lvwItem.SubItems.Add(oUser.Username)

lvwItem.SubItems.Add(oUser.Password)

End Sub

End
Class

 
When you get an "Object reference not set" error it usually means that the object has no value (or a value of nothing).
It's not possible to see the problem without knowing about the CData & CUser classes.
Place a breakpoint in your code then step through and check the values of the variables to find the problem.
 
OK. I made a slight change to my load method

PrivateSub User_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

Dim oUser AsNew CData
Dim dsUser AsNew DataSet
Dim userInfo AsNew CUser --THIS IS WHERE MY PROPERTIES ARE DECLARED
Dim arrUser AsNew ArrayList
Dim i AsInteger

'Header
Call HeaderlvwUser(lvwUser)

'Populating the Collection
dsUser = oUser.GetUserDetail()

If dsUser.Tables(0).Rows.Count > 0 Then

ForEach row As DataRow In dsUser.Tables(0).Rows

userInfo.UserID = Convert.ToInt32(row.ItemArray(0))
userInfo.FName = row.ItemArray(1)
userInfo.LName = row.ItemArray(2)
userInfo.Username = row.ItemArray(3)
userInfo.Password = row.ItemArray(4)
userCol.Add(userInfo)

Next --SUCESSFULLY ADDS EVERYTHING TO THE COLLECTION

Else

EndIf

ForEach userInfo In userCol --FAILS HERE
Call AddItemToListView(userInfo)
Next

EndSub
 
Back
Top