how to merge two tables data

Posted by Mahesh_Chs under ASP.NET on 1/31/2013 | Points: 10 | Views : 4288 | Status : [Member] | Replies : 3
hi

i have taken two tables and i had use dt.Merge(dt1); to merge the tables, the tables columns are merging fine but the values which are in the second table is not coming beside the first table, the values are coming after the last row of first table....


Thanks&Regards
Mahesh Chayanam
9700241122




Responses

Posted by: Satyapriyanayak on: 1/31/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://weblogs.asp.net/raminassar/archive/2008/12/14/6804281.aspx
http://dotnetcircle.blogspot.in/2011/08/merging-two-data-tables-in-aspnet.html

If this post helps you mark it as answer
Thanks

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/2/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Merge._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Merge
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sqlda1, sqlda2;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}

}
public void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
sqlda1 = new SqlDataAdapter("select * from table1", con);
sqlda2 = new SqlDataAdapter("select * from table2", con);
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
sqlda1.Fill(ds1, "table1");
sqlda2.Fill(ds2, "table1");
ds1.Merge(ds2);
GridView1.DataSource = ds1;
GridView1.DataBind();

}
}
}

If this post helps you mark it as answer
Thanks

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 2/5/2013 [Member] Bronze | Points: 25

Up
0
Down
let us suppose
dt1 has two coloums like id,name
dt2 has two coloums like age,sex

now you the merged datatable to look loke (id,name,age,sex)
so create a dt with all the four colums and then added the row dynamically to the merged datatable

Mark answer if this helps you


Thanks
Pavan Kumar
Mark Answer if this fits the need

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response