DBNULL value question

Using VB.NET, I perform a trim function on a SQL server table field   trim(row1(''account'')).

When the field is empty I get an error: cast from 'DBNULL'  to String is not valid.

I want to be able to perform that Trim function without testing each single field if it s equal to DBNULL or not.

That s because I have many fields in the same string generated big string and I wanna insert some spaces inside that string:

 trim(row1(''account'')) & ''  ''   &  (row1(''address''))  & ''   ''    &  trim(row1(''FirstName''))  & (row1('LastName')).

 Thanks a lot




Answer this question

DBNULL value question

  • Zath

    This is not posible, you can use IIf :


    IIf( row1("account") <> DBNull.Value, trim(row1("account")), string.Empty) & IIf( row1("address") <> DBNull.Value, trim(row1("address")), string.Empty)
     



  • Vladimir Chtepa

    Try calling ToString() on each column like:

    trim(row1("account").ToString()) ...

    DBNull.ToString() returns an empty string, which shouldn't cause an exception when trim attempts to trim it.

    You may just want to create a reusable function that will do most of that for you. This is in C#, but you can create the equivalent in VB:

    public string GetString(object item)

    {

    return item.ToString().Trim();

    }

    Then you could just do:

    GetString(row1("account")) + " " + GetString(row1("address")) + ...

    There are other ways to do this as well.

    Regards,

    Dave



  • Marco Foco

    thanks a lot, works perfectly


  • DBNULL value question