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

前端 未结 11 1933
青春惊慌失措
青春惊慌失措 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:19

    String.Concat transforms DBNull and null values to an empty string.

    public string GetCustomerNumber(Guid id)
    {
       object accountNumber =  
              (object)DBSqlHelperFactory.ExecuteScalar(connectionStringSplendidCRM, 
                                    CommandType.StoredProcedure, 
                                    "spx_GetCustomerNumber", 
                                    new SqlParameter("@id", id));
    
        return String.Concat(accountNumber);
    
     }
    

    However, I think you lose something on code understandability

提交回复
热议问题