0

The above code throws an error that Object cannot be cast from DBNull to other types. I tried putting a condition of if else but that does not make sense perhaps because my problem is i will make If, else condition which can be check If var_P_Amount value is null then lbl_Purchasing_Amount.Text = "0"; Please help

private void Get_Purchasing_Amount()
            {
                try
                {
                    double var_P_Amount = 0;
                    int var_C_Code = 0;
                    string query = "select c_code as 'code' from `db_vegetable`.`tbl_payment_master`";
                    DataTable dt_C_Code = method_Class.method_Class.FetchRecords(query);
                    if (dt_C_Code.Rows.Count > 0)
                    {
                        for (int i = 0; i <= dt_C_Code.Rows.Count; i++)
                        {
                            var_C_Code = Convert.ToInt32(dt_C_Code.Rows[i]["code"]);
                            if (var_C_Code.Equals(Convert.ToInt32(txt_Customer_Code.Text)))
                            {
                                string get_P_Amount;
                                if (check_All.Checked.Equals(true))
                                {
                                    get_P_Amount = "SELECT `purchasing` AS 'purchasing' FROM `db_vegetable`.`tbl_payment_master` WHERE `c_code` = " + txt_Customer_Code.Text + "";
                                }
                                else
                                {
                                    get_P_Amount = "SELECT SUM(t_price) as 'purchasing' FROM `db_vegetable`.`tbl_order_details` WHERE `c_code` = " + txt_Customer_Code.Text + " AND (`date` BETWEEN '" + txt_From_Date.Text + "' AND '" + txt_To_Date.Text + "')";
                                }
                                DataTable dt = method_Class.method_Class.FetchRecords(get_P_Amount);
                                var_P_Amount = Convert.ToDouble(dt.Rows[0]["purchasing"]);
                                lbl_Purchasing_Amount.Text = var_P_Amount.ToString();

                                //In this side i use many DBNull methed but it can't  be work
                                //My Question is that If var_P_Amount value is null then
                                //lbl_Purchasing_Amount.Text = "0";
                            }
                            else
                            {
                                lbl_Purchasing_Amount.Text = "0";
                            }
                        }
                    }
                    else
                    {
                        lbl_Purchasing_Amount.Text = "0";
                    }
                }
                catch (Exception)
                {

                }
            }
  • You cannot use ToString() on a null object use following : lbl_Purchasing_Amount.Text = (var_P_Amount == DBNull) ? "" : var_P_Amount.ToString(); – jdweng Oct 21 '18 at 18:43
  • You need to tell us the [complete exception](http://idownvotedbecau.se/noexceptiondetails/), not just a few words from it, and show us the values that are throwing the exception. But my guess is one of your database columns contains `DbNull`; you have to test for `DbNull` **before** you convert it to an `Int32` or `Double`. Consult [Check for DbNull Then Assign to a Variable](https://stackoverflow.com/questions/221582/). – Dour High Arch Oct 21 '18 at 18:54
  • Beware: the code is vulnerable to SQL injection attacks. Use parameters rather than string concatenation to create the queries. – Richardissimo Oct 21 '18 at 20:38
  • Possible duplicate of [How to get nullable](https://stackoverflow.com/questions/9503698/)? – Dour High Arch Oct 21 '18 at 22:02

2 Answers2

0

DbNull in a result set means your database query is returning a NULL value in some column. A NULL value, by definition, cannot be converted to a string or number or anything.

In your case you only have one column in your result set, and it's a number. So zero can probably substitute for NULL without causing damage to your data.

Try adjusting your queries so they never return NULL. Here's how:

 get_P_Amount = "SELECT IFNULL(`purchasing`,0) AS 'purchasing' ... 

and

get_P_Amount = "SELECT IFNULL(SUM(t_price), 0) as 'purchasing' FROM `db_vegetab ...

This is a guess, however. You didn't tell us in your question which line of code in your program threw the exeception.

Pro tip: Never use catch (Exception) { }. It causes your program silently to ignore stuff that goes wrong. You want to know when your program throws exceptions, especially when it processes data about other peoples' money. If you must use it, do something like this:

 catch (Exception) {
      /* empty, intentionally, because we ignore the zumbinated framiss */
 }

This will help keep the next programmer to work on your code from cursing your name.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
0

Most likely it fails on converting to double So fix is

If (dt.Rows[0]["purchasing"] != DbNull) var_P_Amount = Convert.ToDouble(dt.Rows[0]["purchasing"]);
Aldert
  • 4,209
  • 1
  • 9
  • 23