(Datatable)httpcontext.current.session显示为null [英] (Datatable)httpcontext.current.session is showing null

查看:61
本文介绍了(Datatable)httpcontext.current.session显示为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在访问HttpContext.Current.Session值时遇到问题。它显示为null。



例外消息System.NullReferenceException:对象引用未设置为对象的实例



我尝试过:



以下是示例代码。

public static List< string> GetAutoCustomers(string prefixText)

{

string strFilter = string.Empty;

DataTable dtCustomers = new DataTable();



BusinessObjects.es_CustomersBO ObjCustomersBO = new BusinessObjects.es_CustomersBO();

DataObjects.es_CustomersDAL ObjCustomersDAL = new DataObjects.es_CustomersDAL();

ObjCustomersBO.ES_RoleSwitchChar = _importantData;

ObjCustomersBO.ES_RoleValue = _userId;

DataSet dsCustomers = new DataSet();



dsCustomers = ObjCustomersDAL.GetCustomers(ObjCustomersBO);



DataView dv = dsCustomers.Tables [0] .DefaultView;

strFilter + =ES_CustomerName LIKE'+ prefixText +%';

dv.RowFilter = strFilter;

dtCustomers = dv.ToTable();

List< string> Customer = new List< string>();

for(int i = 0; i< dtCustomers.Rows.Count; i ++)

{

Customer.Add(dtCustomers.Rows [i] [2] .ToString());

HttpContext.Current.Session [CustomerName] = dtCustomers;

}



返回客户;



}











public void btnone_Click(object sender,EventArgs e)

试试

