Convert LLBLGenProDataSource2 to DataTable ?

Posts   
 
    
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 18-Feb-2009 15:40:17   

The System.Data.DataTable class has some nice features like .Compute() or .Expression and I would like to use this functionality when working with a LLBLGenProDataSource2. I tried to look through the properties and methods, but I'm not able to find a way to convert it to a DataTable. I don't want to do something like this (= refetching data from DB) though:


DataTable DT = new DataTable();
adapter.FetchTypedList(fields, DT, null);

I would be thankful for some hints how to achieve this, or some advise how I would be able to use already existing similar functionality without converting it to a DataTable.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 18-Feb-2009 16:15:41   

I'm not quite sure I understand your question.

You need some functionality in the DataTable, you can project any entityCollection into DataTabe, or you might fetch a TypedList, typedViewor dynamicList which returns data into a dataTable.

Did you check the EntityCollection's EntityView Projection funcionality?

magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 18-Feb-2009 18:52:32   

Walaa wrote:

I'm not quite sure I undertand your question.

You need some functionality in the DataTable, you can project any entityCollection into DataTabe, or you might fetch a TypedList, typedViewor dynamicList which returns data into a dataTable.

Did you check the EntityCollection's EntityView Projection funcionality?

Thanks for the hints. I have figured out the projection of an IEntityCollection2 (LLBLGenProDataSource2.EntityCollection) into a DataTable. Getting the DataTable from the DynamicList was also trivial, since the DataView.Table property allows direct access to it.

But I'm conceptually stuck with the question of how to transform the ITypedView2 (from LLBLGenProDataSource2.TypedView) into a DataTable (again, without refetching the data from the database again). Maybe you can help me out with a hint here.

In terms of the question: How I can have access to the functionality of DataTable.Compute() or DataTableColumn.Expression (or a similar functionality) when working with a LLBLGenProDataSource2?

In particular the (simplified) code could look something like this:


// ds could be TypedView, Entitycollection, DynamicList, etc.
LLBLGenProDataSource2 ds;

// compute sum of column
DataTable table = ds.???
table.Compute("SUM(columnName)", null);

// create dynamic field
DataTableColumn perc = new DataTableColumn();
perc.Expression = "column1 / column2 * 100";
table.Columns.Add(perc);

EDIT: I have found this article http://forums.asp.net/t/1233982.aspx that suggest to create (extend) the TemplateField control to add a column to the GridView instead of modifying the underlying DataSource/DataTable. +: No pain with adding columns to DataTable (but still separate sorting possibility) -: Still no handy aggregation functions over multiple columns and/or rows. confused

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 19-Feb-2009 09:44:37   

Any TypedView inherits from TypedViewBase which in turn inherits from DataTable.

magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 19-Feb-2009 16:32:17   

Walaa wrote:

Any TypedView inherits from TypedViewBase which in turn inherits from DataTable.

you are right ... amazingly simply if you don't think too complicated! wink


DataTable table = (DataTable)this.TypedView;

magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 26-Feb-2009 22:04:12   

My brain is already grilled, and I think I am more confused now than I was before I started thinking about the topic, but maybe the solution is simple and I just don't see it:

Is there a way I can convert from ResultsetFields (IEntityFields2) to IEntityCollection? Logically seen each of the fields contains information about the Entity, so ... ?

What I'm looking at is to do something like this:


// LLBLGenProDataSource2 ds
// ResultsetFields fields
ds.EntityCollection = ?.convert(fields);

My idea would be that if I could achieve something like this, I could somehow find a way to create an LLBLGenProDataSource2 that is based on a dynamic list of fields from any given entity. Of course I would also take care about fetching the data correctly (relations, etc.), but this seems rather easy.

If I understand it correctly, something similar was discussed in this thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=8471 But the difference is that I'm not interested in editing. I just would like to display data in a grid in a convenient way.

Based on the clues discussed earlier in this thread, I have created my own functionality that extended a DataView and fetched data from the database based on given ResultsetFields. The reason why I was forced to go back to thinking about extending the LLBLGenProDataSource2, is that I would like to achieve something like this: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7434 (in terms of a pulldown or other control driving the select parameters of the data source)

Does anything that I'm posting here make any sense to anybody who doesn't have a grilled brain? wink

EDIT: Could I more precisely achieve what I want by providing functionality on the PerformSelect event, in particular by using something like this?


// ResultsetFields fields
// DataTable dataTable
this.AdapterToUse.FetchTypedList(fields, dataTable, null);

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-Feb-2009 09:03:39   

