Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to update columns of data table1 from datatable1 if column1 is exist .Now if column1 value is not in datatable1 then add new row and add value in datatable1 from datatable2

What I have tried:

DataSet ds = new DataSet();
DataSet ds1 = new DataSet();

ds1.ReadXml(HttpContext.Current.Server.MapPath("XML/myFile.xml"));
DataTable dt1 = ds1.Tables[0];


//dt1.Merge(dt);
//dt1.AcceptChanges();

//DataView dw = new DataView(dt);
//DataTable dt2 = dw.ToTable(true, "DateTime", "Station", "Max_Temp", "Min_Temp", "Weather_Detail");


for (int i = 0; i <= dt.Rows.Count; i++)
{
for (int j = 0; j <= dt1.Rows.Count; j++)
//{
if (dt1.Rows[i]["Station"].ToString().Contains(dt.Rows[i]["Station"].ToString()))
{
//&& dt1.Rows[i]["Max_Temp"].ToString() == dt.Rows[j]["Max_Temp"].ToString() && dt1.Rows[i]["Station"].ToString() == dt.Rows[j]["Station"].ToString())

dt1.Rows[i]["Max_Temp"] = dt.Rows[i]["Max_Temp"];
dt1.Rows[i]["Min_Temp"] = dt.Rows[i]["Min_Temp"];

dt1.Rows[i]["Weather_Detail"] = dt.Rows[i]["Weather_Detail"];
dt1.Rows[i]["DateTime"] = dt.Rows[i]["DateTime"];

}
else
{
DataRow dr = null;


dr = dt1.NewRow();
dt1.Rows.Add(dr);
dt1.Rows[i]["Station"]= dt.Rows[i]["Station"].ToString();
dt1.Rows[i]["Max_Temp"] = dt.Rows[i]["Max_Temp"];
dt1.Rows[i]["Min_Temp"] = dt.Rows[i]["Min_Temp"];

dt1.Rows[i]["Weather_Detail"] = dt.Rows[i]["Weather_Detail"];
dt1.Rows[i]["DateTime"] = dt.Rows[i]["DateTime"];

}
}
dt1.AcceptChanges();
// }
Posted

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