We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

kalliopi
kalliopi asked
on
Medium Priority
516 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
Comment
Watch Question

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

Author

Commented:
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

Commented:
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?


Author

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?
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.

EJ

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

Author

Commented:
I agree!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.