Hi,
i have 4 datatable. I need to make it one single table and I need to keep all duplicate columns also.Merge methode missing some data.I need to join all tables and to a single one and should keep duplicate columns also.I used below codes but i am missing some coloumn (duplicates also)
C#
DataTable dtFinal = new DataTable();
for (int i = 0; i < resultDS.Tables.Count; i++)
{
if (i == 0)
dtFinal = resultDS.Tables[i].Copy();
else
dtFinal.Merge(resultDs.Tables[i]);
}
var ds = new DataSet();
ds.Tables.Add(dtFinal);
Sachin SinghRank #66 · 26.2K reputationPosted Feb 2, 2022, 11:11 AM EST
You need to Call the Copy() method in each iteration if you want to preserve the tableDataTable dtFinal = new DataTable(); for (int i = 0; i { dtFinal = resultDS.Tables[i].Copy(); dtFinal.Merge(resultDs.Tables[i]); } var ds = new DataSet(); ds.Tables.Add(dtFinal);Sujeet RamanRank #1,329 · 1.3K reputationPosted Feb 2, 2022, 12:24 PM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Feb 2, 2022, 12:59 PM EST
not working as expected.. here am getting only 3 columns.I dont need to remove duplicates.below am trying
var Dset = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("Age", typeof(int));
// Create a DataRow, add Name and Age data, and add to the DataTable
DataRow dr1 = dt1.NewRow();
dr1["Name"] = "Mohammadsd"; // or dr[0]="Mohammad";
dr1["Age"] = 24; // or dr[1]=24;
dt1.Rows.Add(dr1);
Dset.Tables.Add(dt1);
DataTable dt2 = new DataTable();
dt2.Columns.Add("Name", typeof(string));
dt2.Columns.Add("Age", typeof(int));
// Create a DataRow, add Name and Age data, and add to the DataTable
DataRow dr2 = dt2.NewRow();
dr2["Name"] = "Mohammad"; // or dr[0]="Mohammad";
dr2["Age"] = 24; // or dr[1]=24;
dt2.Rows.Add(dr2);
Dset.Tables.Add(dt2);
DataTable dt3 = new DataTable();
dt3.Columns.Add("Name", typeof(string));
dt3.Columns.Add("sex", typeof(int));
// Create a DataRow, add Name and Age data, and add to the DataTable
DataRow dr3 = dt3.NewRow();
dr3["Name"] = "last"; // or dr[0]="Mohammad";
dr3["sex"] = 24; // or dr[1]=24;
dt3.Rows.Add(dr3);
Dset.Tables.Add(dt3);
DataTable dtFinal = new DataTable("dtEmployee");
for (int i = 0; i < Dset.Tables.Count; i++)
{
dtFinal.Merge(Dset.Tables[i]);
}
var ds = new DataSet();
ds.Tables.Add(dtFinal);//here i need to get 6 colms
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 2, 2022, 1:04 PM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Feb 2, 2022, 1:13 PM EST
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 2, 2022, 1:22 PM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Feb 2, 2022, 1:40 PM EST
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 2, 2022, 3:53 PM EST
Adrian Nufc Tether38 reputationPosted Feb 2, 2022, 10:49 AM EST
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 2, 2022, 11:51 AM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts