一个对象引用所需的非静态字段,方法或属性“System.Web.UI.Page.Session.get” [英] An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'
本文介绍了一个对象引用所需的非静态字段,方法或属性“System.Web.UI.Page.Session.get”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误
需要一个对象引用的非静态字段,方法或
财产'System.Web.UI.Page.Session.get
块引用>您可以建议我在会议上该问题中恢复。
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Web.Services;
使用System.Configuration;
使用System.Data.SqlClient的;
使用System.Web.SessionState;公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ }
// ONCLICK提交按钮
[的WebMethod(EnableSession =真)]
//[System.Web.Services.WebMethod(EnableSession = TRUE)]
公共静态字符串登录(字符串email,字符串密码)
{
VAR CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [blogConnString]的ConnectionString);
con.Open();
串解析度=0;
SqlDataReader的阅读器;
字符串SQL ='+电子邮件+和密码='+密码+',从个人的电子邮件,其中选择= UID,用户名;
CMD1的SqlCommand =新的SqlCommand(SQL,CON);
读者= cmd1.ExecuteReader();
而(reader.Read())
{
解析度=1;
会话[UID] =读者[UID]的ToString()。这里//错误行
。会话[UNAME] =读者[用户名]的ToString();这里//错误行
}
返回水库;
con.Close();
}
}
解决方案试试这个code和请用品SQL注入的尝试使用参数化查询。
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Web.Services;
使用System.Configuration;
使用System.Data.SqlClient的;
使用System.Web.SessionState;公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ }
// ONCLICK提交按钮
[的WebMethod(EnableSession =真)]
//[System.Web.Services.WebMethod(EnableSession = TRUE)]
公共静态字符串登录(字符串email,字符串密码)
{
VAR CON = ConfigurationManager.ConnectionStrings [blogConnString]的ConnectionString。
con.Open();
串解析度=0;
SqlDataReader的阅读器;
字符串SQL =从个人的电子邮件,其中@ =电子邮件和密码= @密码选择UID,用户名;
使用(SqlConnection的连接=新的SqlConnection(CON))
{
的SqlCommand命令=新的SqlCommand(CommandText中,连接);
command.Parameters.Add(@电子邮件,SqlDbType.String);
command.Parameters [@电子邮件]值=电子邮件。 command.Parameters.AddWithValue(@密码,密码);
读者= Command.ExecuteReader却();
而(reader.Read())
{
解析度=1;
HttpContext.Current.Session [UID] =读者[UID]的ToString()。 //无论是从方法声明中删除静态或使用HttpContext.Current与会话一起。
HttpContext.Current.Session [UNAME] =读者[用户名]的ToString()。
}
}
返回水库;
con.Close();
}
}I'm getting error like
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'
Can you suggest me to recover from this problem in session.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Services; using System.Configuration; using System.Data.SqlClient; using System.Web.SessionState; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //Onclick Submit Button [WebMethod(EnableSession = true)] //[System.Web.Services.WebMethod(EnableSession = true)] public static string Login(string email, string password) { var con = new SqlConnection(ConfigurationManager.ConnectionStrings["blogConnString"].ConnectionString); con.Open(); string res = "0"; SqlDataReader reader; string sql = "select uid,username from personal where email='" + email + "' and password='" + password + "'"; SqlCommand cmd1 = new SqlCommand(sql, con); reader = cmd1.ExecuteReader(); while (reader.Read()) { res = "1"; Session["UID"] = reader["uid"].ToString(); //error line here Session["UNAME"] = reader["username"].ToString(); //error line here } return res; con.Close(); } }
解决方案try this code and please be-ware of SQL Injection try to use Parametrized Query.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Services; using System.Configuration; using System.Data.SqlClient; using System.Web.SessionState; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //Onclick Submit Button [WebMethod(EnableSession = true)] //[System.Web.Services.WebMethod(EnableSession = true)] public static string Login(string email, string password) { var con = ConfigurationManager.ConnectionStrings["blogConnString"].ConnectionString; con.Open(); string res = "0"; SqlDataReader reader; string sql = "select uid,username from personal where email=@Email and password=@Password"; using(SqlConnection connection = new SqlConnection(con)) { SqlCommand command = new SqlCommand(commandText, connection); command.Parameters.Add("@Email", SqlDbType.String); command.Parameters["@Email"].Value = email; command.Parameters.AddWithValue("@Password", password); reader = command.ExecuteReader(); while (reader.Read()) { res = "1"; HttpContext.Current.Session["UID"] = reader["uid"].ToString(); //Either Remove Static from Method Declaration or use HttpContext.Current along with session. HttpContext.Current.Session["UNAME"] = reader["username"].ToString(); } } return res; con.Close(); } }
这篇关于一个对象引用所需的非静态字段,方法或属性“System.Web.UI.Page.Session.get”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文