Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is my collection empty in page_load? 1

Status
Not open for further replies.

AndyInNC

Programmer
Sep 22, 2008
76
US
Please tell me what I am missing here....

Why would my Employees object be Null where the datasource is set? (This is in the initial page_load.)

Code:
[blue]Partial Class [black]_Default[/black]
    Inherits [black]System.Web.UI.Page[/black]

    Protected Sub [black]Page_Load([/black]ByVal [black]sender[/black] As Object, ByVal [black]e[/black] As [black]System.EventArgs)[/black] Handles Me[black].Load[/black]
        If Not [black]Page.IsPostBack[/black] Then
            Dim [black]Employees[/black] As New [black]Collection
            Employees.Add([/black]New [black]Employee("George", "Washington"))
            Employees.Add([/black]New [black]Employee("John", "Adams"))
            Employees.Add([/black]New [black]Employee("Thomas", "Jefferson"))

            GridView5.DataSource = Employees [green]' NullReferenceException at this point[/green]
            GridView5.DataBind()[/black]

        End If
    End Sub
End Class

Public Class [black]Employee[/black]
    Private [black]_FirstName[/black] As String
    Public Property [black]FirstName[/black]() As String
        Get
            Return [black]_FirstName[/black]
        End Get
        Set(ByVal [black]Value[/black] As String)
            [black]_FirstName = Value[/black]
        End Set
    End Property

    Private [black]_LastName[/black] As String
    Public Property [black]LastName[/black]() As String
        Get
            Return [black]_LastName[/black]
        End Get
        Set(ByVal [black]Value[/black] As String)
            [black]_LastName = Value[/black]
        End Set
    End Property

    Public Sub New(ByVal [black]FName[/black] As String, ByVal [black]LName[/black] As String)
        [black]_FirstName = FName
        _LastName = LName[/black]
    End Sub
End Class[/blue]
 
Not sure if this is it, but try setting the property, instead of the variable:
Me.FirstName = FName
Me.LastName = LName
 
Nah, but thanks.

If I put this in the Immediate window:
[tt]
?Employees.Item(2).FirstName
[/tt]
It returns "John"

I feel like it's gotta be something obvious that I've just overlooked.
 
So it's adding to the collection then. If you put a breakpoint on your .datasource line, do a Employees.count in the watch window, you should see a count of 3.
 
You're right. I see 3.

So it doesn't make sense that it goes from 3 items to a Null exception.
 
The problem must be with the gridview definition then. Did you add the columns manually to the grid? If so, maybe you added one that is not in the Employee object. If you just drop a gridview on the page(and don't modify it) and use your code, it will work.
 
You're right.

There's nobody here to look over my shoulder. The problem was that the grid is inside a loginView. I realized it as soon as you said that about the grid (which, indeed, is barebones at this point).

Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top