GridView Sorting Error

In my home page I declare the two following variables at the top of the Default.aspx.vb page
I then load data to a grid using the next piece of code. My intention is to be able to sort the data by clicking
on the column header so I included the  GridView1_Sorting procedure shown further down at the bottom.
When I click on a header I get the following error in that procedure "DataTable must be set prior to using dataview."
Public Class Default
    Private objDV As New DataView
    Private dt As New DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Open in new window

    Sub Bind_Grid_to_Dataview(ByVal oGridView As GridView, ByVal oSQL As String)


            Dim cs As String = ConfigurationManager.ConnectionStrings("SSQL").ConnectionString
            Dim objCon As New SqlConnection 'Declaring connection object
            objCon.ConnectionString = cs
            'opening connection
            If objCon.State = ConnectionState.Closed Then
            End If
            'declaring new Data Adapter
            Dim objDA As New SqlDataAdapter
            objDA.SelectCommand = New SqlCommand(oSQL, objCon)
            Dim objDS As New DataSet
            Call oCheck_DataSet_For_Blank_Columns(objDS)
            'Below i am creating a default view of the Data Set
            objDV = objDS.Tables(0).DefaultView
            oGridView.DataSource = objDV

        Catch ex As Exception
            Response.Write(ex.Message & " dr2")
        End Try
    End Sub

Open in new window

   Private Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting

            objDV.Sort = e.SortExpression
            GridView1.DataSource = objDV
        Catch ex As Exception
            Response.Write(ex.Message & " Gridview Sorting Error 4")
        End Try

    End Sub

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Obadiah ChristopherDeveloper User InterfaceCommented:
The datatable/dataview is not retained across postbacks. You will have to save the datatable in some cache like session/viewstate and then use it in Sorting event method.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. How would I do that?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.