簡體   English   中英

會話在當前上下文中不存在?

[英]Session doesnt exist in current context?

嘗試實施一個會話時遇到麻煩,它說在當前上下文中不存在該會話嗎?

 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"); 
        } 
    } 

它應該是

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

您需要使用方括號

我認為似乎認為您正在將Session對象作為一種方法來訪問。 您需要具備:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM