Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Really weird error I'm having with using Session to store class (asp.net)

Posted on 2012-09-13
4
Medium Priority
?
537 Views
Last Modified: 2012-09-17
Dear Experts,

I'm pretty new to this asp.net thing and am actually working on making my first website. I've put a datagrid on a page and have given it the ability to do paging and sorting. However, at randome, sometimes when I've reloaded the page several times, I'll either be sorting a column and then another column or clicking a page link or another page link and this really bizarre error that I don't understand comes up saying that type "(A)dgSessionSortInfo cannot be cast to (B)dgSessionSortInfo."
Now, I know this is happening when it reloads the page and I even know the mechanism. It is because I have two functions which deal with session. In one, when it binds the data to the datagrid, it takes an object (simple class I wrote that just contains a few properties) and saves it to the session. See function PerformSearch() below.

Then, I have another two functions, dgResults_PageIndexChanging & dgResults_Sorting. In both of these, at seeming random, when I try to read the value of the class I saved in the session last time the page was loaded it will occasionally throw this error, which I just don't understand.

Here's one of the functions that contains the code that throws the error.. it's the first line where it errs, right where it reads the Session("DGSessionSortInfo") back into the dgSessInfo class. See function dgResults_PageIndexChanging() below.

And finally, here's the error. It's probably a fairly easy thing to solve, but since I'm new to all the web stuff, and maybe don't really have a full grasp on sessions and what can be done with them and the way web pages work between page loads.


Thanks! ~Jeffrey
Private Sub PerformSearch(ByVal sSearchValue As String, ByVal searchType As msSearchTypes, ByVal sSortExpression As String, ByVal SortDir As dgSortOptions)
        'Performs a search based on any criteria
        Dim iResultsCount As Integer

        'Save this in our Session so that when the page reloads or user selects a column we can update our search
        Dim dgSessInfo As New dgSessionSortInfo(sSearchValue, searchType, sSortExpression, SortDir)
        Session("DGSessionSortInfo") = dgSessInfo

        'Set the data to the grid
        Select Case searchType
            Case msSearchTypes.Address
                iResultsCount = PerformSearch_Address(sSearchValue, sSortExpression, SortDir)
            Case msSearchTypes.Parcel
                iResultsCount = PerformSearch_Parcel(sSearchValue, sSortExpression, SortDir)
            Case msSearchTypes.TrusteeSaleNum
                iResultsCount = PerformSearch_TrusteeSaleNum(sSearchValue, sSortExpression, SortDir)
            Case msSearchTypes.ZipCode
                iResultsCount = PerformSearch_Zip(sSearchValue, sSortExpression, SortDir)
        End Select

        'Show/Hide the divs depending on whether or not we got results
        Me.divDgResults.Visible = (iResultsCount > 0)
        Me.divNoResults.Visible = (iResultsCount = 0)
    End Sub

    Protected Sub dgResults_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgResults.PageIndexChanging
        'Rebind the datagrid
        Dim dgSessInfo As dgSessionSortInfo = Session("DGSessionSortInfo")
        PerformSearch(dgSessInfo.SearchValue, dgSessInfo.SearchType, dgSessInfo.SortExpression, dgSessInfo.SortDir)
        dgResults.PageIndex = e.NewPageIndex
        dgResults.DataBind()
    End Sub

(A)dgSessionSortInfo cannot be cast to (B)dgSessionSortInfo. Type A originates from 'App_Web_d3menczg, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_d3menczg.dll'. Type B originates from 'App_Web_24fd2ygl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_24fd2ygl.dll'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: (A)dgSessionSortInfo cannot be cast to (B)dgSessionSortInfo. Type A originates from 'App_Web_d3menczg, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_d3menczg.dll'. Type B originates from 'App_Web_24fd2ygl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_24fd2ygl.dll'.

Source Error: 
Line 129:    Protected Sub dgResults_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgResults.PageIndexChanging
Line 130:        'Rebind the datagrid
Line 131:        Dim dgSessInfo As dgSessionSortInfo = Session("DGSessionSortInfo")
Line 132:        PerformSearch(dgSessInfo.SearchValue, dgSessInfo.SearchType, dgSessInfo.SortExpression, dgSessInfo.SortDir)
Line 133:        dgResults.PageIndex = e.NewPageIndex
 
