Binding to a DataSet with more than one DataTable

G

Guest

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

Guest

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 said:
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 said:
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.
 
E

Eliyahu Goldin

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 said:
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 said:
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 said:
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.
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

Guest

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

Steven Cheng[MSFT]

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

Steven Cheng[MSFT]

You're welcome :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top