I have a combobox in form5 and the script is below.
string sql = "SELECT * FROM `stock` WHERE `status`='1' AND `jml_stock`>'0'";
string sql2 = "SELECT * FROM `pelanggan`";
MySqlConnection mcon = new MySqlConnection(con);
mcon.Open();
MySqlCommand comm = new MySqlCommand(sql, mcon);
MySqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
string id_stock = dr["id_stock"].ToString();
string nama = dr["nama_produk"].ToString();
barang.Items.Add(nama);
barang.ValueMember = id_stock;
barang.DisplayMember = nama;
}
mcon.Close();
I want to retrieve data from the combobox above using this script.
id_stock = barang.SelectedValue.ToString();
but I get an error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
What do you think did I do wrong?