Is there a way I can convert from ResultsetFields (IEntityFields2) to IEntityCollection? Logically seen each of the fields contains information about the Entity, so ... ?

Yes there is, fetching it into a DataReader, you can project it into an EntityCollection: Fetching DataReaders and projections

But I think the improtant question here would be why? Why do you need to use the LLBLGenProDataSource in the first place?

Why not just fetch the dynamicList and bind it to the grid.

magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 28-Feb-2009 00:20:50   

Walaa wrote:

Is there a way I can convert from ResultsetFields (IEntityFields2) to IEntityCollection? Logically seen each of the fields contains information about the Entity, so ... ?

Yes there is, fetching it into a DataReader, you can project it into an EntityCollection: Fetching DataReaders and projections

But I think the improtant question here would be why? Why do you need to use the LLBLGenProDataSource in the first place?

Why not just fetch the dynamicList and bind it to the grid.

I'll answer your question first: Because I would like to use the functionality of the LGP data source that is superior in many aspects to the one provided by a simple DataTable.

In particular, I am in the process of implementing a GridView that is driven by a pulldown (or more precise it's value) whose value is stored in a session. I can implement this fairly easy with the <SelectParameter> definition in the LGP data source, but I would have to add such a functionality on my own if I don't use it.

Now back to your suggestion: I guess that by the link you mean the section "Projecting Stored Procedure resultset onto entity collection". I don't quite understand, how this solves my problem since the presented example fetches the data from the db and splits it in two EntityCollections. What I need, is one IEntityCollection (independent of the fact to which entity the field belongs).

Maybe I just don't understand the theory, so ... question straight away: The EntityCollection property of the LLBLGenProDataSource is of type IEntityCollection2, this in turn can hold a list of IEntity2. For me this means that it can be based on multiple entities, right? How do I create an IEntityCollection based on more than one Entity? I guess TypedLists are one solution, but if I don't want to create them in the project, but "on the fly"?

Now what you suggest is that I fetch data in the DataReader and convert it into an EntityCollection2. But the EntityCollection2 can only be based on a single entity (type), am I right?

Also the DataProjectorToIEntityCollection2' constructor will have a problem with my dynamic list cause it does not have a factory set, right? And the description of the constructor says: "IEntityCollection2 destination - The destination of the data. It's required that the destination collection has a factory set." Or am I missing something here?

Frankly ... I'm even more confused now ... confused

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 02-Mar-2009 12:19:10   

magic wrote:

Walaa wrote:

Is there a way I can convert from ResultsetFields (IEntityFields2) to IEntityCollection? Logically seen each of the fields contains information about the Entity, so ... ?

Yes there is, fetching it into a DataReader, you can project it into an EntityCollection: Fetching DataReaders and projections

But I think the improtant question here would be why? Why do you need to use the LLBLGenProDataSource in the first place?

Why not just fetch the dynamicList and bind it to the grid.

I'll answer your question first: Because I would like to use the functionality of the LGP data source that is superior in many aspects to the one provided by a simple DataTable.

In particular, I am in the process of implementing a GridView that is driven by a pulldown (or more precise it's value) whose value is stored in a session. I can implement this fairly easy with the <SelectParameter> definition in the LGP data source, but I would have to add such a functionality on my own if I don't use it.

Now back to your suggestion: I guess that by the link you mean the section "Projecting Stored Procedure resultset onto entity collection". I don't quite understand, how this solves my problem since the presented example fetches the data from the db and splits it in two EntityCollections. What I need, is one IEntityCollection (independent of the fact to which entity the field belongs).

Maybe I just don't understand the theory, so ... question straight away: The EntityCollection property of the LLBLGenProDataSource is of type IEntityCollection2, this in turn can hold a list of IEntity2. For me this means that it can be based on multiple entities, right? How do I create an IEntityCollection based on more than one Entity? I guess TypedLists are one solution, but if I don't want to create them in the project, but "on the fly"?

Databinding and for the web it's no different, is about binding lists of data of the same type to a control or a single object to a control. I.o.w.: binding a list of entities of different types to a control is not going to work, as the grid will not have the ability to create columns for every field in every entity type. This is true for web and for winforms, so only work with lists of entities of the same type.

For the rest of your questions, I think it's time to take a step back and reconsider what you really want. As that's a bit blurred for me now, as in: I don't fully understand what you really want so I can't give you a proper advice.

Frans Bouma | Lead developer LLBLGen Pro
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 02-Mar-2009 14:27:30   

Otis wrote:

Databinding and for the web it's no different, is about binding lists of data of the same type to a control or a single object to a control. I.o.w.: binding a list of entities of different types to a control is not going to work, as the grid will not have the ability to create columns for every field in every entity type. This is true for web and for winforms, so only work with lists of entities of the same type.

For the rest of your questions, I think it's time to take a step back and reconsider what you really want. As that's a bit blurred for me now, as in: I don't fully understand what you really want so I can't give you a proper advice.

well ... the starting point (as I think posted in some other thread too) is still the same: I would like to use the functionality of the LLBLGenProDataSource2, but ...

1) The data source should be based on more than one entity/view. 2) I would like to have flexibility on runtime to decide which fields of the given entities/entity are used and how they are joined. So I don't want to create a predefined TypedList or TypedView for every GridView that I want to display.

