i have asp.net application , dll extends ihttpmodule. have used below method save session variables in httpcontext through
public class handler : ihttpmodule,irequiressessionstate { public void init(httpapplication httpapp) { httpapp.prerequesthandlerexecute += new eventhandler(prerequesthandlerexecute); } public void prerequesthandlerexecute(object sender, eventargs e) { var context = ((httpapplication)sender).context; context.session["myvariable"] = "gowtham"; } }
and in asp.net default.aspx page have used code retrive value as
public partial class _default : system.web.ui.page, irequiressessionstate { protected void page_load(object sender, eventargs e) { string token = context.session["myvariable"].tostring(); } }
i getting error response as
description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.nullreferenceexception: object reference not set instance of object.
in order ensure whether variables store in session have crossed check following method in class handler after storing value in session as
string ss = context.session["myvariable"].tostring();
it executed , retrieved value session.
why need use context , not session directly? code can assume going set value in session, , read value on page load. rather that, can this:
add global application class, right click on project, add > new item, choose global application class, , on file, insert following code initialize value
protected void session_start(object sender, eventargs e) { session["myvariable"] = "gowtham"; }
on page_load, can access by:
if ( session["myvariable"] != null ) { string token = context.session["myvariable"].tostring(); }
hope help..
Comments
Post a Comment