Unable to cast object of type 'System.DBNull' to type 'System.String`

前端 未结 11 2049
青春惊慌失措
青春惊慌失措 2020-11-22 06:45

I got the above error in my app. Here is the original code

public string GetCustomerNumber(Guid id)
{
     string accountNumber = 
          (string)DBSqlHel         


        
11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 07:12

    Since I got an instance which isn't null and if I compared to DBNULL I got Operator '==' cannot be applied to operands of type 'string' and 'system.dbnull' exeption, and if I tried to change to compare to NULL, it simply didn't work ( since DBNull is an object) even that's the accepted answer.

    I decided to simply use the 'is' keyword. So the result is very readable:

    data = (item is DBNull) ? String.Empty : item

提交回复
热议问题