So from what I understand so far this could be explained as: I would like to use a DynamicList as the base for the LLBLGenProDataSource2.

I'm also not aiming at defining the data source in the aspx file, but binding it programatically in "my own" GridView class (that is an extensions of the System.Web.UI.WebControls.GridView).

I hope this gives a better insight into my problem and I'm looking forward to your comments Otis.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Mar-2009 14:54:47   

LLBLGenProDataSource2 main capability/usage is to declare it at the html/aspx code and let it fetch things for you without writing too much code.

But if you are already writing the code to fetch the data dynamically, then I don't think the LLBLGenProDataSource would add much functionality to you. In fact it will only complicate things.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 02-Mar-2009 15:58:30   

To elaborate a bit about what Walaa said: the datasource is only meant for 2-way databinding. If you don't update elements through the webform, there's no need for a datasourcecontrol, as you can easier write the same code with simply normal binding as in the old days: bind the data directly to the control. Of course you've to write the filtering yourself, but that's not that much of a problem.

Frans Bouma | Lead developer LLBLGen Pro
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 02-Mar-2009 18:01:16   

thanks for the input. As said earlier, the LLBLGenProDataSource2 has some functionality that I would like to use even though I'm not defining it in the aspx file. For example the select parameters that are derived from the state of another control, etc.

I also wanted to develop a gridview & data source functionality combo that is flexible enough, so that it works on both, data sources defined in aspx and programatically, as well as ones based on entities, typed views, typed lists or dynamic lists. I understand though that maybe I was aiming to high and I can't have everything that I wish for.

I will try to give your input more thought ... so although I don't have any additional questions right now, I'm not promising I will not come back with some. wink

magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 02-Mar-2009 20:10:04   

As threatened, I have some more questions after thinking through your feedback. wink

First, would you recommend to extend the SQLDataSource or ObjectDataSource class? I guess not, right? Cause if I would want to do this, I could as well extend the LLBLGenProDataSource2 ... ?

So would you recommend to extend the DataView class and fill the underlying DataTable with the adapter.FetchTypedList()? That's what I was doing so far for the DynamicList class that I have created. I could as well use other adapter methods to fill the DataTable from an entity, typed view, or typed list. But this is a comparably simple questions after having sorted out the idea for a dynamic list.

What is missing in this functionality though, is the basic databinding functionality that is not available, so I would have to add it by myself (to "my data source", as well as to the GridView). Also any kind of select parameters whose values are retrieved from other WebControls would have to be added. The filtering and sorting parameters that might be passed from the GridView seem to be rather simple compared to this.

Would you have any recommendation where to start off conceptually, or could even recommend some examples/tutorials how to do something like this properly?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Mar-2009 07:24:28   

magic wrote:

First, would you recommend to extend the SQLDataSource or ObjectDataSource class? I guess not, right? Cause if I would want to do this, I could as well extend the LLBLGenProDataSource2 ... ?

Right simple_smile

magic wrote:

So would you recommend to extend the DataView class and fill the underlying DataTable with the adapter.FetchTypedList()? That's what I was doing so far for the DynamicList class that I have created. I could as well use other adapter methods to fill the DataTable from an entity, typed view, or typed list. But this is a comparably simple questions after having sorted out the idea for a dynamic list.

DynamicList sounds good from the options you have.

magic wrote:

What is missing in this functionality though, is the basic databinding functionality that is not available, so I would have to add it by myself (to "my data source", as well as to the GridView). Also any kind of select parameters whose values are retrieved from other WebControls would have to be added. The filtering and sorting parameters that might be passed from the GridView seem to be rather simple compared to this.

I think the same. Getting the data dynamically isn't the problem. Coding the capabilities to databind this is where I see you'll have to write some code (!!?). Did you evaluate the cost-benefit of this versus writing specific methods that retrieve the exact info and bind it normally?

David Elizondo | LLBLGen Support Team