Session state problem

V

Venkat Chellam

I have a peculiar problem. I have a simple web application which loads
some data from the oracle table and display in the datagrid in the
webpage and datagrid has page enabled which shows 10 rows at a page.I
have a search criteria to search the records based on the data range i
give


This is what i have done, in the !IsPostBack section. I am setting up
the oracle connection, creating dataset object, datadapter and i aslo
load the data from the database into dataset and move all dataset,
datadapter, and connection object to session object. Now user can do
different operations withe records, like he select a row from grid and
there is button on in column of the grid to update , so what i do ,
update the dataset object for that particular row and then when next
time page load is called, i call dataadapter.update to update the data
back to the database. so my dataproviders objects are stored in the
session object. First of all is it a good way to do this?

Second when the session ends, and if the user comes back and does some
operation, it going to fail to update because the session variables
are destroyed and it will generate an error. What should i do to solve
this problem? Or if session object is not a good place to store this
dataprovider object, where shoould i store?. My page doesn't have a
user login, i am just using the default session created by asp.net.
Please help me, its very urgent


venky
 
K

Kevin Spencer

First of all is it a good way to do this?

We can stop right there, because no, it is not a good way to do this. It
sounds like you're coming from an ASP background, where Connection Pooling
was not part of the platform. With ASP.Net, your best bet is to open and
close your Connections as quickly as possible, to release them back into the
pool.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
V

venkat chellam

Thanks for replying. Can you eloborate more what i am suppose to do? I
never worked on asp either, this is the first time i am working with
some webapplication.

can you tell me more in detail what ia m suppose to do or some sample
code?


venky
 
K

Kevin Spencer

Elaborate? Well, when you want to work with your database, open a
Connection, do your work, and close it immediately.

You may want to create a class of static database utility functions that you
can use whenever you need to connect to your database. I believe Microsoft
makes some of these available, although I'm not sure where (I wrote my own).
Hopefully, someone else here can tell you that.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
V

venkat chellam

what makes you think i am keep the connection open. I opening the
connection, getting the data using datadapter and filling it in dataset
and closing the connection. All my data is in the dataset object. When i
need to update, dataadapter opens it and closes it after it updates. In
my all finally statatment of try block, i have database.close if its
open. Only thing i am putting inside the session object is my dataset
object and dataadapter object and connection object which is closed and
i open it when i need it. But the point is after the session ends, my
dataset object is not valid and if user tries to change some thing it
gives error.My point is what particular step i am suppose to take when
session ends?
 
K

Kevin Spencer

It would really help if you quote the thread you're replying to. I'm having
a difficult time trying to remember what started this thread...
what makes you think i am keep the connection open.

I suppose your telling me that you were persisting a Connection in Session
State led me to believe that. You are the first person I've run across that
stores a disconnected Connection class instance in Session State. And if I
recall correctly, you didn't mention that it was disconnected.

Not sure what you mean by "valid." A DataSet is a disconnected set of data.
"Valid" is not a property of the DataSet class.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
V

venkat chellam

Ok, let me explain you my whole project what i am doing and then tell me
whats going wrong and what i am suppose to do


1. I am creating a simple webapplication. First page on the application
contains a search criteria with two datetime control. The page also
contains a webgrid control

2. When the users selects a datarange from the datetime controls, and
hit the search button, it suppose to run a query and display the data in
the datagrid.

3. What i am doing is in PageLoad event !IsNotPostBack, i am creating a
OracleConnection object with appropriate connection string and then
opening it. I create a Oracledatadapter object and set the selectcommand
to appropriate SQL query and also set updatecommand to stored procedure
name and create paramaters. Then i create a blank dataset object and
fill with dataadapter.fill(). Then i bind the grid with dataset view for
appropriate table. I move the dataset object, dataadapter object to the
session object for future page postback events

4. Grid has one column, which has a linkbutton and when i click on it,
i can update that row in the grid. OnEditCommand event, what i do is get
the row which need to be modified. Get the earlier stored dataset object
from the session object, get the appropriate row for which the update is
needed and then update it. LIke that the user can update couple of rows
and then finally when the page is reloaded , in page render event, i
update the data from the dataset back to database using stored
DataAdapter object in the session object.

Now whatever i am doing is right or wrong?

5. My problem now is if the session ends, dataset object and datadapter
object and some other session objects are destroyed , and if user is
alredy in the page and selects the row and updates it fails because
dataset object from the session is not valid any more.


I know i am doing something wrong, i am very new to web application and
asp.net. I hope i detailed what i am doing, you tell me what i am doing
wrong or how can i improve. Thanks for being so patient

venky
 
S

Shankar Reddy

I Agree but still I think sharing database connection across multiple pages is still good idea for scenarios
where you are connecting to legacy systems using system.data.odbc namespace and it puts system to death for 1 or more seconds.
 
K

Kevin Spencer

Okay, I'm following you better now.

Storing anything more than your DataSet is Session accomplishes nothing. As
I mentioned, you may want to create a class of static methods for working
with data. A method for getting a DataSet can open its own Connection, get
the data, close the Connection, and return the DataSet. End of story. All
you have to do is call it and get the DataSet it returns. Then you can store
that in Session or any other cache mechanism you like.

Your real problem is the question you asked about what to do if Session is
lost. This is a common problem, due to the fact that Sessions time out and
disappear for a variety of reasons (such as the user goin gout for a cup of
coffee and coming back). The solution is really quite simple: Whenever you
use Session State, you must always handle the eventuality that a Session
will be lost. This means that, any time you want to access Session, you need
to check if your Session variable is still there. If it isn't, provide some
means of recovering it, such as informing the user that their Session has
expired, and redirecting them, or simply re-querying the database - whatever
is most appropriate for your situation. Just remember that you must always
check for Session objects prior to attempting to use them.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
V

venkat chellam

thank you very much. I did change my code to remove datadapter and
connection object from my session. Now regarding the session end, you
said, i have to check the validity of session object, how do i do that?
In my page load, is there a way to see the session object is valid or
not?
 
K

Kevin Spencer

Not valid. Session State will always be there. You need to check if the
Session variables you create are still there. Example:

if (Session["SomeDataTable"] != null)
....

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
V

venkat chellam

HI kevin,

Can you ask you few more questions in asp.net?

I want to know how do i update the status bar in the client browser? I
mean i want to show show which user logged in? I am using windows user
login of the client as login id

venkat
 
K

Kevin Spencer

Hi venkat,

That would be done using JavaScript. Example:

window.status = "Spank your mamma.";

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top