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;
....
}
}
}