这是我背后的代码
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
结束班
我尝试将数据作为(类员工)添加到ListEmployee中(员工列表),然后在gridview上显示但是我收到了错误。
答案 0 :(得分:0)
最好是断点并将函数拆分以查看故障所在但查看代码我很确定问题出在ListEmployee对象中。 ListEmployee.Add()
将获得对ListEmplyee对象的引用,并且您不会检查此对象是否确实存在。您需要在ListEmployee
属性和objEmployee
中执行一些数据验证检查,我将在ListEmployee
为您执行此操作:
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