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
<%@ 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
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));
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));
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;
}
}