Object cannot be cast from DBNull to other types for Double Data Type

后端 未结 3 502
暗喜
暗喜 2021-01-27 06:39

I am seeing error when there is null value in database for any cell, Object cannot be cast from DBNull to other types. - Asp.net grid view



        
3条回答
  •  一生所求
    2021-01-27 06:52

    Then check if it's DBNull or null:

    protected string PercentageChange(object client_Price, object second_price)
    {
        if(DBNull.Value.Equals(client_Price) || client_Price == null || DBNull.Value.Equals(second_price) || second_price == null)
            return "N/A"; // or whatever
    
        double price1 = Convert.ToDouble(client_Price);
        double price2 = Convert.ToDouble(second_price);
        double percentagechange = ((price1 - price2) / price2) * 100;
        return percentagechange.ToString();
    } 
    

提交回复
热议问题