开发者

Session doesnt exist in current context?

Having trouble trying to implement a session it says it doesnt exist in the current context am i missing something?

 protected void开发者_运维知识库 Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        //database connection string
        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=x; Password=x; OPTION=3;");
        cn.Open();
        OdbcCommand cmd = new OdbcCommand("Select * from User where username=? and password=?", cn);
        DataSet ds = new DataSet();
        //Select the username and password from mysql database 

        cmd.Parameters.Add("@username", OdbcType.VarChar);
        cmd.Parameters["@username"].Value = this.Login1.UserName;

        cmd.Parameters.Add("@password", OdbcType.VarChar);
        cmd.Parameters["@password"].Value = this.Login1.Password;
        //use asp login control to check username and password

        //Session["UserID"] = "usrName";
        //set the UserID from the User Table unsure how to add this to the sql syntax above

        OdbcDataReader dr = default(OdbcDataReader);
        // Initialise a reader to read the rows from the login table.  
        // If row exists, the login is successful  

        dr = cmd.ExecuteReader();
        DataTable dt = ds.Tables[0];
        DataRow dp = dt.Rows[0];

        OdbcDataAdapter adp = new OdbcDataAdapter(cmd)
        { 
        DataTable dt = new DataTable(); 
        adp.Fill(dt); 
        if (dt.Rows.Count != 0) 
        { 
            Session("UserID") = Convert.ToString(dt.Rows[0]["UserID"]);
            e.Authenticated = true; 
            Response.Redirect("UserProfileWall.aspx"); 
        } 
    } 


It should be

Session["UserID"] = Convert.ToString(dp["UserID"]);

You need to use square brackets


I think it seems to think you're accessing the Session object as a method. You need to have:

Session["UserID"] = dp["UserID"].ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