I am trying to show a list in the combobox from SQL, but I can't make it work. I don't get any errors, but my combobox is not showing anything.
Here is my code:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=MEDIXPC197;" +
"Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cd = new SqlCommand();
public void cc()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Department"].ToString());
}
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
comboBox1.Items.Add(dr.GetValue(0));
}
}