By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

re-using c# code in asp.net with minimal resources

P: n/a
Please take your time to read the following, it's simple although the text
is 3 paragraphs...

Everytime an user is logged in I need to make a call to my sql server
database to grab some specific user settings and load into Session
variables. So, I have a "Loggedin" event in login.aspx which does this. As
users can be logged in (automatically) via HttpApplication & Cookies, I need
to run this same code after that. I can put the same code as above in this
particular event handler in my global.asax code but I want to have this code
stored in only one place although called from the above mentioned two
situations.

1) I store the custom code in global.asax, create a new global_asax class
from the login.aspx code and call the method. This means that every time an
user is logged in I have eaten up unneccesary amount of resources since the
new global_asax class resides in memory until destroyed (especially since
the code creates both SqlConnection and SqlDataReader objects). Also I'm
afraid I could run into thread-level clashes in case multiple requests are
using the global_asax code at same time (the SqlDataReader for example is
very sensitive here).
2) I have a custom "code" class e.g. "commons.cs" in my App_Code folder and
from global.asax and login.aspx create new instance of that class and call
the code. The same as above: this uses a lot of resources compared to a very
simple task.
....

I hope you get my point! Any ideas on how to solve this "smoothly", using
minimal resources and without storing the code.
Jan 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Daves,

You should go with #2. I don't understand why you think that uses a
good deal of resources. It's just a class instance, and if it is not such a
complex operation, you can always make the methods static.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daves" <db****@simnet.is> wrote in message
news:ua*************@TK2MSFTNGP15.phx.gbl...
Please take your time to read the following, it's simple although the text
is 3 paragraphs...

Everytime an user is logged in I need to make a call to my sql server
database to grab some specific user settings and load into Session
variables. So, I have a "Loggedin" event in login.aspx which does this. As
users can be logged in (automatically) via HttpApplication & Cookies, I
need to run this same code after that. I can put the same code as above in
this particular event handler in my global.asax code but I want to have
this code stored in only one place although called from the above
mentioned two situations.

1) I store the custom code in global.asax, create a new global_asax class
from the login.aspx code and call the method. This means that every time
an user is logged in I have eaten up unneccesary amount of resources since
the new global_asax class resides in memory until destroyed (especially
since the code creates both SqlConnection and SqlDataReader objects). Also
I'm afraid I could run into thread-level clashes in case multiple requests
are using the global_asax code at same time (the SqlDataReader for example
is very sensitive here).
2) I have a custom "code" class e.g. "commons.cs" in my App_Code folder
and from global.asax and login.aspx create new instance of that class and
call the code. The same as above: this uses a lot of resources compared to
a very simple task.
...

I hope you get my point! Any ideas on how to solve this "smoothly", using
minimal resources and without storing the code.

Jan 11 '06 #2

P: n/a
thx, feel a little more confident in using that way then. Just have this
feeling that a class instance creating a SqlConnection and SqlDataReader
would eat up kilobytes of memory and could be solved in an easier way. Btw,
is there a method to "destroy" that class again when it's been used?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Daves,

You should go with #2. I don't understand why you think that uses a
good deal of resources. It's just a class instance, and if it is not such
a complex operation, you can always make the methods static.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daves" <db****@simnet.is> wrote in message
news:ua*************@TK2MSFTNGP15.phx.gbl...
Please take your time to read the following, it's simple although the
text is 3 paragraphs...

Everytime an user is logged in I need to make a call to my sql server
database to grab some specific user settings and load into Session
variables. So, I have a "Loggedin" event in login.aspx which does this.
As users can be logged in (automatically) via HttpApplication & Cookies,
I need to run this same code after that. I can put the same code as above
in this particular event handler in my global.asax code but I want to
have this code stored in only one place although called from the above
mentioned two situations.

1) I store the custom code in global.asax, create a new global_asax class
from the login.aspx code and call the method. This means that every time
an user is logged in I have eaten up unneccesary amount of resources
since the new global_asax class resides in memory until destroyed
(especially since the code creates both SqlConnection and SqlDataReader
objects). Also I'm afraid I could run into thread-level clashes in case
multiple requests are using the global_asax code at same time (the
SqlDataReader for example is very sensitive here).
2) I have a custom "code" class e.g. "commons.cs" in my App_Code folder
and from global.asax and login.aspx create new instance of that class and
call the code. The same as above: this uses a lot of resources compared
to a very simple task.
...

I hope you get my point! Any ideas on how to solve this "smoothly", using
minimal resources and without storing the code.


Jan 12 '06 #3

P: n/a
Daves,

Well, once you let the instance go, that's it, if there are no other
references to it, then you shouldn't have a problem. The GC will eventually
clean it up.

As for using SqlConnection and SqlDataReader, on a website especially,
you should be creating your own connection, opening it, and closing it in
your specific operation. You definitely should not be sharing an open
connection in your website.

Just make sure you dispose of the SqlConnection and SqlDataReader
correctly (using statements help here), and you should be fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daves" <db****@simnet.is> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
thx, feel a little more confident in using that way then. Just have this
feeling that a class instance creating a SqlConnection and SqlDataReader
would eat up kilobytes of memory and could be solved in an easier way.
Btw, is there a method to "destroy" that class again when it's been used?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
Daves,

You should go with #2. I don't understand why you think that uses a
good deal of resources. It's just a class instance, and if it is not
such a complex operation, you can always make the methods static.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daves" <db****@simnet.is> wrote in message
news:ua*************@TK2MSFTNGP15.phx.gbl...
Please take your time to read the following, it's simple although the
text is 3 paragraphs...

Everytime an user is logged in I need to make a call to my sql server
database to grab some specific user settings and load into Session
variables. So, I have a "Loggedin" event in login.aspx which does this.
As users can be logged in (automatically) via HttpApplication & Cookies,
I need to run this same code after that. I can put the same code as
above in this particular event handler in my global.asax code but I want
to have this code stored in only one place although called from the
above mentioned two situations.

1) I store the custom code in global.asax, create a new global_asax
class from the login.aspx code and call the method. This means that
every time an user is logged in I have eaten up unneccesary amount of
resources since the new global_asax class resides in memory until
destroyed (especially since the code creates both SqlConnection and
SqlDataReader objects). Also I'm afraid I could run into thread-level
clashes in case multiple requests are using the global_asax code at same
time (the SqlDataReader for example is very sensitive here).
2) I have a custom "code" class e.g. "commons.cs" in my App_Code folder
and from global.asax and login.aspx create new instance of that class
and call the code. The same as above: this uses a lot of resources
compared to a very simple task.
...

I hope you get my point! Any ideas on how to solve this "smoothly",
using minimal resources and without storing the code.



Jan 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.