Click here to Skip to main content
15,942,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to set and retrieve session variables. These are actually a memorystream and stream writer.

I set them in global.aspx thus -

VB.NET
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim memoryStream As New System.IO.MemoryStream()
    Dim textWriter As System.IO.TextWriter = New System.IO.StreamWriter(memoryStream)
    Dim bytesInStream As Byte()
End Sub


this seems to work. No errors anyway.

What I have tried:

In an in a function in an aspx.vb page I call on those session variables -

Session("textWriter").WriteLine("test")

No errors on preview, but when I run it, it throws the exception - "Object variable or With block variable not set."

It's not within a loop or with block, so I guess it's not seeing the Session Variable ???? But only when you run the code? As I said, it's not showing up as an error in the code itself.

Really stuck, any ideas? Or for that matter, how I could set these objects once even on another .aspx page_load sub and be able to see them in a function in a different .aspx page?

I can't set them in the page that the function resides because the aspx.vb page loads many times when client AJAX calls it and it has to keep appending to the memorystream.
Posted
Updated 3-Aug-16 10:28am
Comments
jkirkerx 3-Aug-16 15:56pm    
WriteLine? WriteLine to where? to write a line on the webpage, the text has to be in the response output of HTML like Response.Write("Text")

What are you trying to store, like an image?
Member 12637386 3-Aug-16 16:00pm    
The writeline writes text to a memory stream that I will later download as a text file. See the code above in the application_start block (which should actually be session_start)

Dim memoryStream As New System.IO.MemoryStream()
Dim textWriter As System.IO.TextWriter = New System.IO.StreamWriter(memoryStream)
jkirkerx 3-Aug-16 16:14pm    
Yeah but a session is an object that the web server controls, and is just a string or primitive object. Asp.net provides access to the session on the web server called Session.

You may be able to save a base64 string of bytes in the session. Make a byte array and convert it to a base64 string and vice versus.

You have created three local variables in the Application_Start method, but you haven't stored them anywhere. You certainly haven't added them to the session.

To store the variable in the session, you need to add it to the session. To do that, you would need to use the Session_OnStart event:
C#
Sub Session_OnStart(ByVal sender As Object, ByVal e As EventArgs)
    Dim memoryStream As New System.IO.MemoryStream()
    HttpContext.Current.Session.Add("memoryStream", memoryStream)
    
    Dim textWriter As New System.IO.StreamWriter(memoryStream)
    HttpContext.Current.Session.Add("textWriter", textWriter)
End Sub

You can't use Application_Start, because that only fires once when your application starts up. The Session_OnStart event[^] fires when a new session is created, which will happen multiple times whilst your application is running, as different users connect to it.

ASP.NET Application Life Cycle Overview[^]
 
Share this answer
 
v2
Comments
Member 12637386 3-Aug-16 16:24pm    
Richard,

I added -

Sub Session_OnStart(ByVal sender As Object, ByVal e As EventArgs)
Dim memoryStream As New System.IO.MemoryStream()
HttpContext.Current.Session.Add("memoryStream", memoryStream)

Dim textWriter As New System.IO.TextWriter(memoryStream)
HttpContext.Current.Session.Add("textWriter", textWriter)
End Sub

New System.IO.TextWriter(memoryStream) shows an error - 'New cannot be declared on a class that is declared MustInherit

It appears that Session("memoryStream") is available across the session though. Thanks
Richard Deeming 3-Aug-16 16:26pm    
Sorry, that should be StreamWriter, not TextWriter. I've updated the answer.
Member 12637386 3-Aug-16 16:41pm    
Ahh. Got it. thanks again
Member 12637386 3-Aug-16 16:33pm    
And if I remove the 'New' the error is now at memoryStream in the line -
Dim textWriter As System.IO.TextWriter(memoryStream)

Error reads - Array bounds cannot appear in type separators.
Member 12637386 3-Aug-16 16:39pm    
I got it. I changed it to -
Dim textWriter As System.IO.TextWriter = New System.IO.StreamWriter(memoryStream)

Going to accept your answer Richard. it helped a lot. Thanks
In Global.asax, I know you can set a property, like the one when you right click the project, and then click on the resource tab, in which you can set a string resource. But it's not a Session object. It's a reflection Object.

I also know that you can write to the resource or create new ones as well, but my VS2015 is malfunctioning at the moment, and I can't show an example of it.


protected void Application_Start()
{
     AreaRegistration.RegisterAllAreas();
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);                     
}

public static string GetWebsiteName()
{
    string pValue = Properties.Settings.Default.WebsiteName;
    return pValue;
}


To access the string, MvcApplication is the class of the Project, not sure what yours will be.
string websiteName = MvcApplication.GetWebsiteName();
 
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