you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 insightful - 3 fun4 insightful - 2 fun5 insightful - 3 fun -  (2 children)

The DBNull type doesn't implement the ToString method, you can do a type check for DBNull and return an empty string for that case.

if(some_db_value == DBNull.Value) { string.Empty } else { some_db_value.ToString() }

🙏 Namaste

[–]TooMuchClay 3 insightful - 3 fun3 insightful - 2 fun4 insightful - 3 fun -  (1 child)

Hmmm, looks more like an unchecked cast? i.e.

var foo = new DBNull();

var bar = (string)foo; // am ded, crash

// x x

// ~

[–][deleted] 2 insightful - 3 fun2 insightful - 2 fun3 insightful - 3 fun -  (0 children)

Yeah, you are right, it looks like its an invalid cast exception. Thankfully a type check and an empty string would still fix this