11

I am getting the error in the inner foreach while using Select in datatable.

Here is the code I tried so far

foreach (DataRow drOuter in dtLogic.Select("Name='>' OR Name='='"))
{
     foreach (DataRow drInner in dtLogic.Select("ParentId=" + Convert.ToInt64(drOuter["Id"]) + ""))
     {  

     }
}

where Convert.ToInt64(drOuter["Id"]) have the value 2107362180 when I checked in Immediate Window.
Then why does it throw the below error?

enter image description here

Shilpa Praneesh
  • 255
  • 2
  • 4
  • 12

1 Answers1

18

You should check for strings and not for numbers so insert single quotes in query expr='string'

foreach (DataRow drInner in dtLogic.Select("ParentId='" + Convert.ToInt64(drOuter["Id"]) + "'"))
{  

}

after this edit you can replace as @Christos answer says

Convert.ToInt64(drOuter["Id"])

with

drOuter["Id"].ToString()
faby
  • 7,394
  • 3
  • 27
  • 44
  • 1
    Yes. I had missed single quotes inside datatable.Select(). Since the column contains only int values, still Convert.ToInt64(drOuter["Id"]) is working. Thanks a lot @faby – Shilpa Praneesh Jan 05 '15 at 11:47