locked
Converting dataset to list incorrect type RRS feed

  • Question

  • User781764631 posted

    I am trying to convert a dataset into a list but for some reason its failing when a datarow column is null when going in a type of double.. It  appears to be this line that is giving me trouble any ideas of what I could to solve it.

    _supplier.supplier_price = (double)dr["SupplierPrice"];

    public List<SuppliersDetails> copyDstoSupplierDetails(DataSet ds)
     {
          int table = Convert.ToInt32(ds.Tables.Count);// count the number of table in dataset
          for (int i = 0; i < table; i++)// set the table value in list one by one
              {
                    foreach (DataRow dr in ds.Tables[i].Rows)
                    {
                        SuppliersDetails _supplier = new SuppliersDetails();
                        _supplier.code = (string)dr["Product Code"];
                        _supplier.long_description = (string)dr["Long Description"];
                         _supplier.supplier_price = (double)dr["SupplierPrice"];
                      
    
                        _supplierDetails.Add(_supplier);
                    }
                }
                return _supplierDetails;
     }

    My generic list collection is very simple

     public class SuppliersDetails
        {
            public string code { get; set; }
            public string name { get; set; }
            public string warehouse { get; set; }
            public string long_description { get; set; }
            public string unit_code { get; set; }
            public double sterling { get; set; }
            public double nsterling { get; set; }
    
    
            public double neuro { get;set;}
            public double euro { get; set; }
            public double supplier_price { get; set; }
            public double nsupplier_price { get; set; }
    
            public string description { get; set; }
    
            public SuppliersDetails()
            {
    
    
            }
            public  SuppliersDetails (string Code,string Productname,string LongDescription,string UnitCode,double Sterling,double NSterling,double Euro,double Neuro,double Supplier_price ,double NSupplier_price)
            {
    
                this.code = code;
                this.name = Productname;
                this.long_description = LongDescription;
                this.unit_code = UnitCode;
                this.sterling = Sterling;
                this.nsterling = NSterling;
                this.neuro = Neuro;
                this.euro = Euro;
                this.supplier_price = Supplier_price;
                this.nsupplier_price = NSupplier_price;    
    
            }

    Monday, July 10, 2017 10:41 AM

All replies

  • User-1227629429 posted

    Try making 

    supplier price nullable and then setting it to null if the value returned from the db is null instead fo trying to cast null to a double.

    public double? supplier_price { get; set; }
    _supplier.supplier_price = dr["SupplierPrice"] != null ? (double)dr["SupplierPrice"] : null;
    Monday, July 10, 2017 2:06 PM
  • User-1220080607 posted

    Hi

    use row.Isnull('Column Name') to check for null Values

    _supplier.supplier_price = !dr.IsNull("SupplierPrice")?Convert.ToDouble(dr["SupplierPrice"]):0;

    Monday, July 10, 2017 6:11 PM
  • User-832373396 posted

    Hi david39ni,

    _supplier.supplier_price = (double)dr["SupplierPrice"];

    Here is my way which on my real project; 

    double y;
    Double.TryParse("123", out y); // int.TryParse ,etc else.

    So

    Double.TryParse((double)dr["SupplierPrice"], out _supplier.supplier_price);  // it will fill into _supplier.supplier_price ;

    it could keep you no error;

    With Regards,

    Angelia Jolie

    Wednesday, July 12, 2017 9:15 AM