Click here to Skip to main content
13,455,913 members (47,551 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How to clear session values from gridview.

I am getting problem in grid values in session thorugh datatable.

whenever i am filtering i am getting the old valus.

because, the values are maintaing in the session.

I want whenever click the searchig, it should fetch the the fresh session grid values.

How to do this...

see my code
Private Property DataTable() As DataTable

          Return DirectCast(Session("DataTable"), DataTable)
      End Get
      Set(ByVal value As DataTable)

          Session("DataTable") = value
      End Set
  End Property
  Protected Overrides Sub OnLoad(ByVal e As EventArgs)

      If Not Me.IsPostBack Then
          Dim dt As DataTable
          If Me.DataTable Is Nothing Then
              dt = InlineAssignHelper(Me.DataTable, Me.LoadData())
              dt = Me.DataTable
          End If
          Me.GridView3.DataSource = dt
          Me.txtSearch.Attributes.Add("onkeyup", String.Format("javascript:__doPostBack('{0}','')", Me.upnlGridView.ClientID))

          Dim target As String = Me.Request.Form("__EVENTTARGET")
          If Not String.IsNullOrEmpty(target) AndAlso target.Equals(Me.upnlGridView.ClientID) Then
              If Not String.IsNullOrEmpty(Me.txtSearch.Text) Then
                  Dim dt As DataTable = Me.GetTable()
                  For i As Integer = 0 To dt.Rows.Count - 1
                      dt = DirectCast(Session("DataTable"), DataTable)
                      Dim dr As DataRow = dt.Rows(i)

                  Dim rows As DataRow() = Me.DataTable.[Select](String.Format("Emp_Number LIKE '%{0}%'", Me.txtSearch.Text))
                  Me.GridView3.DataSource = Me.LoadData(rows)

                  Me.GridView3.DataSource = Me.DataTable
              End If
          End If
      End If
  End Sub
  Public Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
      target = value
      Return value
  End Function
  Protected Sub PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
  End Sub
  Protected Sub Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
  End Sub
  Private Function LoadData() As DataTable
      Return Me.LoadData(Nothing)
  End Function
Posted 24-Oct-12 2:01am
Walter Di Salvo 24-Oct-12 9:33am
Sorry but I don't understand how you update the Session["DataTable"]. Maybe you have missed some code. It is obvious that if you don't update Session["DataTable"] the application cannot fetch the fresh session grid values.
Sandeep Mewara 24-Oct-12 11:58am
First and foremost, never use variable names that are keywords in the language. I see you do almost all the time possible. :doh:
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you check your code, you'll see that you only set the DataTable property(which sets the session value) in the !IsPostback portion of the if statement. If you want new data, then you need to update DataTable with the new value in the Else block as well once you are done working with it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


to clear all the values stored in session.

but if you want to clear some specific key in session, then over-write the key


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01-2016 | 2.8.180322.1 | Last Updated 24 Oct 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100