Simplified way to convert return DB Null values to empty strings RRS feed

  • Question

  • Hi all,

    I am just wondering if there is a simplied way to convert all Database values that are returned as NULL. At the moment I do a check each and everytime I attach data to a control, For example I have...

    lblReceivablesFromGroupCompanies1.Text = (dsAccounts.Tables[0].Rows[0]["RECIEVABLES_FROM_GROUP_COMPANI"] != DBNull.Value) ? string.Format(currencyFormat, "{0:C}", Convert.ToInt32(dsAccounts.Tables[0].Rows[0]["RECIEVABLES_FROM_GROUP_COMPANI"])) : string.Empty;

    ...as you can see it is quite long line of code, and this is repeated a couple of hundred times. I am looking to pull out common tasks.

    Thanks in advance.
    Wednesday, February 6, 2008 12:02 AM

All replies

  • There is unified way to do this, sine data types are different and what would be definition of NULL conversion into numeric value or string? Is it 0? Is it blank string or is it something else? But you could simplify your life if you select data from database. For example, in a case if you select data from SQL Server database you could use ISNULL function and return some value instead of NULL, like


    SELECT MyColumn, ISNULL(MyAnotherNumericColumn, 0) AS MyAnotherNumericColumn, ISNULL(MyStringColumn, '') AS MyStringColumn FROM MyTable .....


    Wednesday, February 6, 2008 12:33 AM