0

我在 page_load 上填充了一个数据表,以便以后使用它。

Public serviceTable as Datatable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If IsPostBack = False Then
                GetData()
            End If
End Sub


Private Sub getData()
        Dim cmd As SqlCommand
        Dim ds As New DataSet
        Dim da As SqlDataAdapter
        Try
            cmd = New SqlCommand(("dbo.getListofServices"), cn)
            cmd.CommandType = CommandType.StoredProcedure
            da = New SqlDataAdapter(cmd)
            da.Fill(ds)
            ServiceTable = ds.Tables(0)
        Catch ex As SqlClient.SqlException
            MsgBox(ex.ToString)
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            If (IsNothing(da) = False) Then da.Dispose()
            If (IsNothing(cmd) = False) Then cmd.Dispose()
        End Try
End Sub

我想稍后在 Button_Click 上使用 serviceTable

Protected Sub btnView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnView.Click

    Try
                If ServiceTable.Rows.Count = 0 Then Exit Sub
                Repeater1.DataSource = ServiceTable
                Repeater1.DataBind()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
    End Sub

我收到一条错误消息,提示ServiceTable 未引用对象。 数据表是否有可能在回发时清空?如何解决这个问题?

4

1 回答 1

0

声明服务表时使用以下语句:

Public serviceTable as New DataTable()

希望这可以帮助..

If Not (Session("MyData") Is Nothing) Then
Session("MyData")=Servicetable

在 Post Back 之后,从 session 变量中检索保存的数据集

作为

ServiceTable= = (DataTable)Session["MyData"];

希望这可以帮助。

于 2013-10-03T08:18:47.043 回答