How to output a string value from a variable of type "Array of System.Data.DataRow"

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 :smiling_face_with_three_hearts:

Use for each loop to iterate Array of datarow
inside for each loop use write line or log message
String.Join(",",row.ItemArray)

Thanks

2 Likes

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

1 Like

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

1 Like

okay…Let me see :blush:

Its Still showing the same error :frowning_face:
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.

2 Likes

TL;DR is that My guess is that either your datarow array has no entries, or your rows don’t have two columns :nerd_face:.

The longer version :grimacing::

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 :slight_smile: .

I hope that helps!

1 Like

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: :smiling_face_with_three_hearts:

1 Like

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!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.