0

This is my code behind

Private Property ListEmployee As List(Of Employee)
    Get
        Return Session(Me.GetType.Name)
    End Get
    Set(ByVal value As List(Of Employee))
        Session(Me.GetType.Name) = value
    End Set
End Property
Private ReadOnly Property objEmployee As Employee
    Get
        Dim obj As New Employee
        obj.User_ID = txtUserID.Text.Trim
        obj.Name = txtName.Text.Trim
        obj.Position = txtPosition.Text.Trim
        Return obj
    End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
Protected Sub btnAddtoTable_Click(sender As Object, e As EventArgs) Handles btnAddtoTable.Click
    ListEmployee.Add(objEmployee) 'error at here
    gvResult.DataSource = ListEmployee
    gvResult.DataBind()
End Sub

End Class

i try to add data as (Class Employee) into ListEmployee as (List of Employee) and then display on gridview but i got an error.

  • 1
    Have you tried stepping into your code to see what the values for `ListEmployee` and `objEmployee` are? Most likely `ListEmployee` is not in `Session`. – Tim May 17 '14 at 06:02
  • ListEmployee ==> value = count = 0 (Error No children available) objEmployee ==> has value – user3534404 May 17 '14 at 07:32

1 Answers1

0

It is best to breakpoint and split the function up to see where the fault lies but looking at your code I am pretty sure the problem is in the ListEmployee object. ListEmployee.Add() will get a reference to the ListEmplyee object and you are not checking if this object actually exists yet. You need to do some data validation checking in both the ListEmployee property and the objEmployee, I will do it in the ListEmployee for you here:

Private Property ListEmployee As List(Of Employee)
Get
    If Not IsNothing(Session(Me.GetType.Name)) Then
       Return Session(Me.GetType.Name)
    Else
       Dim le as New List(Of Employee)
       le.add(objEmployee)
       Session(Me.GetType.Name) = le
       Return le
    End If
End Get
Set(ByVal value As List(Of Employee))
    Session(Me.GetType.Name) = value
End Set
End Property
Craig Moore
  • 1,093
  • 1
  • 6
  • 15
  • Thanks Mr.Moore. The error was solved but i don't get any data in my gridview. – user3534404 May 17 '14 at 07:16
  • Yes that would be correct. What is happening is that you are trying to load data from `Session(Me.GetType.Name)` but there is nothing there. You are not loading data into the session object. To solve this problem you need to work out why there is no data in `Session(Me.GetType.Name)`. Are you sure you have loaded it? – Craig Moore May 17 '14 at 08:14
  • Do you have any idea How do i add objEmployee into ListEmployee ==>Session(Me.GetType.Name) – user3534404 May 17 '14 at 08:33
  • If I understand you want to create a new instance of objEmployee, then add this to ListEmployee and then put that inside Session(Me.GetType.Name). Is that correct? – Craig Moore May 17 '14 at 08:37
  • yes and then Bind it into Gridview. – user3534404 May 17 '14 at 08:40
  • I have modifed my answer. It is not advised to put complex objects in the session memory but there it is anyway. – Craig Moore May 17 '14 at 08:41
  • Thanks a lot Mr.Moore. You are genius. – user3534404 May 17 '14 at 08:49