문제

I am filling a datatable on page_load in order to use it later on.

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

I want to use the serviceTable later on Button_Click

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

I am getting an error that says ServiceTable doesn't refer to an Object. Is it possible that the datatable is emptying on postback? How to fix this?

올바른 솔루션이 없습니다

다른 팁

Use the following statement while declaring Service Table:

Public serviceTable as New DataTable()

Hope this helps..

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

After the Post Back , retieve the saved data set from the session variable

as

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

Hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top