PC Review


Reply
Thread Tools Rate Thread

Binding to a DataSet with more than one DataTable

 
 
=?Utf-8?B?RGljaw==?=
Guest
Posts: n/a
 
      5th Feb 2007
I have a function that creates a DataSet, populates its many DataTables and
then returns the DataSet.

I want to bind separate controls to each of the DataSet's DataTables, e.g. a
separate GridView for each DataTable.

As far as I can tell, the ObjectDataSource only binds to the DefaultView of
the DataSet's first DataTable. I'd like to know how to bind to the other
DataTables or, if this is not possible, what alternative technique is
recomended?

Note that the overhead required to populate the DataTables is quite high
(and so I wouldn't want to call the function more than once). There are also
strong dependencies between the DataTables such that I wouldn't be able to
deliver them independently in their own separate DataSets.

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      5th Feb 2007
Set GridView.DataMember property to the table name.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Dick" <(E-Mail Removed)> wrote in message
newsBED2EF3-314E-4EEC-9406-(E-Mail Removed)...
>I have a function that creates a DataSet, populates its many DataTables and
> then returns the DataSet.
>
> I want to bind separate controls to each of the DataSet's DataTables, e.g.
> a
> separate GridView for each DataTable.
>
> As far as I can tell, the ObjectDataSource only binds to the DefaultView
> of
> the DataSet's first DataTable. I'd like to know how to bind to the other
> DataTables or, if this is not possible, what alternative technique is
> recomended?
>
> Note that the overhead required to populate the DataTables is quite high
> (and so I wouldn't want to call the function more than once). There are
> also
> strong dependencies between the DataTables such that I wouldn't be able to
> deliver them independently in their own separate DataSets.
>



 
Reply With Quote
 
=?Utf-8?B?RGljaw==?=
Guest
Posts: n/a
 
      5th Feb 2007
This was my first guess too, but the it fails with the exception shown below.

System.ArgumentException: The data source 'ObjectDataSourceAllScoresOnRound'
only supports a single view named 'DefaultView'. You may also leave the view
name (also called a data member) empty for the default view to be chosen.

"Eliyahu Goldin" wrote:

> Set GridView.DataMember property to the table name.
>
> --
> Eliyahu Goldin,
> Software Developer & Consultant
> Microsoft MVP [ASP.NET]
> http://msmvps.com/blogs/egoldin
> http://usableasp.net
>
>
> "Dick" <(E-Mail Removed)> wrote in message
> newsBED2EF3-314E-4EEC-9406-(E-Mail Removed)...
> >I have a function that creates a DataSet, populates its many DataTables and
> > then returns the DataSet.
> >
> > I want to bind separate controls to each of the DataSet's DataTables, e.g.
> > a
> > separate GridView for each DataTable.
> >
> > As far as I can tell, the ObjectDataSource only binds to the DefaultView
> > of
> > the DataSet's first DataTable. I'd like to know how to bind to the other
> > DataTables or, if this is not possible, what alternative technique is
> > recomended?
> >
> > Note that the overhead required to populate the DataTables is quite high
> > (and so I wouldn't want to call the function more than once). There are
> > also
> > strong dependencies between the DataTables such that I wouldn't be able to
> > deliver them independently in their own separate DataSets.
> >

>
>
>

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      5th Feb 2007
If you already have the dataset, bind to it with DataSource property instead
of using ObjectDataSource.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Dick" <(E-Mail Removed)> wrote in message
news:4D176BA3-2765-46F7-BC33-(E-Mail Removed)...
> This was my first guess too, but the it fails with the exception shown
> below.
>
> System.ArgumentException: The data source
> 'ObjectDataSourceAllScoresOnRound'
> only supports a single view named 'DefaultView'. You may also leave the
> view
> name (also called a data member) empty for the default view to be chosen.
>
> "Eliyahu Goldin" wrote:
>
>> Set GridView.DataMember property to the table name.
>>
>> --
>> Eliyahu Goldin,
>> Software Developer & Consultant
>> Microsoft MVP [ASP.NET]
>> http://msmvps.com/blogs/egoldin
>> http://usableasp.net
>>
>>
>> "Dick" <(E-Mail Removed)> wrote in message
>> newsBED2EF3-314E-4EEC-9406-(E-Mail Removed)...
>> >I have a function that creates a DataSet, populates its many DataTables
>> >and
>> > then returns the DataSet.
>> >
>> > I want to bind separate controls to each of the DataSet's DataTables,
>> > e.g.
>> > a
>> > separate GridView for each DataTable.
>> >
>> > As far as I can tell, the ObjectDataSource only binds to the
>> > DefaultView
>> > of
>> > the DataSet's first DataTable. I'd like to know how to bind to the
>> > other
>> > DataTables or, if this is not possible, what alternative technique is
>> > recomended?
>> >
>> > Note that the overhead required to populate the DataTables is quite
>> > high
>> > (and so I wouldn't want to call the function more than once). There are
>> > also
>> > strong dependencies between the DataTables such that I wouldn't be able
>> > to
>> > deliver them independently in their own separate DataSets.
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
Guest
Posts: n/a
 
      5th Feb 2007
Hello Dick,

I assume you are expecting functionality which is not availible when using
ObjectDataSource.
As Eliyahu wrote one possibility is to use DataMember but in this case you
cannot use DataSourceID property and ObjectDataSource. You have to set
DataSource to your DataSet and bind explicitly instead.

If you want to use ObjectDataSource you will probably have to create many
different select methods - it means many ObjectDataSources - to create 1:1
relations among GridViews and ObjectDataSources. You can use Cache to store
your DataSet.

Other possibility is to use one select method with parameter to control
binding to different tables in you dataset. You will have to set this
parameter in ObjectDataSource's Selecting event handler to use select method
on correct table in DataSet. This should be difficult.

Regards,
Ladislav Mrnka

"Dick" wrote:

> I have a function that creates a DataSet, populates its many DataTables and
> then returns the DataSet.
>
> I want to bind separate controls to each of the DataSet's DataTables, e.g. a
> separate GridView for each DataTable.
>
> As far as I can tell, the ObjectDataSource only binds to the DefaultView of
> the DataSet's first DataTable. I'd like to know how to bind to the other
> DataTables or, if this is not possible, what alternative technique is
> recomended?
>
> Note that the overhead required to populate the DataTables is quite high
> (and so I wouldn't want to call the function more than once). There are also
> strong dependencies between the DataTables such that I wouldn't be able to
> deliver them independently in their own separate DataSets.
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      6th Feb 2007
Hello Dick,

As Eliyahu has suggested, one way is to directly assign the DataSet object
to DataBound control(such a gridview)'s Datasource property and set the
proper "DataMember". This is just like the programmatic databinding in
ASP.NET 1.X.

If you want to utilize the ObjectDataSource declarative databinding, you
can consider creating a wrapper class which expose a Select method that
will return the certain DataTable(from internal DataSet object) according
to a given input parameter(select parameter in DataSource control). e.g.

==============================
public class WrapperClass
{
private MyDataSet _dataset;

public WrapperClass()
{
_dataset = new MyDataSet();
//fill in datatables here
}

public DataTable Select(string tablename)
{
return _dataset.Tables[tablename];
}
}

===============================

In the ObjectDataSource, you can supply this parameter through the
DataSource control's "Select" parameter collection(from any available
parameter sources or directly embeded in it)

===============
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="Select"
TypeName="WrapperClass">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="tablename"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
==========================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
=?Utf-8?B?RGljaw==?=
Guest
Posts: n/a
 
      6th Feb 2007
Hi Steven

Thanks for your suggestion but it overlooks the point I made in my original
question, namely that the overhead required to populate the DataTables is
quite high (and so I wouldn't want to call the function more than once).

I worked on this some more last night and think I've found an elegant and
efficient solution. I've described it below for your comment and in case it
might help others with a similar problem.

I made my business rule object stateful as below (I've made the example
losely typed for simplicity):

Public Class BR

Private m_DataSet As DataSet

Public Function GetTable1 As DataTable
Me.PopulateDataSet
Return Me.m_DataSet.Table1
End Function

Public Function GetTable2 As DataTable
Me.PopulateDataSet
Return Me.m_DataSet.Table2
End Function

Private Sub PopulateDataSet
If Me.m_DataSet Is Nothing Then
Me.m_DataSet = New DataSet
'Populate...
End If
End Sub

End Class

Then – on my web form – I overrode the process by which the ObjectDataSource
objects are created as below

Private m_BR As New BR

Protected Sub ObjectCreating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.ObjectDataSourceEventArgs) Handles
ObjectDataSource1.ObjectCreating, ObjectDataSource2.ObjectCreating
e.ObjectInstance = Me.m_BR
End Sub

This ensures that the BR object is created just the once – and therefore the
associated overhead is minimised - yet I'm still able to complete all my
other binding without any more code.

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      7th Feb 2007
Hello Dick,

Yes, you're right. Actually the class I showed in my previous reply just
demonstrate a very basic code logic about using wrapper class. Of course,
it would be necessary to cache the dataset(and its contained datatables).

One thing you can still consider here is the place where you cache the
dataset/datatables instance. From your code, you use a private variable of
the page class to hold the class BR for caching, correct? If so, this
class will still be instanced and destroyed on each page request.
Therefore, if you want to cache the DataSet/Datatables instances across
multiple page requests, you can consider use the ASP.NET Cache storage to
hold it.

http://msdn2.microsoft.com/en-us/library/ms178597.aspx

http://msdn2.microsoft.com/en-us/library/aa478965.aspx

In addition, for your below code
===========
Protected Sub ObjectCreating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.ObjectDataSourceEventArgs) Handles
ObjectDataSource1.ObjectCreating, ObjectDataSource2.ObjectCreating
e.ObjectInstance = Me.m_BR
End Sub

==============

I would also suggest you move it to your custom wrapper class(always check
the dataset/datatable instances from Cache, if not exists, load them from
backend database). How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
=?Utf-8?B?RGljaw==?=
Guest
Posts: n/a
 
      7th Feb 2007
Cool, I'll look at that next, thanks for your help.

"Steven Cheng[MSFT]" wrote:

> Hello Dick,
>
> Yes, you're right. Actually the class I showed in my previous reply just
> demonstrate a very basic code logic about using wrapper class. Of course,
> it would be necessary to cache the dataset(and its contained datatables).
>
> One thing you can still consider here is the place where you cache the
> dataset/datatables instance. From your code, you use a private variable of
> the page class to hold the class BR for caching, correct? If so, this
> class will still be instanced and destroyed on each page request.
> Therefore, if you want to cache the DataSet/Datatables instances across
> multiple page requests, you can consider use the ASP.NET Cache storage to
> hold it.
>
> http://msdn2.microsoft.com/en-us/library/ms178597.aspx
>
> http://msdn2.microsoft.com/en-us/library/aa478965.aspx
>
> In addition, for your below code
> ===========
> Protected Sub ObjectCreating(ByVal sender As Object, ByVal e As
> System.Web.UI.WebControls.ObjectDataSourceEventArgs) Handles
> ObjectDataSource1.ObjectCreating, ObjectDataSource2.ObjectCreating
> e.ObjectInstance = Me.m_BR
> End Sub
>
> ==============
>
> I would also suggest you move it to your custom wrapper class(always check
> the dataset/datatable instances from Cache, if not exists, load them from
> backend database). How do you think?
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      7th Feb 2007
You're welcome

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fix A DataTable named 'Tag2' already belongs to this DataSet.(XML/Datatable) frodenekkoy@hotmail.com Microsoft Dot NET 0 8th Jul 2008 08:14 AM
Copying records from datatable to datatable in dataset tshad Microsoft C# .NET 1 24th Jun 2008 01:39 AM
copying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft Dot NET Framework 3 31st Oct 2003 01:05 PM
copying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft ASP .NET 2 31st Oct 2003 01:05 PM
Ccopying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft Dot NET Framework 2 31st Oct 2003 02:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:52 AM.