Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 30th, 2004, 05:38 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default Binding multiple db tables to dataset

Hello!

I just picked up C# about 3 weeks ago and now I am trying to bind 6 different database tables to one dataset. Is this possible? I've tried several variations of the same code, but I am having the same trouble. The code compiles with no problems, but when I click to to display the datagrid on the ASP.Net webpage, everything shows but the datagrid. It just disappears. Can someone please look at my code and tell me what I'm doing wrong, it will be greatly appreciated:

...
private void BindDGrdRegTime()
        {
            //Create a SqlConnection and SqlDataAdapter object
            string ConnectionString = @"User ID=sa;Initial Catalog=TimeSheet;Data Source=local;";
            SqlConnection cnn = new SqlConnection(ConnectionString);
            SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Account",con);
            //Create a dataset to store the Project Info data.
            DataSet ds1 = new DataSet();
            da1.Fill(ds1, "Account");
            DGrdRegTime.DataSource = ds1.Tables["Account"];

            SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM Emp",con);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2, "Emp");
            DGrdRegTime.DataSource = ds2.Tables["Emp"];

            SqlDataAdapter da3 = new SqlDataAdapter("SELECT * FROM Project",con);
            DataSet ds3 = new DataSet();
            da3.Fill(ds3, "Project");
            DGrdRegTime.DataSource = ds3.Tables["Project"];

            SqlDataAdapter da4 = new SqlDataAdapter("SELECT * FROM Hrs",con);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4, "Hrs");
            DGrdRegTime.DataSource = ds4.Tables["Hrs"];

            SqlDataAdapter da5 = new SqlDataAdapter("SELECT * FROM DayOfWk",con);
            DataSet ds5 = new DataSet();
            da5.Fill(ds5, "DayOfWk");
            DGrdRegTime.DataSource = ds5.Tables["DayOfWk"];

            SqlDataAdapter da6 = new SqlDataAdapter("SELECT * FROM Group",con);
            DataSet ds6 = new DataSet();
            da6.Fill(ds6, "Group");
            DGrdRegTime.DataSource = ds6.Tables["Group"];

            ds1.Merge(ds2);
            ds1.Merge(ds3);
            ds1.Merge(ds4);
            ds1.Merge(ds5);
            ds1.Merge(ds6);


           }
...

*******(*)*******
__________________
*******(*)*******
 
Old August 30th, 2004, 06:04 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you're trying to do is only possible if all the tables have the same or similar schema's. If they do have the same schema's then it would be easier just to write a single SQL SELECT (using UNION ALL) to get all the rows from the relevant tables. From what I can see the tables that you've selected from would not have similar schema's (is that correct).

To get all the information on one page I think you'll need to have one datagrid for each dataset and display the grids one after the other on the page (or tab them or provide links to each grid on the page).

Whichever option you go for I don't think the DataSet.Merge method was intended for the purpose you require.

Kep.

 
Old August 31st, 2004, 04:26 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

Thank you for the reply...after posting this I did some more research and decided to use a different SQL statement in my code. I am using JOIN to connect all the tables then, put them in the dataset. But you are right about the ds.merge. Thanks

*******(*)*******
 
Old August 31st, 2004, 04:31 PM
Authorized User
 
Join Date: Jul 2004
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can however have multiple tables within the same dataset. ds.Tables.Add( dt ); will add a DataTable to the dataset which you can then databind to almost anything.


www.CoderForRent.com
Get A Computer Job!
 
Old September 1st, 2004, 11:56 AM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

Ok..thank you :)

*******(*)*******





Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding a menu with dataset zaghmout ASP.NET 2.0 Professional 1 August 14th, 2007 05:24 AM
Create a report from multiple tables in a Dataset verdun Crystal Reports 4 May 12th, 2005 09:20 AM
dataset with multiple tables Bhavin Crystal Reports 2 October 8th, 2004 09:18 AM
Binding multiple db tables to dataset (union) miguel.ossa C# 1 September 13th, 2004 02:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.