Source File: 
.. \Search.aspx.vb    Line: 131 
Stack Trace: 
[InvalidCastException: (A)dgSessionSortInfo cannot be cast to (B)dgSessionSortInfo. Type A originates from 'App_Web_d3menczg, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_d3menczg.dll'. Type B originates from 'App_Web_24fd2ygl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_24fd2ygl.dll'.]
   Members_Search.dgResults_PageIndexChanging(Object sender, GridViewPageEventArgs e) in C:\HostingSpaces\accurate\accurateforeclosures.com\wwwroot\Members\Search.aspx.vb:131
   System.Web.UI.WebControls.GridView.HandlePage(Int32 newPage) +81
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1023
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707 

Open in new window

0
Comment
Question by:JeffreyDurham
  • 3
4 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38396132
Typecast Session("DGSessionSortInfo") to object of type dgSessionSortInfo before assigning it

Dim dgSessInfo As dgSessionSortInfo = DirectCast(Session("DGSessionSortInfo"), dgSessionSortInfo)
0
 

Author Comment

by:JeffreyDurham
ID: 38396805
informaniac:

Ok, I tried that, sounded like a good idea to me, still coming up with an error though.

[A]dgSessionSortInfo cannot be cast to [B]dgSessionSortInfo. Type A originates from 'App_Web_m3bmhmky, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_m3bmhmky.dll'. Type B originates from 'App_Web_m3hyufai, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_m3hyufai.dll'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: [A]dgSessionSortInfo cannot be cast to [B]dgSessionSortInfo. Type A originates from 'App_Web_m3bmhmky, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_m3bmhmky.dll'. Type B originates from 'App_Web_m3hyufai, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9873ac5\9ff4dbe7\App_Web_m3hyufai.dll'.

Source Error: 


Line 141:        'Make sure you always do a DirectCast error when pulling in classes from sessions so that you don't get errors saying
Line 142:        '  that type [A]dgSessionInfo isn't like type [B]dgSessionInfo ~Michael 9/13/2012
Line 143:        Dim dgSessInfo As dgSessionSortInfo = DirectCast(Session("DGSessionSortInfo"), dgSessionSortInfo)
Line 144:
Line 145:        'If it's the same column we should flip the sort direction, otherwise start ascending
 

Open in new window

0
 

Author Comment

by:JeffreyDurham
ID: 38396821
Not sure if it'd help, but this is the class I'm trying to store in the session..


    Private Enum msSearchTypes
        Address
        Parcel
        TrusteeSaleNum
        ZipCode
    End Enum

    Private Enum dgSortOptions
        Ascending
        Descending
    End Enum

    'Very simple class that just holds the user's sort preferences for the datagrid and the information needed to requery it
    'I've decided not to store the datagrid in the Session, I've noticed it tends to bloat browsers (especially IE) and crash them.. ~Jeffrey 9/11/2012
    Private Class dgSessionSortInfo
        Private m_SearchValue As String
        Private m_searchType As msSearchTypes
        Private m_SortExpression As String
        Private m_SortDir As dgSortOptions

        Public Sub New(ByVal searchValue As String, ByVal searchType As msSearchTypes, ByVal sSortExpression As String, ByVal SortDir As dgSortOptions)
            m_SearchValue = searchValue
            m_searchType = searchType
            m_SortExpression = sSortExpression
            m_SortDir = SortDir
        End Sub

        Public ReadOnly Property SearchValue As String
            Get
                Return m_SearchValue
            End Get
        End Property
        Public ReadOnly Property SearchType As msSearchTypes
            Get
                Return m_searchType
            End Get
        End Property
        Public ReadOnly Property SortExpression As String
            Get
                Return m_SortExpression
            End Get
        End Property
        Public Property SortDir As dgSortOptions
            Get
                Return m_SortDir
            End Get
            Set(ByVal value As dgSortOptions)
                m_SortDir = value
            End Set
        End Property
    End Class
0
 

Author Closing Comment

by:JeffreyDurham
ID: 38406488
Turns out the class was just too complicated an object to save to a session or something. Not sure if it's because it was unserializable or what, but when I wrote out a function that just records each of the properties on the class that error stopped happening. So I'm glad.

Thanks! ~Jeffrey
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question