Click here to Skip to main content
15,887,293 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please Correct me! Here I am trying too create a third data table merging two other tables having same patient names.

This is the .aspx file code

C#
<%@ Page Title="" Language="C#" MasterPageFile="~/Shopping Cart Pages/MasterPage_ShoppingCart.master" AutoEventWireup="true" CodeFile="DemoJoin.aspx.cs" Inherits="DemoJoin" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style48
        {}
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="Panel4" runat="server" CssClass="style48" Height="560px">
        <br />
        Table:1<asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
        Table 2<br />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
        <br />
        Table 3<br />
        <asp:GridView ID="GridView3" runat="server">
        </asp:GridView>
    </asp:Panel>
</asp:Content>


This is the .aspx.cs file code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class DemoJoin : System.Web.UI.Page
{
    DataTable dt1 = new DataTable();
    DataTable dt2 = new DataTable();
    DataTable dt3 = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        dt1.Columns.Add("Dosage", typeof(int));
        dt1.Columns.Add("Drug", typeof(string));
        dt1.Columns.Add("Patient", typeof(string));
        dt1.Columns.Add("Date", typeof(DateTime));

        //
        // Here we add five DataRows.
        //
        dt1.Rows.Add(25, "Indocin", "David", DateTime.Now);
        dt1.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        dt1.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        dt1.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        dt1.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

        dt2.Columns.Add("Age", typeof(int));
        dt2.Columns.Add("City", typeof(string));
        dt2.Columns.Add("Patient", typeof(string));

        //
        // Here we add five DataRows.
        //
        dt2.Rows.Add(23, "India", "Jnui");
        dt2.Rows.Add(50, "Brussels", "Sam");
        dt2.Rows.Add(10, "Uzbekistan", "Christoff");
        dt2.Rows.Add(21, "Scandinevia", "Mary");
        dt2.Rows.Add(19, "Ireland", "Melanie");

        GridView1.DataSource = dt1;
        GridView1.DataBind();

        GridView2.DataSource = dt2;
        GridView2.DataBind();

        dt3 = MergeTables(dt1, dt2);

        GridView3.DataSource = dt3;
        GridView3.DataBind();

    }

    protected DataTable MergeTables(DataTable dat1, DataTable dat2)
    {
        DataTable dat3 = new DataTable();

        var row = from ddr1 in dat1.AsEnumerable()
                  join ddr2 in dat2.AsEnumerable()
                  on ddr1.Field<string>("Patient") equals ddr2.Field<string>("Patient")

                  select dat3.LoadDataRow(new object[]
                  {
                    ddr1.Field<int>("Dosage"),
                    ddr1.Field<string>("Drug"),
                    ddr1.Field<string>("Patient"),
                    ddr1.Field<DateTime>("Date"),

                    ddr2.Field<int>("Age"),
                    ddr2.Field<string>("City"),
                  }, false);

        row.CopyToDataTable();
        return dat3;
    }
}
Posted
Updated 7-May-14 8:06am
v2
Comments
Maciej Los 7-May-14 14:23pm    
In which line?
J. Chatterjee 7-May-14 14:46pm    
Line 65: select dat3.LoadDataRow(new object[]
Maciej Los 7-May-14 15:46pm    
Sorry, i do not see this line in your code.
J. Chatterjee 7-May-14 15:42pm    
Please show me the correct format to join two data tables.. is what I'm doing correct?
J. Chatterjee 7-May-14 15:37pm    
Someone please help.. :( I am stucked with this problem.. :(

1 solution

dat3 needs to have columns. Currently it does not have any columns which is why you get the error. Add the columns first and then call LoadDataRow.

http://msdn.microsoft.com/en-us/library/kcy03ww2(v=vs.110).aspx[^]
 
Share this answer
 
Comments
J. Chatterjee 7-May-14 15:52pm    
Thank you very very much.. :) :) you saved me ^_^ ^_^
ZurdoDev 7-May-14 15:53pm    
No problem. You're welcome.
Maciej Los 7-May-14 16:17pm    
Hawk eye ;)
+5!
ZurdoDev 7-May-14 16:20pm    
Wasn't he on the A-team?
Maciej Los 7-May-14 16:24pm    
A-Team rules!
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900