Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i created ASP.NET web Application and when users are login to the System what i did is i stored users Login name in a Static variable and Showed name in the theme page.But when multiusers used the same Application its get to show different names as Login Name. i guess static variable was changed to Last one who Login to the System. E:x if Jhon 1st login to system is shown as Jhon and then Watson logged to the system. Then when Jhon go to another page it is shown as 'Watson'. This is really Crutial issue that i faced in this application. i thought that static variables are get different asp.server objects for each and every user.but its seems to be wrong. so does any one recommend a method for this issue.\

Code in Login page :

if (Page.IsValid)

               UserValidation UserV = new UserValidation();
               int a = UserV.check_Login(UNtxt.Text, PWtxt.Text, List_company_cmbo.Text);
               if (a == 1)
                   //Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Login is ok')</SCRIPT>");
                   //Response.Write("<a href='WebForm1.aspx'></a> ");
                   //  WebForm1 wb = new WebForm1();
                   Session["logged"] = "logged";


               else if (a == 3)

                   // warning_txt.Text = "";
                   // warning_txt.Visible = true;
                   System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Action Will be cancelled. Please contact the system administrator')</SCRIPT>");


                   // Response.Redirect("index.aspx");
                   warning_txt.Visible = true;




Code in CS Class :

public static string F_name;
       public static string Comp_Code;
       public static string Comp_sqnumber;
       static SqlCommand cmd;

       public int check_Login(string userName, string Password, string c_id)
           DatabaseTrans dTrans = new DatabaseTrans();
           string page = "UserValidation.cs";
           string d = userName;
           string pwr = Password.Trim();
           string sql;
           int a = 0;
           // string Nname;

           string cipherPW = CryptorEngine.Encrypt(pwr, true);


               sql = "Select * from tblUser where UserID = '" + userName + "' and userPassword = '" + cipherPW + "' and compID ='" + c_id + "' ";
               cmd = new SqlCommand(sql, DatabaseTrans.getConn());
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();


               if (dt.Rows.Count == 1)
                   a = 1;
                   F_name = dt.Rows[0][3].ToString();
                   Comp_Code = dt.Rows[0][1].ToString();
                   Comp_sqnumber = dt.Rows[0][2].ToString();
                   a = 0;

              // DatabaseTrans.closeCon();
               return a;

           catch (Exception ex)
               ErrorLog el = new ErrorLog();
               el.ErrorRegistry(ex, page, "User Validation.cs under login click", "NA", "NA");
               a = 3;
               return a;


CAN Someone help me on that that would be a great help for me!Thanks in Advance!!!

Don't use static variables (at all in a web project - it's not deterministic enough).

Store the user name in the Session, or in a cookie, depending on how long you want a log in to last. Session storage expires normally after about 20 minutes, cookie you can specify how long it is stored for.
Share this answer
Well, you answered your own question (almost!), this is what static variables do. They can only contain one value for the entire session, so if another user logs in the details of the previous user would be discarded and replaced by this new one.

It would put you in good stead to learn the ASP.NET page lifecycle first to understand how an ASP.NET web application or page works from start to finish. How does it deal with page level or instance level variables.

On the issue of login, use custom forms authentication the examples of which are all over the internet. You can also use a Session variable created for every user and identify the user based on the validity of the corresponding session.
Share this answer
Hesha 16-Oct-12 9:15am    
now i have a another doubt. because i used another Static variables for some of web forms for identify the mode of the users. example NEW and EDIT. mode = 1 then it is a new record and if mode = 2 then it is for Edit ect. ohh i guess same thing will be happening here. for that do you have any advice?how do i limit user modes to only to particular session can we used any other method?
I.explore.code 16-Oct-12 9:33am    
how do you use these modes i.e. how are your reading them? can you explain a bit more?
Hesha 16-Oct-12 9:46am    
in my web forms i have 5 buttons namely ADD ,Edit , SAVE ,DELETE and PRINT. when user needs to add a new entry user should press NEW button then i change the Static Variable Mode = 1 when user Click Edit then i change the Variable Mode = 2 then in the save button if mode =1 i wrote to add entry to DB. then it is Mode = 2 then system identify it is a Edit request.but now i feel it is big mistake to use Static variables for i want to remove that static variables from the system.but how could i replace that?
I.explore.code 16-Oct-12 9:56am    
I think you should redesign your forms to make the controls and their behavior less coupled. Have a Gridview that will have a Edit button template field to edit a record, a Delete button template field to delete a record and a totally separate form level button to Add new record. That way you can easily separate buttons and behaviors.
Unfortunately, this is not something you can instantly learn on a forum like this, you are gonna have to read up and do it yourself.
Hesha 16-Oct-12 10:09am    
i used gridview templetes for some forms.but i cannot used that for all the forms.this button structure was mandatory requirement in this system.i know there should be any other method for maintain session wise modes.perhaps i may made a wrong decision to have static variables because i thought it could be does not effect as long as server creates a new instance of the object.but i wrong on that occasion.but i feel it could be done with viewstatemode.Anyway thanks for support. i

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