YaakovHatam
YaakovHatam

Reputation: 2344

'Object reference not set to an instance of an object' after Server.Transfer

I have a search control, when the user clicking on search the query is passing in the url (get mode) with the following line from the code behind:

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

now i have a very strange problem, if the user is clicking on the search button the search works fine, but if the user click search again the error

Object reference not set to an instance of an object.

is raising. going to other page after the search and searching again works fine, this is happening only when clicking search button twice in a row. the search control is in the top of the page inside the master. the code in the search function:

    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];

and the error is in the session.

If i am using Response.Redirect instead of Server.Transfer it's not working at all and getting the above error even in the first time searching.

edit: adding stack trace.

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 

Upvotes: 1

Views: 2760

Answers (2)

YaakovHatam
YaakovHatam

Reputation: 2344

I solve it by passing the session as parameter to my classes in App_code directory in my project instead using the session parameter inside the App_code classes. so the study is that session wouln't be use in the app_code directory .

Upvotes: 0

Anton Norka
Anton Norka

Reputation: 2312

I suggest you diagnose line by line your objects in debug mode. For example:

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];

So, this way you should receive detailed information about your error. Not sure if I type well my code - I didn't check spell errors. I think main idea is understandable.

Regards.

Upvotes: 1

Related Questions