1

我有一个搜索控件,当用户单击搜索时,查询将通过后面的代码中的以下行传入 url(获取模式):

 Server.Transfer("portfolio_search_results.aspx?search=" + query); 

现在我有一个非常奇怪的问题,如果用户点击搜索按钮搜索工作正常,但如果用户再次点击搜索错误

你调用的对象是空的。

正在提高。搜索后转到其他页面并再次搜索工作正常,只有连续两次单击搜索按钮时才会发生这种情况。搜索控件位于主页面的顶部。搜索功能中的代码:

    SqlCommand cmd = new SqlCommand("something", _mainConnection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("ID", SessionManager.Session[SessionParam.ID].ToString()));
    cmd.Parameters.Add(new SqlParameter("Type", type));
    return _mainDb.ExecuteDataSet(cmd).Tables[0];

并且错误在会话中。

如果我使用的是 Response.Redirect 而不是 Server.Transfer 它根本不起作用,即使在第一次搜索时也会出现上述错误。

编辑:添加堆栈跟踪。

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


[No relevant source lines]


Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\e76777bb\bf0abb72\App_Web_portfolio_search_results.aspx.cdcab7d2.jiztmxz2.0.cs    Line: 0

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   Appeals.GetAppeals(String typeAppeals) +104
   search.make_dt() +21
   search..ctor() +311
   portfolio_search_results..ctor() +26
   ASP.portfolio_search_results_aspx..ctor() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\e76777bb\bf0abb72\App_Web_portfolio_search_results.aspx.cdcab7d2.jiztmxz2.0.cs:0
   __ASP.FastObjectFactory_app_web_portfolio_search_results_aspx_cdcab7d2_jiztmxz2.Create_ASP_portfolio_search_results_aspx() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\e76777bb\bf0abb72\App_Web_portfolio_search_results.aspx.cdcab7d2.jiztmxz2.1.cs:0
   System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119
   System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
   System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +160
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Version Information: Microsoft .NET Framework Version:2.0.50727.3634; ASP.NET Version:2.0.50727.3634 
4

2 回答 2

1

我建议您在调试模式下逐行诊断您的对象。例如:

if (_mainConnection == null)
{
  throw new Exception("_mainConnection is null");
}

SqlCommand cmd = new SqlCommand("something", _mainConnection);
cmd.CommandType = CommandType.StoredProcedure;

if (string.IsNullOrEmpty(SessionParam.ID))
{
  throw new Exception("SessionParam.ID is null or empty");
}
else if (SessionManager.Session[SessionParam.ID] == null)
{
  throw new Exception("SessionManager.Session[SessionParam.ID] is null or empty");
}

cmd.Parameters.Add(new SqlParameter("ID", SessionManager.Session[SessionParam.ID].ToString()));


if (type == null)
{
  throw new Exception("type is null")
}

cmd.Parameters.Add(new SqlParameter("Type", type));

var res =  _mainDb.ExecuteDataSet(cmd);

if (res == null)
{
  throw new Exception("res is null")
}

if ((res.Tables == null) || (res.Tables.Length == 0))
{
  throw new Exception("tables is null or = 0")  
}

return res.Tables[0];

因此,通过这种方式,您应该会收到有关错误的详细信息。不确定我的代码输入是否正确——我没有检查拼写错误。我认为主要思想是可以理解的。

问候。

于 2012-12-17T12:52:08.863 回答
0

我通过将会话作为参数传递给我项目中 App_code 目录中的类来解决它,而不是使用 App_code 类中的会话参数。所以研究是该会话不会在 app_code 目录中使用。

于 2012-12-20T14:58:21.910 回答