c# - how to use session in hyper link selection -


i have hyperlink in web page, link should work if user logged in if not should prompt error message indicating user not logged in. have tried lot not working expect. think session code not working. please me find out problem.

this login button code:

    protected void button2_click(object sender, eventargs e)     {         mysqlconnection connection = new mysqlconnection("server=localhost; database=e-learningsystem; uid=root; password=123;port=3307;");         connection.open();         try         {             mysqlcommand cmd = new mysqlcommand("select password student username='" + textbox2.text + "'", connection);             // string password;             password = convert.tostring(cmd.executescalar());             mysqldatareader dr = cmd.executereader();             if ((textbox1.text == password) && (dr.hasrows))             {                 response.redirect("1sttymstucategoryselection.aspx");                 while (dr.read())                 {                     userinfo ui = new userinfo(dr["username"].tostring(), dr["password"].tostring(), dr["name"].tostring());                     session["loggeduser"] = ui;                 }                 connection.close();             }             else             {                 response.write(@"<script language='javascript'>alert('invalid username , password, try again!')</script>");                 // session.clear();             }         }          catch (exception ex)         {         }         textbox2.text = "";         textbox1.text = "";     } 

this userinfo class code:

    public class userinfo {     private string _username;     private string _password;     private string _name;      public userinfo(string username, string password, string name)     {         _username = username;         _password = password;         _name = name;     }     public string username     {         { return _username; }     }     public string password     {         { return _password; }     }     public string name     {         { return _name; }         set { _name = value; }     }  } 

this link hyperlink selection done:

    protected void page_load(object sender, eventargs e)     {         if (session["loggeduser"] != null)         {             hyperlink1.navigateurl = "year1sem1sub1.aspx";         }         else         {             hyperlink1.visible = false;         }      } 

your problem redirecting page before setting session value, session null.

this code problem:

if ((textbox1.text == password) && (dr.hasrows)) {     response.redirect("1sttymstucategoryselection.aspx"); 

move response.redirect further down in code logic (i.e. after set session value).


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

android - Inheriting from Theme.AppCompat* -