{

if(txtCustomers.Text.Length> 0)

{

DataTable dtCustomer = new DataTable();

dtCustomer =(DataTable)HttpContext.Current.Session [CustomerName];

DataRow []博士;

var primaryKey =ES_CustomerName;

string s = Session [CustomerName]。ToString();

string h = s;



dr = dtCustomer.Select([+ primaryKey +]喜欢'%'+ txtCustomers.Text +%');

ddlCustomers.SelectedValue = dr [0] [0] .ToString();

}

ObjCustomersBO.ES_CustomerMapID = Convert.ToInt32(hdnedit_recid.Value);

ObjCustomersBO.ES_CustomerIDRef = Convert.ToInt32(ddlCustomers.SelectedValue);



GetCustomerUserMapping();

}

catch(Exception除外)

{

Response.Write(错误是:+例外);

string str = excep.ToString();

}

解决方案

HttpContext为空,HttpContext.Current为null或者HttpContext.Current.Session为null。我们无法运行您的代码,所以我们不知道哪个,您需要使用调试器来查找。你为什么不只是使用Session?会议正常工作,如果它不适合你那么它是由于你正在做什么而你没有告诉我们。


给你一些建议:



1.引用 Session 值时,请始终检查对象首先是为了避免意外错误。例如:



  if (会话[  CustomerName]!=  null ){
// 访问此处的会话值
}





2.从 DataTable 过滤数据时,请先尝试检查行数。例如:



  if (dtCustomer.Rows.Count >   0 ){
// 找到行。
// 做点什么
}





3.如果您正在过滤数据而您没有得到所需的结果,那么你做的最好的事情是使用已经建议的调试器。设置一个断点并进入代码,找出代码中发生了什么。请参阅:使用调试器浏览代码



4.如果您正在使用SQL查询并且您的查询没有提供所需的结果,那么使用SQL Management Studio并在您的代码中运行您的查询,以便您知道您的查询您传递给它的参数很好。例如,在SQL查询分析器中运行您的实际Select查询,但这次您必须将更难的编码值传递给 Like 子句:



  SELECT  *  FROM  TableName  WHERE  ES_CustomerName  LIKE  '  ActualValueThatYouWantToTest%'; 





5.使用参数化查询保护您的代码免受SQL注入。但由于您使用 DataView RowFilter ,我不确定是否可以使用<$ c的参数$ C>数据视图。你可以做一个简单的字符串 替换来逃避单引号。例如:



 dv.rowfilter =  String  .Format(< span class =code-string>  ES_CustomerName LIKE'{0}%',prefixText.Replace( ' ' )); 


hi,
I am having a problem while accessing the HttpContext.Current.Session value.It is showing null.

exception message "System.NullReferenceException: Object reference not set to an instance of an object"

What I have tried:

Here is the sample code.
public static List<string> GetAutoCustomers(string prefixText)
{
string strFilter = string.Empty;
DataTable dtCustomers = new DataTable();

BusinessObjects.es_CustomersBO ObjCustomersBO = new BusinessObjects.es_CustomersBO();
DataObjects.es_CustomersDAL ObjCustomersDAL = new DataObjects.es_CustomersDAL();
ObjCustomersBO.ES_RoleSwitchChar = _importantData;
ObjCustomersBO.ES_RoleValue = _userId;
DataSet dsCustomers = new DataSet();

dsCustomers =ObjCustomersDAL.GetCustomers(ObjCustomersBO);

DataView dv = dsCustomers.Tables[0].DefaultView;
strFilter += "ES_CustomerName LIKE '" + prefixText + "%'";
dv.RowFilter = strFilter;
dtCustomers = dv.ToTable();
List<string> Customer = new List<string>();
for (int i = 0; i <dtCustomers.Rows.Count; i++)
{
Customer.Add(dtCustomers.Rows[i][2].ToString());
HttpContext.Current.Session["CustomerName"] = dtCustomers;
}

return Customer;

}





public void btnone_Click(object sender, EventArgs e)
try
{
if (txtCustomers.Text.Length>0 )
{
DataTable dtCustomer = new DataTable();
dtCustomer = (DataTable)HttpContext.Current.Session["CustomerName"];
DataRow[] dr;
var primaryKey = "ES_CustomerName";
string s = Session["CustomerName"].ToString();
string h = s;

dr = dtCustomer.Select("[" + primaryKey + "] Like '%" + txtCustomers.Text + "%'");
ddlCustomers.SelectedValue = dr[0][0].ToString();
}
ObjCustomersBO.ES_CustomerMapID = Convert.ToInt32(hdnedit_recid.Value);
ObjCustomersBO.ES_CustomerIDRef = Convert.ToInt32(ddlCustomers.SelectedValue);

GetCustomerUserMapping();
}
catch(Exception excep)
{
Response.Write("error is:"+excep);
string str = excep.ToString();
}

解决方案

Either HttpContext is null, HttpContext.Current is null or HttpContext.Current.Session is null. We can't run your code so we don't know which, you'll need to use the debugger to find out. Why aren't you just using "Session"? The Session just works, if it isn't for you then it is due to something about what you're doing that you're not telling us.


A few suggestions for you:

1. When referencing a Session value, always check for object nullity first to avoid unexpected error. For example:

if(Session["CustomerName"] != null){
   //access Session value here
}



2. When filtering data from DataTable, try to check for it's row count first. For example:

if(dtCustomer.Rows.Count > 0){
   // found rows.
   // do something
}



3. If you are filtering data and you are not getting the desired result, then the best thing you do is to use the debugger as already suggested. Set a break point and step into your code to figure out what's going on in the code. See: Navigating through Code with the Debugger

4. If you are working with SQL queries and your query doesn't give you the desired result, then use the SQL Management Studio and run the query that you have in your code against it so you would know if your query is fine with the parameters that you passed on to it. For example run your actual Select query in SQL query analyzer but this time you have to pass a harder coded value to your Like clause:

SELECT * FROM TableName WHERE ES_CustomerName LIKE 'ActualValueThatYouWantToTest%';



5. Use parameterize query to protect your code from SQL Injection. But since you were using DataView RowFilter, I'm not sure if it's possible to use parameters with a DataView. You can do a simple string replace to escape single quotes though. For example:

dv.rowfilter= String.Format(" ES_CustomerName LIKE ‘{0}%’", prefixText.Replace("'", "''"));


这篇关于(Datatable)httpcontext.current.session显示为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