Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
How to use public variable in Asp.NET with vb, in my web application i have class fils,aspx and asmx file, in this all pages i need access one public variable which i assign when user login, if i use session i cant access in asmx and class files, if i declare public variable in class file once page refresh it clear the assign values and became empty.

i want assign public variable with values and access in whole application until user logout, and once assign values when login it never change.

This is for connection sting , i know can declare in web conig, but we create each sql login for each and every user, so when user login we pass username to sql User ID

What I have tried:

For now i declare static variable and used in same page itself.If asp page no problem, but from class and asmx i need to each function connection strings.

Regards,
Aravind
Posted
Updated 4-Feb-19 8:36am

Here's the problem: unless you use the Session or Cookies (or a query string if you are desperate) you can't store more than one UserId per app: and the app is shared among users on the same web server, so using a single public variable (which would have to be Shared if you want to access it without the page class instance) is not going to work as you need multiple ones.

The best way to handle this is to use the session in your page, and pass the UserID from that to subsequent code. You probably shouldn't be using session specific data in your asmx page anyway, it's not a sign of good app design as they are intended for Web Services, not direct user interaction!
 
Share this answer
 
It's quite possible to use session state from an .asmx service:
C#
[WebMethod(EnableSession = true)]
public string checkSession()
{
    return HttpContext.Current.Session.SessionID;
}

As for class files, you should add the session value as a parameter to any methods that need it, and pass it in from your page / service.
 
Share this answer
 
Comments
Maciej Los 4-Feb-19 14:57pm    
5ed!

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