本文介绍了如何在combox的选择值之后获取文本框中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

how can i get data in textboxes from database if my combobox selected values are from table1 and the values i want to get in textboxes are from table2 ?





i我在所有文本框中只获得1个主题的名称我希望所有主题在从combox中选择课程后显示在文本框中



我尝试过:





i am getting name of just 1 subject in all textboxes i want all subject to show in textboxes after selecting class from combox

What I have tried:

private void Result_Load(object sender, EventArgs e)
       {

           string query = "SELECT  class_id , class_name FROM class";
           fillcombo(comboBox3, query, "class_name", "class_id");
          // comboBox3_SelectedIndexChanged(null, null);

       }
       public void fillcombo(ComboBox combo, string query, string displayMember, string valueMember)
       {
           command = new SqlCommand(query, con);
           adapter = new SqlDataAdapter(command);
           table = new DataTable();
           adapter.Fill(table);
           combo.DataSource = table;
           combo.DisplayMember = displayMember;
           combo.ValueMember = valueMember;
       }










private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
       {

           con.Open();
           int val;
           Int32.TryParse(comboBox3.SelectedValue.ToString(), out val);
           //string query = "SELECT  sub_name, class_id FROM subject  WHERE class_id = " + val;
           string query = "SELECT dbo.Subject.sub_name, dbo.Class.class_name FROM     dbo.Class full JOIN dbo.Subject ON dbo.Class.class_id = dbo.Subject.class_id where Class.class_id="+val;
           //string query = "SELECT sub_name FROM subject";


          SqlCommand command = new SqlCommand(query, con);

           using (var reader = command.ExecuteReader())
           {
               //SqlDataReader sdr = command.ExecuteReader();
               if (reader.Read())
               {


                   textBox1.Text = reader["sub_name"].ToString();
                   textBox2.Text = reader["sub_name"].ToString();
                   textBox3.Text = reader["sub_name"].ToString();
                   textBox4.Text = reader["sub_name"].ToString();
                   textBox5.Text = reader["sub_name"].ToString();
                   textBox7.Text = reader["sub_name"].ToString();
                   textBox8.Text = reader["sub_name"].ToString();
                   textBox9.Text = reader["sub_name"].ToString();
                   textBox10.Text = reader["sub_name"].ToString();
                   textBox11.Text = reader["sub_name"].ToString();




               }
               reader.Close();
           }
           con.Close();
       }

推荐答案



这篇关于如何在combox的选择值之后获取文本框中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:07