-2

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));
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
yael
  • 5
  • 4
  • 2
    OK, basic starter question. Did you call the `cc` function anywhere? Because I don't see it being called. – Wyck Dec 05 '18 at 02:05
  • Have you stepped through the code with the debugger to see if it is actually getting results back? – Phil M Dec 05 '18 at 02:06
  • 1
    You should call cc() when initializing the form. It's not called anywhere so no data is loaded – Oscar Siauw Dec 05 '18 at 02:07
  • oh yes, now I see it, thanks, now there's the list showing in my combobox, but here comes another problem, when I try to choose anything from the list, this error comes up "System.InvalidOperationException: 'Invalid attempt to read when no data is present.'" – yael Dec 05 '18 at 02:13
  • You're trying to do too many things at once. First, forget about databases and [get your ComboBox working](https://stackoverflow.com/questions/600869/). Then get your database calls out of your class definitions where they can crash or freeze your entire application and [put them in business objects](https://stackoverflow.com/questions/3258788/). – Dour High Arch Dec 05 '18 at 02:14

1 Answers1

0

You can try this.

   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);

             if (dt!= null && dt.Rows.Count > 0)
                    {
                        comboBox1.DisplayMember = "Department ";
                        comboBox1.ValueMember = "Department ";
                        comboBox1.DataSource = dt;                    
                    }
            conn.Close();
        }
Chetan Sanghani
  • 2,058
  • 2
  • 21
  • 36