Answered by:
saving a class in a session variable

Question
-
User297437924 posted
The following code worked with a asp.net website (not web app) that I created years ago. I should say that the idea is to create a pointer to a new class, save the pointer in a session variable, and then in other pages or places to use the session variable to get at the class by typecasting it back to a variable that represents the class.
This code does not work when compiled as a web-app. I have the class in the app_code folder, which probably means it is compiled into a dll. Anyway, I can't cast the session variable back to a variable (see line with asterixes) . Any help is appreciated. Here is the code (only a few lines)
Dim cde As ClassDatabaseEchoIf Not Page.IsPostBack Then
If Session("ClassDatabaseEcho") Is Nothing Then
Session("ClassDatabaseEcho") = New ClassDatabaseEcho("some value")
Else
cde = Session("ClassDatabaseEcho") ' ***** does not work, may expect namespace? Or can't work as webapp?
End If
End IfFriday, February 19, 2021 8:02 PM
Answers
-
User475983607 posted
You did not provide the error message, class, or enough code to figure out where the bug is in your code/design.
Working Example
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="WebFormsVBDemo._default5" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Public Class ClassDatabaseEcho Public Property Message As String Public Sub New(ByVal Message As String) Me.Message = Message End Sub End Class Public Class _default5 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then If Session("ClassDatabaseEcho") Is Nothing Then Session("ClassDatabaseEcho") = GetClassDatabaseEcho() End If End If Label1.Text = CType(Session("ClassDatabaseEcho"), ClassDatabaseEcho).Message End Sub Protected Function GetClassDatabaseEcho() As ClassDatabaseEcho Return New ClassDatabaseEcho("some value") End Function End Class
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 19, 2021 8:36 PM
All replies
-
User475983607 posted
You did not provide the error message, class, or enough code to figure out where the bug is in your code/design.
Working Example
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="WebFormsVBDemo._default5" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Public Class ClassDatabaseEcho Public Property Message As String Public Sub New(ByVal Message As String) Me.Message = Message End Sub End Class Public Class _default5 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then If Session("ClassDatabaseEcho") Is Nothing Then Session("ClassDatabaseEcho") = GetClassDatabaseEcho() End If End If Label1.Text = CType(Session("ClassDatabaseEcho"), ClassDatabaseEcho).Message End Sub Protected Function GetClassDatabaseEcho() As ClassDatabaseEcho Return New ClassDatabaseEcho("some value") End Function End Class
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 19, 2021 8:36 PM -
User297437924 posted
I looked closer, and the error happens in a different form, but this is the error message:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'ClassDatabaseEcho' to type 'EchoRedo4.ClassDatabaseEcho'.
Source=EchoRedo4The odd thing is that this error does NOT happen in the home page, which has similar code.
I'll keep you posted on what I find.
Saturday, February 20, 2021 6:13 PM -
User297437924 posted
I looked at the "inherits" attribute on the page that gave the error (see my other reply) and it looked like this:
<%@ Page Title="" Language="VB" AutoEventWireup="false" inherits="EchoRedo4.FormTellUserLogIn"
So I did a global replace throughout my project, getting rid of the "EchoRedo4. prefix in all the aspx pages.
I tried running the program again, and then the pages would not load, but that turns out to be another problem (they have to have 'codefile', not 'codebehind' attribute.
Anyway, I think I'm out of the woods, but if not, I'll post again.
Saturday, February 20, 2021 6:30 PM