C# 会话在当前上下文中不存在?
在尝试实现一个会话时遇到问题,它说它在当前上下文中不存在,我是否遗漏了什么C# 会话在当前上下文中不存在?,c#,asp.net,mysql,sql,html,C#,Asp.net,Mysql,Sql,Html,在尝试实现一个会话时遇到问题,它说它在当前上下文中不存在,我是否遗漏了什么 protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { //database connection string OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=local
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["UserID"] = dp["UserID"].ToString();
哈哈,我看到了。你可能会说dp[“UserID”].ToString()更正确,虽然我可以,但我并没有那么挑剔。我如何在另一个aspx页面上检索它?@Garrith Graham:如果你的意思是在完成后如何访问另一个网页中的会话变量,你会使用:
If(session[“UserID”!=null){string UserID=session[“UserID”].ToString();}
@Garrith您需要使用OdbcDataAdapter来填充数据表。试试这个:使用(OdbcDataAdapter adp=newodbcdataadapter(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”);}对不起,霍普迪泽,你能把这个作为回答吗?我有一些错误?