0

I have an ASP.Net app and am trying to implement a sort of "change my role" functionality for testing purposes. Once I make the selections that determines my role, I use an ajax function to call a web service to reset session variables. Web service in turn calls another function to handle this.

But when I try to set session variable I keep getting null reference error.

Here is what I have done:

$(document).on("click", "#btnHome", function (event) {debugger
    var pathName = '<%= ResolveUrl("Home\\") %>';

    var role = $('#ddlRoles option:selected').text();
    var org = $('#ddlOrg option:selected').text();
    var orgID = $('#ddlOrg option:selected').val() != undefined ? $('#ddlOrg option:selected').val() : 0;
    var dept = $('#ddlDpt option:selected').text();
    var deptID = $('#ddlDpt option:selected').val() != undefined ? $('#ddlDpt option:selected').val() : 0;

    var ws_url = '<%= Page.ResolveUrl("~/WebService/RIA.asmx/ResetSessionVars")%>';
    var params = JSON.stringify({ Role: role, Org: org , OrgID: orgID, Dept: dept, DeptID: deptID });

    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: ws_url,
        cache: false,
        data:params,
    }).done(function (result) {debugger
        jResult = JSON.parse(result.d);
        window.location = pathName;      // go to home page with new role settings      

    }).fail(function (jqXHR, textStatus, errorThrown) {debugger
    });
});


[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

public void ResetSessionVars(string Role, string Org , string OrgID, string Dept, string DeptID)
{
    Sessions.SetSession(Role, Org, OrgID, Dept, DeptID);
}

Some BLL file:

I tried adding "HttpAPplication" as well as "IRequireSessionState: interface, but no change

namespace RIA
{
    public class Sessions : HttpApplication, IRequiresSessionState
    {
        public static void SetSession(string Role, string Org, string OrgID, string Dept, string DeptID)
        {
            HttpContext.Current.Session["UserRole"] = Role;  // null reference error
            HttpContext.Current.Session["OrganizationID"] = 0;
            HttpContext.Current.Session["Organization"] = string.Empty;
            ....
        }
    }
}
NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • Making a random class inherit from HttpApplication and IRequireState isn't going to do anything. Only classes that ASP.NET creates itself will benefit from adding those. Try changing `[WebMethod]` to `[WebMethod(EnableSession =true)]`. – mason Apr 14 '20 at 21:42
  • Thank you. I tried setting one of the session variables for testing by moving it to web method and it worked. Change your comment to answer and I will mark it as such. – NoBullMan Apr 14 '20 at 22:16

0 Answers0