Solved

using two data sources for one data list

Posted on 2006-06-26
4
237 Views
Last Modified: 2012-05-05
ASP.Net 2.0 and C# 2.0 only please:

I have two data sources, different tables, same structure in the output (two sets of addresses), that I want to display in one final DataList on my aspx page.

Is there an easy way to go about doing this? I've been googling for a half hour with no luck.
0
Comment
Question by:Paul Kahl
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
JocGaurav earned 500 total points
ID: 16985522
0
 
LVL 3

Author Comment

by:Paul Kahl
ID: 16985731
The example in the first link looks approachable, with one small question from me.

I have two stored procedures, and two calls to the database (these sprocs are used singly in some places, so they have their own datasets in my database object layer), so I have two functions in my DBLayer to call:

getPrimaryAccountAddress
getAccountAddressList

They both contain the same columns (cherry picked from larger tables with many unmatched columns). I want to just spew out both of them.

I'm a bit of a newbie to .Net (This would be my third week using it full-time), so I'm not really sure how to use those two data sources in this (the linked code you mentioned):

private void MergeDataTable()
    {
        DataTable dtOne = new DataTable("MyFirstTable");
        dtOne.Columns.Add("Number");

        DataTable dtTwo = new DataTable("MySecondTable");
        dtTwo.Columns.Add("Number");

        // Populate the first table
        for (int i = 1; i <= 10; i++)
        {
            DataRow row = dtOne.NewRow();
            row["Number"] = "Data" + i;
            dtOne.Rows.Add(row);
        }

        // Populate the second table
        for (int i = 11; i <= 20; i++)
        {
            DataRow row = dtTwo.NewRow();
            row["Number"] = "Data" + i;
            dtTwo.Rows.Add(row);
        }

        // Now merge the two tables /* ONLY ONE LINE OF CODE COOL RIGHT! */
        dtOne.Merge(dtTwo);
        GridView1.DataSource = dtOne;
        GridView1.DataBind();
    }

0
 
LVL 3

Author Comment

by:Paul Kahl
ID: 16985874
Ok - I figured out how to hook up my two data sources, but I can't get it to just output the rows and colums (in this case 3 rows, 5 columns).

My code:

public void getPortalAccountAddressList(int intAxaptaCustNum)
    {
        DataTable dtOne = new DataTable();
        dtOne = AxaptaWeb.getPrimaryAccountAddress(intAxaptaCustNum);

        DataTable dtTwo = new DataTable();
        dtTwo = AxaptaWeb.getAccountAddressList(intAxaptaCustNum);

        // Populate the first table
        for (int i = 1; i <= 1; i++)
        {
            DataRow row = dtOne.NewRow();
            dtOne.Rows.Add(row);
        }

        // Populate the second table
        for (int i = 11; i <= 20; i++)
        {
            DataRow row = dtTwo.NewRow();
            dtTwo.Rows.Add(row);
        }

        // Now merge the two tables /* ONLY ONE LINE OF CODE COOL RIGHT! */
        dtOne.Merge(dtTwo);
       
        dlCPAccountAddressList.DataSource = dtOne;
        dlCPAccountAddressList.DataBind();
    }
0
 
LVL 3

Author Comment

by:Paul Kahl
ID: 16985935
OK - answered my own question on that last point. The following code correctly accesses the data the way I'm wanting (played with the looping a bit on the data population). For those who want to have the first table output correctly, change "int i = 2" to "int i = 1". I set it to 2 because I know I will ALWAYS only have 1 record, so I set it up to basically not repeat the rows in the first table.

public void getPortalAccountAddressList(int intAxaptaCustNum)
    {
        DataTable dtOne = new DataTable();
        dtOne = AxaptaWeb.getPrimaryAccountAddress(intAxaptaCustNum);
        int dtOneRowCount = dtOne.Rows.Count;

        DataTable dtTwo = new DataTable();
        dtTwo = AxaptaWeb.getAccountAddressList(intAxaptaCustNum);
        int dtTwoRowCount = dtTwo.Rows.Count;

        // Populate the first table
        for (int i = 2; i <= dtOneRowCount; i++)
        {
            DataRow row = dtOne.NewRow();
            dtOne.Rows.Add(row);
        }

        // Populate the second table
        for (int i = 11; i <= dtTwoRowCount; i++)
        {
            DataRow row = dtTwo.NewRow();
            dtTwo.Rows.Add(row);
        }

        // Now merge the two tables /* ONLY ONE LINE OF CODE COOL RIGHT! */
        dtOne.Merge(dtTwo);
       
        dlCPAccountAddressList.DataSource = dtOne;
        dlCPAccountAddressList.DataBind();
    }

Thanks, JocGaurav. Your help was timely and accurate, and just what I needed.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question