0

以下のコードを使用してコンボボックスを埋めていますが、usingコードが機能し、国アイテムを含む国コンボボックスを取得していますが、コードよりもコードをusing記述してcomboBox1_SelectedIndexChangedusing機能しないのはなぜですか? このため、選択した国に基づいて州のドロップダウンを取得できません。どうすればよいですか?

public partial class RegPatient : Form
    {
        DBHandling db = new DBHandling();
        string cmbvalue="";
        public RegPatient()
        {
            InitializeComponent();
             using (DataTable dt = DBHandling.GetCountryDataTable())
            {
                comboBox1.DataSource = new BindingSource(dt, null);
                comboBox1.DisplayMember = "CountryName"; //column to show in comboBox
                comboBox1.ValueMember = "Code"; 
            }//here the table is disposed 
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {            
            //using code not working here          
        } 
    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (DataTable dt = DBHandling.GetStateDataTable(comboBox1.Text) )
        {
            // contine using dt
            comboBox2.DataSource = new BindingSource(dt, null);
            comboBox2.DisplayMember = "ProvinceName"; 
           }//here the table is disposed 
    }   
    }

データ可能な国と州を取得するためのコード

public static DataTable GetCountryDataTable()
        {
            DataTable countryTable = new DataTable();

            using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sonorepo.mdb")) //use your conn. string here
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(@"SELECT CountryName, Code FROM Country", con))
                    da.Fill(countryTable);
            }
            return countryTable;
        }
        public static DataTable GetStateDataTable(string countryCode)
        {
            DataTable stateTable = new DataTable();
            using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sonorepo.mdb"))
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(@"SELECT ProvinceName FROM Province where Country='" + countryCode + "'", con))
                    da.Fill(stateTable);
            }
            return stateTable;
        }

前もって感謝します

4

2 に答える 2