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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session issues 1

Status
Not open for further replies.

gagz

Programmer
Nov 21, 2002
333
US
Hi-

I'm trying to simply set/get session variables with no luck.

I'm attempting to set them in global.asax:
Sub Session_Start(Sender As Object, E As EventArgs)
Session("UserName") = "gagz"
End Sub

and trying to access from an aspx file a couple ways:
in aspx as:
<title>Welcome <%=Session(&quot;UserName&quot;)%>

and in aspx.vb file:
txtbox.Text = Session(&quot;UserName&quot;)

Neither way is working... am I missing something obvious?

Thanks for the help.
 
I don't think you can set a session variable within the global asax file...

try setting it a different way... i alwas use a splash screen (a welcome screen you can say) and when the people hit ok, then in the code behind that ok button set your session...

just an idea.
--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
well thats stinks... I want it to be someplace generic, because some of the values will be coming from a cookie. Also, the user could enter from any page on the site... There my be a generic place to do it...

Could I maybe do it in a header file and do a simple check to see if a session is started when each page loads?
 
This worked for me. ASP.NET does not like render tags. Instead use response.write(). Make sure you use the parentheses. I don't know why your codebehind wasn't working, since I didn't see all the code.

global.asax
------------------------
<script language=&quot;VB&quot; runat=&quot;server&quot;>
Sub Session_Start(ByVal Sender As Object, ByVal E As EventArgs)
Session(&quot;UserName&quot;)=&quot;ASDF&quot;
End Sub
</script>

test.aspx
---------------------------
<%@ Page Language=&quot;VB&quot; %>
<html>
<body>
<%
response.write(Session(&quot;UserName&quot;))
%>
</body>
</html>
 
see, I show how little i know more often then not...:)

Good job warche!

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
you're right... response.write does work... but I still can't assign the variable to a text box in my code behind:
txtName.Text = Session(&quot;UserName&quot;)

any ideas?
 
Just wondering if anyone ever had a chance to see if they could read the session variable in code behind and assign it to a text box as above?
 
oh nevermind... i think i got it.
 
I use the session feature with a custom session object that holds all the user permissions, including user name. Once I access the custom object I could place the data anywhere I want to, even in textboxes. works great for me.

this saves a session object; it is run when user logs in:
Dim authRes As Boolean = authObj.authenticate(txtUser.Text, txtPass.Text)
If (authRes) Then
'set session object
Session(&quot;curSessionRights&quot;) = authObj.session_obj

this accesses the session object; is in page_init:
cur_session.session_obj = Session(&quot;curSessionRights&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top