Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npgsql : Selecting a null data throws exception with error "Column is Null"

I am running npgsql v3.7 with .NetCore on Ubuntu.

When I execute a select query and a cell in any row in the results is null, an exception is thrown with the error message "Column is null".

I am having to work around this by putting every column in the select clause inside a case statement which tests for NULL

"CASE WHEN " + fieldName + " IS NULL THEN '' ELSE " + fieldName + " END "

This seems a bit extreme and should not be necessary. Has anyone else come across this.

Thanks.

like image 463
Hughgo Avatar asked Oct 22 '25 06:10

Hughgo


1 Answers

You are probably trying to read the column like this:

using (var reader = cmd.ExecuteReader()) {
  reader.Next();
  var o = reader.GetString(0);   // Or any other of the Get methods on reader
  ...
}

This code will fail if the column contains a null, and is the expected behavior. In ADO.NET, you need to check for a null value with reader.IsDBNull(0) before actually getting the value. That's just how the database API works.

like image 65
Shay Rojansky Avatar answered Oct 23 '25 19:10

Shay Rojansky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!