Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
------------
VB
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
Comments
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:

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.
 
Share this answer
 
v2
use
VB
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

VB
Session.Add("Mykeyname","")
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900