Friday, March 16, 2012

Passing A Variable ~ Headache needs Cure!!

Hi All,

I have been trying to pass a variable all day and I am very nealry there...I have been through three books and obviously this prob is too easy for books

passing a variable form page 1 to 2

page one code

This a snippet but it all works and I have tested I get my variable by doing a response.write

<code/
strName = dslogin.tables("UserInfo").rows(0).item("ID")
Session.Item("UserID")=strNAme
response.redirect("./success10.aspx")

</code
Page 2 Code

<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

</code
I get the referring page (/success10.aspx) but the variable does not get written in the response.write test

Any help would be greatly apprecitated.......I can then go to bed....:)

rgs

TonyJust a suggestion, but why don't you try using a Cookie or a QueryString instead?
Hi

Thanks for the post.......

I dont want to use a cookie as not everyone has them turned on......and I have never used session variables so its good learning....

cheers

Tony
Try this...


First Page.

Session("UserID")=strNAme

Second Page.

dim strNAme as string = Session("UserID")


Session("blah") = "Hello There!"
Response.Write(Session("blah"))

Try debugging the string variable and make sure that it contains a value.

Edit: And as another poster said, maybe you should be looking into querystrings since you're trying to "pass" variables to another page.
Basically, you are losing your session as soon as you redirect. Redirect() actaully executes a Response.End as soon as the header is sent, thus losing your session (which is stored in a cookie on the client). Try using the following overload method of Redirect():

Response.Redirect("~/SomePage.aspx", false);

The second parameter tells the method whether or not to execute a call to Response.End(). By default, if not specified, it is set to TRUE, and wa la.. you lose your session. If set to FALSE, a call to Response.End isn't called and you should be OK. :-)

Another alternative is to use Server.Transfer(). However, the URL will stay the same in the client's browser, as opposed to Response.Redirect().

Here is a decent explanation:
http://blogs.msdn.com/bleroy/archive/2004/08/03/207486.aspx

Hopefully this will point you in the right direction.

Let me know how it turns out.

0 comments:

Post a Comment