locked
regarding session Variable RRS feed

  • Question

  • User-733682151 posted

    namespace OnlineTest.Pagelet1
    {



        public partial class header : System.Web.UI.UserControl
        {
            
            public class ForumHeader : UserControl
            {

                public Label lblOptions;
                public Label lblForumName;
                public HyperLink linkTitle;
                public Label lblUser;
                public Label lblPass;
                public TextBox txtUser;
                public TextBox txtPass;
                public Button btnLogin;
                public Label lblLoginError;
                public Label lblLogin;
                public LinkButton lnklogoff;
                int UserId;
                DataSet myDS = new DataSet();
                OleDbConnection myConn;
                OleDbDataAdapter myCommand;
                string strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" +      System.Web.HttpContext.Current.Server.MapPath("test.mdb");
                string strSQL;
                String u;



                //Eample of checking if logged on
                private bool blnLoggedOn;
                protected void lnklogoff_Click(object sender, EventArgs e)
                {
                    Session("UserId") =0;  //error
                    CheckLogon();
                }


                protected void btnLogin_Click(object sender, EventArgs e)
                {// See if login is correct
                    strSQL = "SELECT * From Users Where Username='" + txtUser.Text + "' And Pass = '" + txtPass.Text + "'";


                    // Populate the Dataset from the SQL statement
                    // Start Connection to ADO source
                    myConn = new OleDbConnection(strConnString);
                    // Run the Sql statement and get the Data into the DataAdapter
                    myCommand = new OleDbDataAdapter(strSQL, myConn);
                    // Fill the DataSet and name the table
                    myCommand.Fill(myDS, "Login");
                    if (myDS.Tables["Login"].Rows.Count < 1)
                    {
                        // bad login
                        lblLoginError.Text = "Invalid Login!";
                        return;
                    }
                    else
                    {
                        // good login
                        u = myDS.Tables["Login"].Rows[0]["Id"].ToString().Trim();
                        Session("UserId")=Convert .ToInt16(u); //error
                    }
                    myConn.Close();

                    //CheckLogon();

                }


                private void CheckLogon()
                {
               
                   if (Session("UserID") != 0) //error
                   {
                       blnLoggedOn = true;
                  }
                  else
                    {
                       blnLoggedOn = false;
                   }

                    if (blnLoggedOn)
                   {
                       lblLogin.Visible = false;
                       lblUser.Visible = false;
                      lblPass.Visible = false;
                      txtUser.Visible = false;
                      txtPass.Visible = false;
                        btnLogin.Visible = false;
                       lnklogoff.Visible = true;
                  }
                   else
                    {
                     lblLogin.Visible = true;
                       lblUser.Visible = true;
                       lblPass.Visible = true;
                    txtUser.Visible = true;
                       txtPass.Visible = true;
                       btnLogin.Visible = true;
                      lnklogoff.Visible = false;
                  }
                }
                
                private void Page_Load(object sender, EventArgs e)
                {
                  CheckLogon();
                }

            }
            
        }   
        
    }


    error is

    Error    4    The name 'Session' does not exist in the current context    D:\OnlineTest\OnlineTest\Pagelet1\header.ascx.cs    56    17    OnlineTest

    Error    5    The name 'Session' does not exist in the current context    D:\OnlineTest\OnlineTest\Pagelet1\header.ascx.cs    83    21    OnlineTest

    Error    6    The name 'Session' does not exist in the current context    D:\OnlineTest\OnlineTest\Pagelet1\header.ascx.cs    97    21    OnlineTest


    if i comment it ther error like

    'ASP.Header' does not contain a definition for 'btnLogin_Click' and no extension method 'btnLogin_Click' accepting a first argument of type 'ASP.Header' could be found (are you missing a using directive or an assembly reference?)

    Saturday, February 12, 2011 2:09 AM

Answers

  • User224899486 posted

    Hi,


    In c# Session variable are not used in the manner u used in your code ( Session("UserID") ).

    This denotes that you are calling a function named Session which accepts one parameter of String type.

    So, if you want to use Session Variales in C#, you shoul use following syntax

    Session["UserID"] = 124;

    and in VB.net, syntax will be like this

    Session(UserID") = 124


    Hope it will help you.


    Please dont forget to mark Answer it helps you.....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 13, 2011 1:42 AM

All replies

  • User-42918493 posted

    HttpContext.Current.Session["Login"];

    use like this... Refer this...

    http://www.geekpedia.com/KB85_The-name-Session-does-not-exist-in-the-current-context.html

    Saturday, February 12, 2011 3:52 AM
  • User1682618242 posted

    First make sure that you have using System.Web.UI;

    And second, is Session["key"] not Session("key") - in other words is a property not a function

    Saturday, February 12, 2011 3:55 AM
  • User224899486 posted

    Hi,


    In c# Session variable are not used in the manner u used in your code ( Session("UserID") ).

    This denotes that you are calling a function named Session which accepts one parameter of String type.

    So, if you want to use Session Variales in C#, you shoul use following syntax

    Session["UserID"] = 124;

    and in VB.net, syntax will be like this

    Session(UserID") = 124


    Hope it will help you.


    Please dont forget to mark Answer it helps you.....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 13, 2011 1:42 AM
  • User-733682151 posted

    i want to chat with u  nitesh? it is possible

    Monday, February 14, 2011 1:02 AM
  • User224899486 posted

    Sure. Any confussion regarding my answer!!!

    Monday, February 14, 2011 1:12 AM
  • User2022958948 posted

    Did you resolve it? If so, could you please post your answer here. If not, you can post your updated question you encountered  

    Tuesday, February 15, 2011 9:52 PM