I have trying to do the validation for non-existed of the customer ID. If the ID is exist, then the report will display the records for the ID, if is not existed, then error will be prompted out. But the error prompted out even I try to enter customer ID which is existed.
Error: Object reference not set to an instance of an object.
string sql = "SELECT whbal.customer, customer.imp_license_no, customer.psq_level, " +
"CONVERT(DECIMAL(8,3),SUM(CASE WHEN whbal.warehouse='SKW' THEN (CONVERT(DECIMAL(8,3),whbal.qty_good) + CONVERT(DECIMAL(8,3),whbal.qty_slack)) * CONVERT(DECIMAL(8,3),whbal.std_weight) /1000 ELSE 0.0 END)) AS SENOKO, " +
"FROM customer INNER JOIN whbal ON whbal.customer=customer.customer AND whbal.date_create<=@date1 " +
"INNER JOIN stktype ON whbal.stock_type=stktype.stock_type " +
"WHERE whbal.customer BETWEEN @cust1 AND @cust2 AND whbal.stock_type=@type " +
"GROUP BY whbal.customer, customer.imp_license_no,customer.psq_level";
SqlCommand custcom = new SqlCommand(sql, myconnection);
custcom.Parameters.AddWithValue("@cust1", cboFrom.SelectedValue.ToString());
custcom.Parameters.AddWithValue("@cust2", cboTo.SelectedValue.ToString());
custcom.Parameters.AddWithValue("@type", cboStk.SelectedValue.ToString());
custcom.Parameters.AddWithValue("@date1", dateTimePicker1.Value);
SqlDataAdapter da = new SqlDataAdapter(custcom);
DataSet1 ds = new DataSet1();
da.Fill(ds, "customer1");
DataTable dt = new DataTable();
da.Fill(dt);
myconnection.Close();
if (dt.Rows.Count > 0)
{
code...
}
else if (dt.Rows.Count <= 0)
{
MessageBox.Show("Customer not existed.");
}
Do anyone know what is the problem, please guide and advise.