1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2901955
  • 93
  • 3
  • 13

2 Answers2

1

Combobox, it seems you are working on Window Application. Try using SelectedText if you want entered value should be considered otherwise if manual entry can be omitted, it is recommended to avoid that.

1

Paraphrasing the answer here:

The cause is cboFrom.SelectedValue.ToString() from the comboBox.You must set the the DataSource property of your Combobox control if you want to use the DisplayMember and ValueMember properties. if you currently use comboBox1.Items.AddRange to add the lookups just Try to replace comboBox1.Items.AddRange(Lookup);with comboBox1.DataSource=Lookup;

Or if you can do the same thing as follows you can solve this out.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<MyListItem> item = new List<MyListItem>();
            item.Add(new MyListItem{Text = "A", Value ="1"});
            item.Add(new MyListItem { Text = "B", Value = "2" });
            item.Add(new MyListItem { Text = "C", Value = "3" });
            comboBox1.DataSource = item;
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // This one not throw the null refference exception
            var ss = comboBox1.SelectedValue.ToString();
        }
    }
    struct MyListItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Thilina H
  • 5,754
  • 6
  • 26
  • 56