When does a DataSource control "fill" the datatable and how can I get to that data?


I used to (2003) bind my data controls to a dataset, and then I would have to "fill" that dataset.  These concepts all appear to be antiquated now.  Now there are these new "DataSources" - such as the SqlDataSource, XmlDataSource and ObjectDataSource.

These seem very cool, but I'm confused about exactly WHEN they do their thing, and how I get at that data without reduntantly loading all my data multiple times.  In other words...

1) I go to the server explorer and drop the Customers table from the Northwind database on a web form
2) It automatically creates a datagrid and SqlDataSource on my page.
3) I hit F5 and there are the customers - Magic!

In 2003, I could then add this code to PreRender event...
void Page_PreRender(...) {
   Label1.Text = dataSet11.Tables[0].Rows[0]["MyField"];

Obviously I don't have to say SqlDataAdapter1.Fill anymore, cause now I just have a SqlDataSource and it "automatically" fills itself.  How can I access the resulting data table though, as above?  And when does the data become available?

Any guidence appreciated.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think you can get at that stuff in the GridView's DataBound event, or look at the SqlDataSource Selecting event.
kalliopiAuthor Commented:

Thanks.  Do you know if the data available in the "PreRender" event?  And more importantly, if it is, how would I get to it?  If I know that, I can test exactly WHEN it's available, but at this point, I can't even figure out what it's doing.  It seems to just magically fill all of the controls pointing at it.  I just wish I had a better understanding of what was going on under the hood...


Ok, the order (with relevant events) goes like this:


So I guess it depends on which PreRender event you're using. It's not available at Page_PreRender, but it is at GridView_PreRender.

If you need it earlier, I would suggest that you not use a data source control for this. The old way of doing it is still supported.

Why is it important to use the PreRender event?

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

kalliopiAuthor Commented:
I think I'm not making myself clear.

The thing that I'm confused about is this.  The datasource is going to do a "SELECT " statement against the database at some point (presumably in the Selecting) event.  There are 6 events that take place AFTER that point in time, so presumably (based on your previous post) I could write my code in any of those events.  

The part I'm still missing is - WHERE'S the Data?  How do I get to the datatable that was retreived by the select statement?  The data must be available somewhere, but I can't find it, and therefore I can't access/manipulate/retrieve/etc it....

Does my question make sense?
It makes sense, but I cannot find any way to retrieve it directly from the data source control -- it's not exposed at all.

The one thing I think you should know is that if you're using SqlDataSource, you're assuming there is a DataTable but it may be using a DataReader or some other collection. I suppose you could use a reflector to find out exactly how it stores the results of the query, but I haven't done it myself.

If you want full control, then you're going to have to do it yourself. No magic here. All of the old data binding methods still work, you can create the data source yourself and bind it manually, and you can cache it, sort it, page it manually as well. I'm trying to use these new data source controls myself, but I'm finding that the decreased flexibility is outweighing the benefits -- I think that's just the cost of using a "no-code" solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kalliopiAuthor Commented:
Thanks mppeters - I'll have to play around with it and see if I can figure out exactly how to get at the data.  This has been very helpful though.  I appreciate your time.

I object because the answer is that you can't get to the data. It is not exposed at all. And that's what I said in my last comment. I would like my last comment to be accepted as the answer.
kalliopiAuthor Commented:
I agree!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.