Wednesday, March 21, 2012

Passing A Session Variable

Hi All,

Please can anyone help here.....I am trying to pass a simple Session Variable from page 1 to page two , but it ain working

Any help would be great

Page the information is passed from:

Sub Button2_Click(sender As Object, e As EventArgs)

session("ID") = 150
response.REDIRECT("./page2.aspx")
End Sub

SUB PAGE_LOAD(OBJ AS OBJECT, E AS EVENTARGS)

response.querystring(session("Name"))

RESPONSE.WRITE(session("Name"))

END SUB

any help would be most appreciated or any URLS you may have.

thanks

TonyYou can not use "response.querystring" to get session, do it like this:
<code>
Dim name As String = session("Name").ToString
<code>
Thanks Jimmy

I put this info into my 'receiving' page and it generated an error

sub page_load(byval sender as object, byval e as eventargs)

Dim name As String = session("UserID").ToString

if Len(name) = 0 then

response.redirect("./login10.aspx")

end if

end sub

error

Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

any help is really apprecaited,,,,,,,
Where did you create the session before the page_load?
yes it was before the page load but please see my latest post

amy help here is really apprecioated....I just want it to work so i can go to bed

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=723625
I test your code, it is working fine. May be you can do a debugging to test if the session is created like this:


strName = dslogin.tables("UserInfo").rows(0).item("ID")
Session.Item("UserID")=strNAme
response.write(Session.Item("UserID"))

Hi Jimmy

Thanks for your post and help....

I have done a response.write as test and it works fine and writes the variabvle with no problems....

I think the issue lies in the way it is passed...as you can see all i do is load the variable and then redirect the page...is this all I have to do......do I not need to specify the variable oris this passed automatically?

Also, is the code on the receiving page OK to receive the variable???

thanks for your help so far.....what lesson this will be.....

cheers

Please help more if you can ....I am really fustrated

cheers

Tony
Are these two pages in the same web application?
Hi Jimmy,

Yep they are both in the root.........

page 1 can indeed 'see' page 2 and it does open it up when we do the response.redirect...just does not pass the variable......do i need to pass the variable in some way...I feel all I am doing is storing it in a varaible in page 1 then activating page 2.....

Are session varaibles automatically passed???

and if so are they automatically received or do I have to assign them to a variable on the way in?

heres all my code

any help here is much appreciated.......cheers mate

rgs

Tony

page 1

dim un as string
dim pass as string
dim strName as string

sub Page_Load(byVal Sender as object, byval e as eventargs)

If session("NumTries") > Application("MaxTries") then
response.redirect ("./failed_login.aspx")

end if
end sub

Sub Button1_Click(Sender As Object, e As EventArgs)

un = textBox1.Text
pass = TextBox2.Text

dim DBConn as OleDbConnection
dim DBCommand as OleDBDataAdapter
dim DSlogin as New DataSet

DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& Server.MapPath("/fpdb/" _
& "clt.mdb;"))

DBCommand = New OleDbDataAdapter _
("Select ID From tblReg Where UserName = '" & un & "' And Password = '" & pass & "'", DBConn)

Dbcommand.fill (dslogin, _
"UserInfo")

strName = dslogin.tables("UserInfo").rows(0).item("ID")

Session.Item("UserID")=strNAme

'response.write("Hello" & Session.Item("UserID")) ' this is the test i did and it worked

response.redirect("./success10.aspx")

end sub

PAGE 2 ...code

sub page_load(byval sender as object, byval e as eventargs)

dim strNAme as string = Session.Item("UserID")

response.write("hello " & strNAme)

end sub
Here is a snip from a post I made in another thread...hopefully it help you:

<snip>
Understanding this problem requires a little background about how Sessions work, and more specifically how they work in relation to Response.Redirect()...

When a Session is first established, a cookie is placed on the client (this cookie contains a unique session identifier). This cookie is then passed to the server upon every subsequent page request; this is how the server knows which session belongs to which end-user.

Normally, all is well, and this process of validating the cookie goes on behind the scenes. When you perform a Response.Redirect, however, a special instruction is sent to the client browser, telling it to now request a different page than the one it was expecting to receive; this page is the URL that you specified in the .Redirect() call. On the server, after sending the redirect instruction, the Response process ends; and not very gracefully. It actually performs a Response.End, which stops execution of the current page (wherever it is) then results in a ThreadAbortException. This halting of execution causes the session cookie to essentially "get lost"; nice, eh?

What you can do to get around this "feature" is to use the overload version of Response.Redirect:
Response.Redirect("myPage.aspx", False)
The False parameter here says to NOT stop page execution, and in turn the Session cookie (and its unique identifier) is retained.
</snip
HTH...

Chris
In your page Load there is no check for postback. Maybe thats the problem:


sub Page_Load(byVal Sender as object, byval e as eventargs)

if(!IsPage.IsPostBack) {
If session("NumTries") > Application("MaxTries") then
response.redirect ("./failed_login.aspx")
}
end if
end sub


GOT IT!!!!!!!!!!!!!!!

Tried the the application on my second machine and it worked no problem and passed the variable fine!!!!!!

Uploaded to the web and it worked fine their also!!!!!!!!!

Turned OFF Zonealram on my original development machine....and it worked fine.....!!!!

Hope anyone else can get a lesson from this also...

Many thanks to all who contributed...I have learned alot here...

Many thanks to you all......Anyone know why this happens?

regards

Tony

0 comments:

Post a Comment