Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Gridview session
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
      Get
 
          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)
      MyBase.OnLoad(e)
 

      If Not Me.IsPostBack Then
          Dim dt As DataTable
          If Me.DataTable Is Nothing Then
              dt = InlineAssignHelper(Me.DataTable, Me.LoadData())
          Else
              dt = Me.DataTable
          End If
          Me.GridView3.DataSource = dt
          Me.GridView3.DataBind()
          Me.txtSearch.Attributes.Add("onkeyup", String.Format("javascript:__doPostBack('{0}','')", Me.upnlGridView.ClientID))
      Else
 
          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)
                      dt.Rows.Remove(dr)
                  Next
                  GridView3.DataBind()
 
                  Dim rows As DataRow() = Me.DataTable.[Select](String.Format("Emp_Number LIKE '%{0}%'", Me.txtSearch.Text))
                  Me.GridView3.DataSource = Me.LoadData(rows)
                  Me.GridView3.DataBind()
 
              Else
                  Me.GridView3.DataSource = Me.DataTable
                  Me.GridView3.DataBind()
              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 3:01am
gani77871.3K
Comments
Walter Di Salvo at 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 at 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
good
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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use
Session.Clear()
to clear all the values stored in session.
 
but if you want to clear some specific key in session, then over-write the key
 
Session.Add("Mykeyname","")
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 Andreas Gieriet 100
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Oct 2012
Copyright © CodeProject, 1999-2014
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