Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8061919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:29:48+00:00 2026-06-05T10:29:48+00:00

I am attempting to sort a gridview in an updatepanel. It compiles, but I

  • 0

I am attempting to sort a gridview in an updatepanel. It compiles, but I get an Object reverence not set to an instance of an object error. I am trying to follow this guide, but in vb

default.aspx

 <ajx:UpdatePanel ID="ajaxpanel" runat="server">   
    <ContentTemplate>         
                <asp:GridView ID="gvProgramDetails" runat="server" AutoGenerateColumns="False" 
                    CssClass="gridview" DataKeyNames="ProgramNumber" 
             AllowPaging="True" PageSize="2" AllowSorting="True" OnSorting="gvProgramDetails_Sorting">
                    <Columns>
                        <asp:CommandField ControlStyle-CssClass="button delete" ShowDeleteButton="True">
                            <ControlStyle CssClass="button delete" />
                        </asp:CommandField>
                        <asp:CommandField ControlStyle-CssClass="button save" ShowEditButton="True">
                            <ControlStyle CssClass="button save" />
                        </asp:CommandField>
                        <asp:BoundField DataField="ProgramNumber" HeaderText="ProgramNumber" 
                            InsertVisible="False" ReadOnly="True" SortExpression="ProgramNumber" />
                        <asp:BoundField DataField="ProgramName" HeaderText="ProgramName" 
                            SortExpression="ProgramName" />
                        <asp:BoundField DataField="ProgramStatus" HeaderText="ProgramStatus" 
                            SortExpression="ProgramStatus" />
                        <asp:BoundField DataField="RecordType" HeaderText="RecordType" 
                            SortExpression="RecordType" />
                        <asp:BoundField DataField="ProgramInformation" HeaderText="ProgramInformation" 
                            SortExpression="ProgramInformation" />
                    </Columns>
                </asp:GridView>
                </ContentTemplate>

default.aspx.vb

Imports System.Web.Services
Partial Class processes_ProgramTrack_Default
Public Property SortOrder() As String
        Get
            If (ViewState("SortOrder").ToString = "desc") Then
                ViewState("SortOrder") = "asc;"
            Else
                ViewState("SortOrder") = "desc"
            End If
            Return ViewState("SortOrder").ToString()
        End Get
        Set(ByVal value As String)
            ViewState("SortOrder") = value
        End Set
    End Property
    Public Sub bindGridView(Optional ByVal sortExp As String = "", Optional ByVal sortDir As String = "")
        Dim connstr As String = ConfigurationManager.ConnectionStrings("WEBConnectionString").ConnectionString
        Dim conn As New SqlConnection(connstr)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        Dim myDataView As New DataView()
        Dim mysqlCommand As New SqlCommand("SELECT tblPrgTrackPrograms.ProgramNumber, tblPrgTrackPrograms.ProgramName, tblPrgTrackPrograms.ProgramStatus, tblPrgTrackProgramDocumentation.RecordType, tblPrgTrackProgramDocumentation.ProgramInformation FROM tblPrgTrackPrograms INNER JOIN tblPrgTrackProgramDocumentation ON tblPrgTrackPrograms.ProgramNumber = tblPrgTrackProgramDocumentation.ProgramNumber")
        Dim myDataSet As New DataSet()
        Dim mySQLAdapter As New SqlDataAdapter(mysqlCommand)
        mySQLAdapter.SelectCommand.Connection = conn

        mySQLAdapter.Fill(myDataSet)
        myDataView = myDataSet.Tables(0).DefaultView
        If sortExp <> String.Empty Then

            myDataView.Sort = String.Format("{0}{1}", sortExp, sortDir)
        End If
        gvProgramDetails.DataSource = myDataView
        gvProgramDetails.DataBind()
    End Sub

    Protected Sub gvProgramDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProgramDetails.Load
        bindGridView()
    End Sub
    Protected Sub gvProgramDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProgramDetails.PageIndexChanging
        gvProgramDetails.PageIndex = e.NewPageIndex
        bindGridView()
    End Sub

    Protected Sub gvProgramDetails_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProgramDetails.RowDeleting

    End Sub

    Protected Sub gvProgramDetails_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvProgramDetails.Sorting
        SortOrder = ViewState("SortOrder").ToString
        bindGridView(e.SortExpression, SortOrder)
    End Sub

End Class

I get the error message as a popup msgbox. What I think is happening, SortOrder isn’t getting assigned a value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:29:49+00:00Added an answer on June 5, 2026 at 10:29 am

    You need to make sure that you ViewState("SortOrder") has a value in it before you try to call ToString. Otherwise, it will throw that exception (as you’ve seen) because you cannot call the ToString method on null.

    Essentially, you need to wrap your references to that ViewState variable in a “If (ViewState("SortOrder") IsNot Nothing Then” block, and possibly provide special handling in the “Else” section (if you need that).

    Something like this for your property:

    Public Property SortOrder() As String
        Get
            If (ViewState("SortOrder") IsNot Nothing Then
                If (ViewState("SortOrder").ToString = "desc") Then
                    ViewState("SortOrder") = "asc;"
                Else
                    ViewState("SortOrder") = "desc"
                End If
            End If
            Return ViewState("SortOrder").ToString()
        End Get
        Set(ByVal value As String)
            ViewState("SortOrder") = value
        End Set
    End Property
    

    And then the same thing for your sorting event:

    Protected Sub gvProgramDetails_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvProgramDetails.Sorting
        If (ViewState("SortOrder") IsNot Nothing Then
            SortOrder = ViewState("SortOrder").ToString
            bindGridView(e.SortExpression, SortOrder)
        End If
    End Sub
    

    Note: This is my best guess at your problem, given the information you’ve provided so far. If you provide the line that’s throwing the error, I’d be glad to take another look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to do a simple bubble sort code to get familiar with list/string
I am attempting sort this array by each stdClass Object's [title] in an Ubercart
I'm attempting to sort a ListView using C#, but whenever I click the sort
I'm trying to sort an array of NSStrings but i'm unsure how to actually
I'm attempting to sort of highlight a tile object within a game I'm making
I am attempting to sort an ArrayList of TokenDoubleCounters (my custom object). In TokenDoubleCounter,
I have sort of a tricky problem I'm attempting to solve. First of all,
Attempting/struggling to get registration and sign-up working within an active admin project. I have
Not long ago I asked a question attempting to identify a certain unicode character
So I am pulling a list of links and I am attempting to sort

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.