Reputation: 125
Actually, i have two data tables. Here is the code for reference. Actually Relationship holds the sub string values(Code not mentioned here). So i am splitting the data's to store in two table's based on the condition.
if (child.Relationship.Contains("P"))
{
var newRow = dataTable.NewRow();
newRow["EmployeeNo"] = child.EmployeeNo;
newRow["FirstName"] = child.FirstName;
newRow["MiddleName"] = child.MiddleName;
newRow["LastName"] = child.LastName;
newRow["FullName"] = child.FullName;
newRow["MemberIc"] = child.MemberIc;
dataTable.Rows.Add(newRow);
}
else
{
var newRow1 = dataTable1.NewRow();
newRow1["EmployeeNo"] = child.EmployeeNo;
newRow1["FirstName"] = child.FirstName;
newRow1["MiddleName"] = child.MiddleName;
newRow1["LastName"] = child.LastName;
newRow1["FullName"] = child.FullName;
newRow1["MemberIC"] = child.MemberIC;
dataTable1.Rows.Add(newRow1);
}
So, whenever the MemberIc in the second data table is empty. I need to copy the MemberIc from first data table(only if EmployeeNo Matches) based on the EmployeeNo. How can i copy the value ? Any help appreciated. Thanks in advance !!!
Upvotes: 1
Views: 1228
Reputation: 6891
Here you go.
var rows = datatable.Select("EmployeeNo = " + child.EmployeeNo);
var memberIC = 0;
if(rows.length > 0)
{
memberIC = rows[0].Field<int>("memberIC");
}
if (child.Relationship.Contains("P"))
{
var newRow = dataTable.NewRow();
newRow["EmployeeNo"] = child.EmployeeNo;
newRow["FirstName"] = child.FirstName;
newRow["MiddleName"] = child.MiddleName;
newRow["LastName"] = child.LastName;
newRow["FullName"] = child.FullName;
newRow["MemberIc"] = child.MemberIc;
dataTable.Rows.Add(newRow);
}
else
{
var newRow1 = dataTable1.NewRow();
newRow1["EmployeeNo"] = child.EmployeeNo;
newRow1["FirstName"] = child.FirstName;
newRow1["MiddleName"] = child.MiddleName;
newRow1["LastName"] = child.LastName;
newRow1["FullName"] = child.FullName;
newRow1["MemberIC"] = child.MemberIC == 0 ? memberIC : child.MemberIC;
dataTable1.Rows.Add(newRow1);
}
This has nothing to do with C#, ADO.NET or datatable. This is a simple logic of what to check and where to check.
Upvotes: 1