DBNull to type String is not valid

Hey everyone,

dt.AsEnumerable().Where(Function(x) CStr(x(“INVOICE”)).toString.Equals(invoice.trim.ToString)).CopyToDataTable

Above code is the code I run to filter the datatable where the row values are equal to invoice. I get the dbnull to type String error when there is a blank cel in the “INVOICE” row, its completely normal because it cannot convert null to String so I get the reason why I am getting the error message. BUT, the thing is I need it to be able to convert it as “” empty string. Is there any way of making this work?

Thank you.

2 Likes

invoke code … use it to check firstly if every case in every row contains data, if yes then keep the loop if not assign =“” to the cell which will be empty. then after this u can use the code above to do ur action.
Hope its helpful.

Try to assign:
mystring = “” + CStr(myvar)

1 Like

give a try on:

(From d in dt.AsEnumerable
Where Not (isNothing(d(“INVOICE”)) OrElse String.IsNullOrEmpty(d(“INVOICE”).toString.Trim))
Where d(“INVOICE”).toString.Trim.Equals(invoice.trim.ToString)
Select d).CopyToDataTable

Kindly note:
x(“INVOICE”)).toString returns a string. Prepended CStr converts a string into string and can be ommited

Sequence: The source contains no DataRows. This is the error code I got

" dt.AsEnumerable.Select(Function ( r ) r(“Invoice Number”).toString.Trim).Distinct().toArray " I use this to get the distinct invoices to filter acording to those numbers. So when I do this in a scenario where there is an empty invoice the array becomes as (“something”,“”), so ı process “something” its flawless but when the invoice is “” I got the above error using ur filter.

we can handle empty filter result by the pattern from below

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