Access Static Class Member from .ashx file [Resolved]

Posted by Nismeh under .NET Framework on 9/10/2013 | Points: 10 | Views : 4892 | Status : [Member] | Replies : 8
Hi,

I want to access static class member in .ashx file. Actually I am using static class to maintain session. But I've added jquery template which uses .ashx file. And in that file I want to access session's userID. Can any one suggest solution. ?

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It



Responses

Posted by: Bandi on: 9/11/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Hi Nimesh,
Finally you got solution as per my 3rd point listed in the post (Posted on: 9/11/2013 12:57:25 AM )

>> 3) Your code runs in an IHttpHandler, that does not specify either the IRequiresSessionState or IReadOnlySessionState interface


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Basically we can access global (static) variable by using class name as follows:

ClassName.VariableName will return the value from the static variable..

Refer these links:
http://go4answers.webhost4life.com/Example/access-session-variables-ashx-90708.aspx
http://forums.asp.net/t/1607101.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/10/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Nismeh,

I have confused with you questions that sayd "But I've added jquery template which uses .ashx file".
Can you post some code/Jquery code so that we can help you.

Happy Coding.

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 9/10/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Mahesh,

Its typical to paste code snippet here. In short you can think like my jquery is based on .ashx. And I want that .ashx file to use static class members.

I tried by putting in same namespace but still that object returns null value.

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 9/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hey Guys any suggestions?

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/11/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link once
http://forums.asp.net/t/1613704.aspx
http://www.codeproject.com/Questions/184412/How-to-access-session-in-ashx-file

The scenarios where Session value might be NULL are :
The Session object might be null, but only in certain circumstances, which you will only rarely run into:
1) If you have disabled the SessionState http module, disabling sessions altogether
2) If your code runs before the HttpApplication.AcquireRequestState event.
3) Your code runs in an IHttpHandler, that does not specify either the IRequiresSessionState or IReadOnlySessionState interface.

Check the above 3 cases....

Refer this link
http://stackoverflow.com/questions/8741164/using-session-vs-static-class-for-saving-user-data?lq=1

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/11/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Nismeh,

There are some regions where you can get object value as null.
The regions may include as Bandi(Chandu) mentioned in the above post.
But we can not help without seeing actual you code.
Well as I see you requirement, you follow below another approach to achieve this.

1. I thing your .ashx file is present on one any .aspx page.
2. So you can get static class session member values in you .aspx page.
3. What I suggested to do the below process.
4. Create a public property in you .ashx page.
public string ID

{
get
{
return _eid;
}
set
{
_eid = value;
}
}

5. Assign this Id values to you static session values form you .aspx page load event. Like below:-
protected void Page_Load(object sender, EventArgs e)

{
this.UserControl.ID = id; // Here id is you static class session member values.
}

6. So you can access the public property ID instead of "static class session member values" on you .ashx file. Pass this values to JQuery code.

Hope it can help you.

Happy Coding.

If it helps you or directs U towards the solution, MARK IT AS ANSWER




Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 9/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hey Guys Thanks for your suggestions.

I did it by inheriting System.Web.SessionState.IRequiresSessionState into my class.

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response