jgok222
jgok222

Reputation: 414

Does a Sql Datasource run a select query on load if no contol binds is bound to it?

How can I configure a gridview and datasource on a page to only execute the query if the user clicks a button?

The datasource will return over 1 million records and the page will be accessed by a lot of people at the same time. One potential way to achieve this is to set up the datasource with a connection string and query, but do not assign it to the grid view. Then assign the gridview to the datasource and call databind when it is needed.

In this scenario, will the datasource run the query on page load? Or will it only run the query when I call databind on the gridview?

Upvotes: 3

Views: 4692

Answers (2)

Ratna
Ratna

Reputation: 2319

The short answer in no, the datasource select statement is only called when bind method is called. see details http://msdn.microsoft.com/en-us/library/dz12d98w%28v=vs.80%29.aspx

http://msdn.microsoft.com/en-us/library/w1kdt8w2%28v=vs.100%29.aspx

Transcribe-->

The data source control executes the commands when its corresponding Select, Update, Delete, or Insert method is called. The Select method is called automatically when you call the DataBind method of the page or of a control bound to the data source control. You can also call any of the four methods explicitly when you want the data source control to execute a command. Some controls, such as the GridView control, can call the methods automatically, without requiring that you call the methods or that you explicitly call the DataBind method.

Upvotes: 4

watraplion
watraplion

Reputation: 287

Datasource will not run the query on page load on your case. Write your gridview binding code on click event, then it will run the query only after click the button. If you have millons of records on your DB, its not fair way to bind the whole records on the gridview. Fetch only the particular page records from DB and bind it to gridview. It will increase your performance and wil take very less time.

Upvotes: 2

Related Questions