Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to share session variable between asp and asp.net 2

Status
Not open for further replies.

dembyg

Programmer
Oct 20, 2001
58
0
0
US
I have an asp form that has sessions for first and last name
code reads.

asp
Session("sesFirstName") = Dembyg
Session("sesLastName") = Dembyg


I would like to use within my asp.net page. how is this done.


Thanks for helping
 
I need alittle more assistance.... I am using a header in my asp.net project by using a site.master page. I want the user name to appear with the querystring I am able to display the user name however when I go to the next page it seems to refresh field and make it NULL.. I want the user name to appear throughout the application...
 
Once you pass the variables you want to your asp.net app, then store them in session.
 
I stored the variable in a session var called session("curUser") however going to the next page i loose the value in the session.
 
If you say it is not there then there must be something wrong with the way you are trying to store the value.
What is the code you use to set and get the values from the session object?
 
jben did the right question.. in the last post.

dembyg, If you are doing session("curUser") = something then you are wrong. Do this first: session.add("curUser",something). Before calling the .add, the variable name in the session do not exist. Instead it will be a nothing, returning nothing (null in c#). So, session vars are alive in the whole site. Create once the variable anywhere with a default value (whatever). Then doing the marked as bold you can change the value.

Hope this helps.
 
Thank you for you response... I will try out what youve suggested.
 
TipGiver said:
If you are doing session("curUser") = something then you are wrong
I disagree. Whilst you should use the .Add method of the session to add a variable, the shorthand way of assigning it will still work. Try it out by adding a Button on one page, and assigning the variable e.g.
Code:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("Test1") = "Hello"
        Response.Redirect("Page2.aspx")
    End Sub
Then, on the next page write that variable out e.g.
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(Session("Test1"))
    End Sub
It should still work.

I suspect dembyg's problem is something else, possibly to do with how the sessions are stored.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
ok this is what ive done thous far after logging into the application there is a home page (homepage.asp) that page contain hyperlinks. One of the hyperlinks have the following code:
application/Menu.aspx?ID=<%=UserID%>. UserID is a session var within the asp page. I then navigate to Menu.aspx which has master.site header. In the header i have i field that should display the current logged in user.. I have code in the page load that reads:
Dim x As String = Page.Request.QueryString("ID")
Session("sesNewID") = x

however when i go to the addnew.aspx page which also has the mastersite header the session("sesNewID") is Null.
 
I changed the session("sesNewID") = x
to Session.Add("sesNewID", x) and i go to the next page and for some reason the header reloads with null value.
 
As I said above, I don't think the way you are assigning the value is the problem, I think it is something else, possibly to do with how the sessions are stored.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Hey guys thanks for your help... I figured it out I added to code to the global.asax under the Session_Start
 
In theory, that shouldn't be the problem. No matter where you add your session variable it should still exists from page to page. I think you are probably masking the real problem or it has been fixed via other settings.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Ok... well do you have any suggestions on how i may find the real problem... I need this application working in a couple of days.. I am expected to demo what i have done so far.
 
Sorry Im confused.. Im storing my session in a Master page.. This master page is used as a header page throughout the application... It displays back,home,main menu and the logged in user name... this name is coming from an asp page which i am using a querystring to capture the information..
 
in the web.config I am using the default...
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top