Re: Session and Database Connection, Which Is More Costy?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




You can write static methods (shared vb.net) to encapsulate the class.


public class HelperSessionInfo
{

private readonly string USER_ID_KEY = "UserIdKey";

public static void SetUserID (int userId)
{
Session[USER_ID_KEY] = userId;
}

public static int GetUserId()
{
int returnValue = 0;
if(null!=Session[USER_ID_KEY]
{
returnValue = (int)Session[USER_ID_KEY];
}

return returnValue;
}


}



That's a tad bit cleaner.

You can check this out as well.
http://sholliday.spaces.live.com/blog/cns!A68482B9628A842A!151.entry

it will actually give you strong typing.
and good ".Clear()" and ".Remove(key)" methods.


...

"gnewsgroup" <gnewsgroup@xxxxxxxxx> wrote in message
news:1194630149.099969.21060@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 9, 11:23 am, bruce barker <nos...@xxxxxxxxxx> wrote:
i check session first, if not there do query and save to session. this
handles never clicking button, and session recycles

-- bruce (sqlwork.com)

gnewsgroup wrote:
I am just curious about this issue.

I have a few buttons on a single aspx page. Each time one of the
buttons is clicked, I need to know if a customer has an unpaid
balance.

I only know of 2 ways to handle this:

1. Open the database connection only when the page is loaded the first
time, check the database for unpaid balance and save it as a session
variable. (Thus avoiding repetitive db connections.)

2. Upon clicking of each button on the page, I open up the database
and check the database for unpaid balance. (Thus avoiding session
variables.)

I heard through the grapevine that database connection is costy. In
the same manner, I heard that session will cause a scalability
problem.

So, in situations like this, what factors help us decide which
strategy to use?

Also, how about saving the value in a hidden field?

OK, great, but I find session sorta messy though. For example, if
somewhere in your logic you forget to update the session variable
value, you will be using the same old value, and you may not know that
something is wrong.



.



Relevant Pages