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)
{
}
}