473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binding to a DataSet with more than one DataTable

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.

Feb 5 '07 #1
9 10896
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" <Di**@nospam.nospamwrote in message
news:DB**********************************@microsof t.com...
>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.

Feb 5 '07 #2
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" <Di**@nospam.nospamwrote in message
news:DB**********************************@microsof t.com...
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.


Feb 5 '07 #3
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.
Feb 5 '07 #4
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" <Di**@nospam.nospamwrote in message
news:4D**********************************@microsof t.com...
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" <Di**@nospam.nospamwrote in message
news:DB**********************************@microso ft.com...
>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.



Feb 5 '07 #5
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.

Feb 6 '07 #6
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.ObjectDataSourceEventArg s) 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.

Feb 6 '07 #7
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.ObjectDataSourceEventArg s) 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.

Feb 7 '07 #8
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.ObjectDataSourceEventArg s) 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.

Feb 7 '07 #9
You're welcome :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bardo | last post by:
Does anyone knwo how I can bind a textbox to a single dataset/dataTable row? The following will give me only a Column: textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",...
3
by: Just D | last post by:
Hi, If anybody needs to show some data retrieved from the database table, what method is more preferrable? 1. DataSet ds = ...; DataGrid.Data.Source.ds; DataGrid.Bind(); 2. Write a custom...
2
by: Andrew Robinson | last post by:
Is there any way to accomplish two way data binding in a Details View with a DataSet or DataTable as the DataSource. All I want is to get an updated DataSet or DataTable back from the...
2
by: Alex S Moore | last post by:
I built a dataset with two datatables and added the relation. The example in the help text for datatable class seemed sufficient. Binding a datalist using the dataset for datasource and the...
0
by: mark | last post by:
What I have is three forms. Form one with buttons 1, 2 and 3; and forms 2 and 3 with datagrids. Button 1 populates arrays a(i,j) and b(i,j) with values. Button 2 creates a datatable containing...
8
by: Richard L Rosenheim | last post by:
I have a dataset containing a parent table related to a child table. The child table contains an ID field (which is configured as autonumber in the datatable), the ID of the parent, plus some...
2
by: pwh777 | last post by:
I need help in understanding the DataAdapter Fill method and how it relates to the binding to controls on a form. I have a table called tbl_CID_XRef on SQL Server. I have written as a test the...
0
by: A.J | last post by:
There is a : 1.DataGrid: Columns-->firstName,LastName,EMail,PhoneNumber 2.GroupBox: controls-->4 TextBoxes(firstName,LastName,EMail,PhoneNumber) 3.Both DataGrid and GroupBox are binded to the...
10
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi, Problem: How can I databind (or put) a SqlServer query's row return of 115,000 items into a ComboBox quickly? Not much longer than a matter of seconds, that is... Scenario: I am...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.