Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

i have created module in vb.net which combine with the main application using asp classic...once i login and session variable just show in the default page but once i add or edit or back to first page the session was blank.
need advice and suggestion.

here my piece of code:

on page load
VB
UserID = Request.QueryString("userid")
       BranchID = Request.QueryString("branchid")
       Session("userid") = UserID
       Session("branchid") = BranchID
       Response.write(Session("userid") & "," & Session("branchid"))


on button event(for add new record)
Response.Redirect("WebForm1.aspx?userid=" & UserID & "&branchid=" & BranchID & "")

Thanks
Allesya
Posted

Hi,
When you click on your button, at first your page_load is called. but there is no correct querystring on postback! so you refill your sessions with empty!
change your code (page load) to this: ( I write c#, please change it to vb!)
if (!IsPostback)
{
   //your old code 
}
else
{
UserID=Session["userid"];
BranchID=Session["branchid"];
}


hope this can help.
 
Share this answer
 
Comments
Reena Aleesya 13-Jun-11 21:43pm    
hi aidin..thanks for suggestion..
i changed like your code..the session still blank..

If Not IsPostBack Then

'1. Create a connection

'2. Create the command object, passing in the SQL string

'3. Create the DataReader

Else
'Get the values of UserID and BranchID
UserID = Request.QueryString("userid")
BranchID = Request.QueryString("branchid")
Session("userid") = UserID
Session("branchid") = BranchID
Response.Write(Session("userid") & "," & Session("branchid"))
End If

rdgs
Aleesya
aidin Tajadod 14-Jun-11 17:47pm    
Hi Aleesya, you need to Initialize sessions first.
If Not IsPostBack Then
Session("userid")=Request.QueryString("UserID)
...
Else
UserID=Session("userid");

you did exactly viceversa!
hope this can help.
You should do it like this

if(!Page.IsPostBack)
{
  if(Request.QueryString["userid"] == null || Request.QueryString["userid"] == "")
  {
     UserID=Session["userid"].ToString();
     if(Request.QueryString["userid"] == null || Request.QueryString["userid"] == "")
     {
         BranchID=Session["branchid"].ToString();
     }
  }
}


Above is the c# code, use the same logic in VB,net with correct syntax....

What's the problem : Whenever you login the page is first loaded, sets up the querystring value to the session.

But whenever a postback occur your codes assign querystring value to session which is empty now, fill ups the session again with the empty string.

So in above code I have checked and sets the session only once and next time a postback or full pageload occurs, if their is nothing in querystring it simply ignores the assignment.


Hope it helps ........ :)
 
Share this answer
 
Comments
Reena Aleesya 14-Jun-11 3:29am    
hi rahul...thanks for your code..
once im replace into your code i got this error: object expected.
Im not sure where should i do.
Need your advice.
Thanks
Aleesya
aidin Tajadod 14-Jun-11 17:51pm    
That code should be like this:
if (!Page.IsPostBack)
{
if (Request.QueryString["userid"]!=null && Request.QueryString["userid"]!="")
...
}
Reena Aleesya 15-Jun-11 2:47am    
hi aidin..
i already retrieve the session variable...
but now i have another problem...once i'm click the menu or tab..the session will show blank...
need favor..
Thanks
Aleesya
aidin Tajadod 15-Jun-11 13:33pm    
What kinds of menu/tab are you using?! did you correct your code at your page_load?! I can not tell you what is wrong unless you put your code here.
Reena Aleesya 15-Jun-11 19:46pm    
hi aidin..
im modified your given code..
and finally i can display the session variable..
thanks buddy..next time i hope you can give me some advice..
rgds
Aleesya...
If Request.QueryString("userid") = "" Or Request.QueryString("userid") Is Nothing Then
Session("userid") = Session("userid")
ElseIf Request.QueryString("branchid") = "" Or Request.QueryString("branchid") Is Nothing Then
Session("branchid") = Session("branchid")
Else
UserID = Request.QueryString("userid")
Session("userid") = UserID

BranchID = Request.QueryString("branchid")
Session("branchid") = BranchID
End If
Response.Write(Session("userid") & "," & Session("branchid"))
 
Share this answer
 
Comments
aidin Tajadod 16-Jun-11 10:33am    
Why are you doing the two first If!! I mean Session("userid")=Session("userid") !?
What page is your first page? and WebForm1.aspx and first page is the same or not?
I don't understand your workflow.
but you can refer following code:

'
' Session("userid") and Session("branchid")hove to Initialize in Global.asax file
If Not IsPostBack Then 
   UserID = Request.QueryString("userid")
   BranchID = Request.QueryString("branchid")
   Session("userid") = UserID
   Session("branchid") = BranchID
Else
   ' your code
   ' maybe   UserID = Session("userid") and BranchID = Session("branchid") 
End If


If your Sessions is blank, you should recheck userid and branchid at address bar, their value can blank
good lucky!
 
Share this answer
 
v2

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