Convert row to array


#1

I’ve been trying to convert a row to an array of strings with all sorts of tricks, but haven’t gotten it to work. I have cells wih null values, so I keep getting index out of range errors,


#2

May not be the efficient solution, but you can try below way

//to remove nulls
foreach( column c in dt.Columns)
{
  if(row(c) is Nothing
  row(c) = string.Empty
}

string.Join(",", row.ItemArray)

#3

Hi Vvaidya,

I think String.Join would return a String, not an array of string.

@path

I don’t think the reason you have out of range exception is due that you have null values on datarow.

Also datatable cannot contain null values but only dbnull’s as far as i know

Regarding converting to array of string i would go for something like that with an assign:

If you row contains other types than string:
strArray = row.ItemArray.OfType(Of Object).Select(Function(x) x.ToString).ToArray

If you row contains only strings
strArray = rows(1).ItemArray.OfType(Of String).ToArray simply will do.

Finally if you really would like to replace your dbnull (which would simply be missing from the array, but not throw exception during the process), you could use that form.

strArray = row.ItemArray.OfType(Of Object).Select(Function(x) If(IsDbNull(x),String.empty,x.ToString)).ToArray

Cheers.