3
Answers

Problem with the comboBox_SelectedIndexChanged

Photo of Nel

Nel

13y
1.7k
1
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)

1
Photo of Satyapriya Nayak
NA 53k 8m 13y

Attachment comboboxs.rar

Hi Nel,


Refer the below code and change accordingly as per your requirement.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Comboboxs
{
  public partial class Form1 : Form
  {
  string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
  OleDbCommand com;
  OleDbDataAdapter oda;
  DataSet ds;
  string str;

  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  comboBox1.Items.Add("Choose Color serial No");
  OleDbConnection con = new OleDbConnection(ConnectionString);
  con.Open();
  str = "select * from Color";
  com = new OleDbCommand(str, con);
  OleDbDataReader reader = com.ExecuteReader();
  while (reader.Read())
  {
  comboBox1.Items.Add(reader["ColorserialNo"]);
  }
 
  }

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {
  comboBox2.Items.Clear();
  OleDbConnection con = new OleDbConnection(ConnectionString);
  con.Open();
  str = "select * from Color where ColorserialNo='" + comboBox1.Text.Trim() + "'";
  com = new OleDbCommand(str, con);
  OleDbDataReader reader = com.ExecuteReader();
  while (reader.Read())
  {
  comboBox2.Items.Add(reader["ColorpartNo"].ToString()); 
  }
  con.Close();
  reader.Close();
 
  }
  }
}



Thanks
If this post helps you mark it as answer
Accepted
0
Photo of Nel
NA 713 955.4k 13y
Thank you very much
0
Photo of Pravin Ghadge
NA 2.5k 358.5k 13y
Nel,

Just use foll lines in place of      conn.Open():
conn.Close();
conn.Open();