Nel

Nel

  • NA
  • 716
  • 1.1m

Problem with the comboBox_SelectedIndexChanged

Feb 9 2012 3:10 AM
Hi,

I have two comboboxes on my windows form, and I want to fill the second combobox depending on the selected value of the first one.
I have this code for the first combobox_selectedindexchanged:

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
  {
 OleDbCommand command2 = new OleDbCommand();
  command2.CommandText = "Select * from Dogovor where Komint=? order by Brdog";
  command2.Parameters.AddWithValue("Komint", comboBox3.SelectedItem);
  command2.Connection = conn;
  DataSet3 _dataset = new DataSet3();
  OleDbDataAdapter oleDBDataAdapter1 = new OleDbDataAdapter(new OleDbCommand(command2.CommandText, conn));
 
  try
  {
 //conn.Open();  command2.ExecuteNonQuery();
 oleDBDataAdapter1.Fill(_dataset, "Dogovor");
  comboBox2.DataSource = _dataset.Tables["Dogovor"];
  comboBox2.DisplayMember = "Brdog";
  comboBox2.ValueMember = "Brdog"; 
 }
  finally
  {
  conn.Close();
  }
  textBox1.Focus();
}
 and I get this error:
"Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."

I tried to avoid using parameter, though I need it, but just to try what I will get. For the bold lines I have now:

 command2.CommandText = "Select * from Dogovor order by  Brdog";
  //command2.Parameters.AddWithValue("Komint",  comboBox3.SelectedItem);
  command2.Connection = conn;

and I get error "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed."

But if I uncomment
  //conn.Open();

then I get error " The connection was not closed. The connection's current state is open."

Can anybody help me please?
Thank you in advance.


P.S. Here is how I fill the first combobox:

private void Dogovor_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
          
            textBox1.Focus();
            DataSet3 _dataset = new DataSet3(); 
            OleDbCommand command3 = new OleDbCommand();
                        
             command3.Connection = conn;            
             command3.CommandText = "Select * from Komintent order by Komintent asc";                     
             OleDbDataAdapter DatAd = new OleDbDataAdapter(new OleDbCommand(command3.CommandText, conn));
           
            try
            {
                conn.Open();
               command3.ExecuteNonQuery();

                DatAd.Fill(_dataset, "Komintent");

                comboBox3.Items.Add("[Site]");
             
                comboBox3.DataSource = _dataset.Tables["Komintent"];
              
                comboBox3.DisplayMember = "Komintent";
                comboBox3.ValueMember = "ID";
               
                comboBox3.SelectedIndex = comboBox3.Items.Count - 1;
                }
            finally
            {
                conn.Close();
            }
        }



Answers (3)