views:

374

answers:

5

I bind a SQLDataSource to a GridView using GridView.DataBind() and these queries often take a significant time to execute.

What code would cancel an in-progress DataBind()?

I can't seem to find any method to stop the DataBind() or even close the DB connection.

Rebind Attempt:

myConn.ConnectionsString = ""
myConn.SelectCommand = ""
myGrid.DataSource = Nothing
myGrid.DataBind()

Dispose Attempt

myConn.Dispose()
myGrid.Dispose()

Neither attempt actually closed the connection. Oracle still indicated it was still connected and executing the query.

A: 

You could manually bind the GridView and that would allow you to insert logic into the binding procedure to allow you to exit that procedure.

To do this subscribe to the onDataBinding event of the GridView.

You can read more about manually binding the GridView here: http://www.aarongoldenthal.com/post/2009/04/19/Manually-Databinding-a-GridView.aspx

Justin C
Elaborate on "insert logic ... to exit that procedure". I want to stop the DataBind, close the DB connection, and therefore terminate the query on the DB.
Steven
By this I meant instead of using a SelectCommand from a SQLDataSource to automatically populate your GridView you could write your own logic to populate the GridView. So in the function that your register to be called onDataBinding for your GridView you query your database and start inserting the values into your GridView.This is a fair amount of work since you're really re-inventing the wheel, but it allows you to stop the process whenever you want, close the connection, and even provide the user with feedback.Does that help?
Justin C
How do I close the connection? I can't find the method for that.
Steven
Well, I'm assuming that in this code-behind function you open the connection, so that would mean that you just have to call the .Close() function on your connection object.
Justin C
There are no explicit open/close calls in nether my code nor on the example page you provided. In the ASP XML code, I set the DataSourceID of the GridView to the SQLDataSource. My page_load() event handler simply calls DataBind and formats the GridView.
Steven
Right, what I am suggesting would be to not set the GridView DataSource to a SQLDataSource. Instead do not set the DataSource or DataSourceID in the ASPX file.In your ASPX set the onDataBinding property of your GridView to a function in the CodeFile. In that function open a connection, query the DB, create a DataTable, and set your GridView's DataSource to equal the DataTable that you have created. This is a lot of work to get you in the same place you were. The difference is you can add logic to break out of this code at any point and close the connection.Almost out of space.does this help?
Justin C
I'm fairly new to ASP. This is the only way I know to connect to the database with ASP. Can you provide code (in your answer) to manually connect to the database and put the results in a GridView?
Steven
well, this is pretty complicated and if you're new to ASP it may cause you more headaches than it is worth. I see your post is tagged with Oracle, is that the database you are using? Where is your connection string, in the web.config?
Justin C
Yes, I'm using Oracle9i. My connectionstring is in web.config.
Steven
+1  A: 

How about addressing the problem from a different direction. Have you looked at optimizing the query, either by changing joins and subqueries, or simply by pulling less data, maybe based on other inputs on the page?

Dave
A: 

I agree with Dave, although I would suggest paging in this scenario. There are many variations, but my first approach here would be to limit the number of rows pulled back to a specific number at a time. Trying to make the connection, then break it if it dosen't respond in time is only going to frustrate your application users.

Here's one post that might help you get started.

http://stackoverflow.com/questions/711019/smart-paging-with-datagrid

Joel Tipke
A: 

using{} block suits this situations. use your connections with using block

Ozan BAYRAM
A: 

Just close the Connection and free all the resource.

myConn.Close();
myConn.Dispose();
Devi
Did you actually read the question?
Filburt