0

I recently converted my App to use Session State "SQL Sever" instead of "InProc" as I had to do some redirects to a third-party app. Everything is working ok and I can set and retrieve session values easily. The problem occurs when I try to access the Session again from a different Controller method

This is how it is structured:

Add Company Method

public String AddNewCompany()
    {
       SalesInvoiceSettingController invoiceSetting = new SalesInvoiceSettingController();
       string Uname = Session["UserName"].ToString(); // This session value is picked correctly
     
     invoiceSetting.SaveInvoiceSettings(); // This is another method which I am calling inside this method, by creating an object of 'SalesInvoiceSettingController'

The SaveInvoiceSettings Method

 public string SaveInvoiceSettings()
    {
        
        string Uname = Session["UserName"].ToString(); <-- This is where I get a NULL POINTER EXCEPTION i.e Object Reference not set to an instance of an object.

Not sure what i am doing wrong here? it`s accessible from a method of one controller but not from a method of a different controller. Can anyone help me on this?

6
  • You're creating an instance of a controller inside another controller? Why? A better design would be for them to share a business layer object. I'm not sure if this is the cause, but you newing up a controller will do different things in the lifecycle than if the framework does it? Commented Apr 8, 2021 at 19:37
  • I can understand the design flaw here. What I can’t understand is why exactly is this issue occurring though? Could it be that the instance is causing the issue? Commented Apr 8, 2021 at 20:08
  • Possibly. I'm not sure how you're instantiating the controller, but it's possible the Session state is handled by the framework and injected at some point when it creates the controller, but not when you do, since you're not passing along a context where it comes from. Commented Apr 8, 2021 at 21:07
  • 1
    Ah, this looks interesting. stackoverflow.com/questions/889516/… Perhaps you can set the httpcontext and pass session that way Commented Apr 8, 2021 at 21:09
  • 1
    @Nikki9696... youre a life saver. This helped alot. I have posted the answer as well below. :) Commented Apr 9, 2021 at 9:35

1 Answer 1

0

Ok so I finally found the reason why this was happening. Basically as per the Framework, when you create an Instance , It doesnt really initialize it completely (i.e No Sessions, Request etc) are initialized but actually are 'Injected' later on in the process Life Cycle. In Order to resolve this issue , we would need to manually Initialize it. The solution is below on how to do it:

Consider Two Controllers:

  • Controller A (The controller where the Instance of 'Controller B' is created).
  • Controller B

First you need to create a method for Initialization in Controller B like this:

Controller B

  Public class ControllerB : Controller
{
    public void InitializeController(RequestContext requestContext)
    {
        base.Initialize(requestContext);
       
    }
   }

Now, create the instance for Controller B in Controller A and call this method:

Controller A

Public Class ControllerA : Controller
 {
   ControllerB obj1 = new ControllerB();
   obj1.InitializeController.InitializeController(this.Request.RequestContext);
  }

And you are good to go. You will have full access to all sessions. Thanks to nikki9696 for pointing me to the right direction.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.