Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement to check, if datatable is null, then copy the row to datatable otherwise need to append the row to the datatable.
In this code in "else" section it should be appending a row, but here it adding a row without any values in it.In the first column it is appending a text as [System.Data.DataRow].

What I have tried:

C#
if (dtTable == null)
    {
        dtTable = (from DataRow dr in dtDetails.Rows
                   where dr["ID"].ToString() == Id
                   select dr).CopyToDataTable();
    }
    else
    {
       

        dtTable.Rows.Add(from DataRow dr in dtDetails.Rows
        where dr["ID"].ToString() == Id
        select dr)
        // Here in DataTable it adding a new row but without any values.
        // In the first column it is appending a text as [System.Data.DataRow].

    }
Posted
Updated 19-Dec-18 19:20pm
v4

1 solution

By looking at the code, the below code is returning a collections (1 or more)

C#
from DataRow dr in dtDetails.Rows
        where dr["ID"].ToString() == Id
        select dr


The best altrernative, I think is to loop through the object and import the row into the dtTable object.
C#
var misteryObject = from DataRow dr in dtDetails.Rows
        where dr["ID"].ToString() == Id
        select dr;

foreach (DataRow dr in misteryObject )
		   {
			 dtTable.ImportRow(dr);
		   }
 
Share this answer
 
Comments
dipak prodhan 20-Dec-18 3:28am    
Thanks for your reply, but row are not getting imported in DataTable.

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