Hi,
I’ve filtered and stored data from a datatable varible using .Select operation to a variable of Array of System.Data.DataRow type. How can i access the data in the datarow variable and output it as a string? I’m having trouble with the syntax.
Thanks in advance
Use for each loop to iterate Array of datarow
inside for each loop use write line or log message
String.Join(",",row.ItemArray)
Thanks
Thank you.
But can we directly index and output a single element from the variable, without using a loop. Is that possible?
@Midhun_Pc
YourDataRowVar(YourColumnIndexOrName).toString
With DT as your DataTable
and “Column1” as your column name
String.Join(", ", From row In DT.AsEnumerable() Select row.Field(Of String)("Column1"))
If you just want an EnumerableRowCollection of Strings
From row In DT.AsEnumerable() Select row.Field(Of String)("Column1")
And an IEnumerable of Strings with distinct values:
From row In DT.AsEnumerable() Select row.Field(Of String)("Column1") Distinct
I can use the expression datarow(0).Item(1).ToString to read the table data right?
But the error message - Log Message: Index was outside the bounds of the array.
is showing up?? What might be the problem?
Try datarow(1).ToString
if datarow is already a DataRow
okay…Let me see
Its Still showing the same error
p.s. The variable is of type array of datarow
EDIT2: didn’t saw this was an array of datarow
datarow(0).Item(0).ToString
But len.solis is right, you should check first.
TL;DR is that My guess is that either your datarow array has no entries, or your rows don’t have two columns .
The longer version :
datarow(0).Item(1).ToString
If datarow is an array of datarows, that’s the correct syntax.
I just did a little testing. I saw the error you’re getting only in two cases: a) the datarow was empty, or b) the row had only one column.
You can check the how many rows are in your datarow array using
datarow.count
Likewise, you can check how many many columns a row has by using
datarow.itemarray.count
Yes, that uses itemarray. Not a typo .
I hope that helps!
Thanks a lot. You are right. My data row was empty… I had given incorrect coloumn name in .Select operation. Thus it returned an empty datarow. Now it’s all right.
Thanks again:smile:
Good news!
Issues like this can make you sincerely doubt you understand the syntax. I know because I had a very similar issue a couple of days ago. It drove me crazy for a couple of hours! I’m glad my suffering (lol) could help!
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.