?
Solved

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

Posted on 2006-03-21
9
Medium Priority
?
478 Views
Last Modified: 2012-08-14
Hi,

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(...) {
   SqlDataAdapter1.Fill(dataSet11);
   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.

EJ
0
Comment
Question by:kalliopi
  • 4
  • 4
8 Comments
 
LVL 8

Expert Comment

by:mppeters
ID: 16261606
I think you can get at that stuff in the GridView's DataBound event, or look at the SqlDataSource Selecting event.
0
 
LVL 6

Author Comment

by:kalliopi
ID: 16265353
Hi,

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...

Thoughts?

EJ
0
 
LVL 8

Expert Comment

by:mppeters
ID: 16269645
Ok, the order (with relevant events) goes like this:

Page_Load
Page_PreRender
DataSource_Selecting
DataSource_Selected
GridView_DataBinding
GridView_RowCreated
GridView_RowDataBound
GridView_DataBound
GridView_PreRender

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?


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:kalliopi
ID: 16270458
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?
0
 
LVL 8

Accepted Solution

by:
mppeters earned 2000 total points
ID: 16270672
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.
0
 
LVL 6

Author Comment

by:kalliopi
ID: 16274786
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.

EJ
0
 
LVL 8

Expert Comment

by:mppeters
ID: 16543820
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.
0
 
LVL 6

Author Comment

by:kalliopi
ID: 16544772
I agree!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question