Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi experts,

I have got one data table I wanted to check each and every row of that data table
using for each loop ..if that row values contains valid data then i want to insert to a data table called dtvalid data table,If that row contains invalid data then insert to dterror data table.Am using this code but only in 0 index of the new data table is getting updated
here is my code.

C#
foreach (DataRow dr in dsbmVillagedata.Tables[0].Rows)
                         {
 dtValidData = dsbmVillagedata.Tables[0].Clone();
                                DataRow drNew=dtValidData.NewRow();
                                drNew.ItemArray =dr.ItemArray;
                                dtValidData.Rows.Add(drNew);
                                int a = dtValidData.Rows.Count;
}

thanks in advance
(keerthi Kumar)
Posted
Updated 4-Sep-19 23:30pm
v2

try like this..


C#
 dtValidData = dsbmVillagedata.Tables[0].Clone(); // place this line outside the loop.

foreach (DataRow dr in dsbmVillagedata.Tables[0].Rows)
                         {
 dtValidData = dsbmVillagedata.Tables[0].Clone();
                                DataRow drNew=dtValidData.NewRow();
                                drNew.ItemArray =dr.ItemArray;
                                dtValidData.Rows.Add(drNew);
                                int a = dtValidData.Rows.Count;
}
 
Share this answer
 
Comments
Keerthi Kumar(Andar) 10-Jan-14 1:51am    
thanks Karthik...
Karthik_Mahalingam 10-Jan-14 2:22am    
Welcome keerthi 😊
You need to use "importrow" when only you have rows to be copied, without merging the new structure.

simply:

For index = 0 To dtOld.Rows.Count - 1
dtNew.ImportRow(dtReceived.Rows(index))
Next
 
Share this answer
 

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