0

I have two comboboxs(name and category) and two tables(tbl_participant and tbl_category). I want to retrieve data from tlb_paticipant to cboName and tbl_category to cboCategory when the form load. Below is the code for retrieve tbl_paticipant to cboName, but I don't know how to retrieve another one. Make tow connection, command, DataReader object?

        con = ConnectionDB.GetConnection();
        string sql = "select * from tbl_Participant";
        cmd = new SqlCommand(sql, con);

        try
        {
            con.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                cboName.Items.Add(dr.GetValue(2).ToString());

            }
            cboName.SelectedIndex = 0;
            cmd.Dispose();
            con.Close();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
Ben
  • 83
  • 2
  • 8
  • Irrespective of this being a basic/starters design. You can copy/paste same code and change the SQL Query and the ComboBox object to get what you need. – Prateek Shrivastava Sep 10 '19 at 02:30
  • Is there other way? – Ben Sep 10 '19 at 02:32
  • Yes, there are several ways that I can think of. Firstly there should be a Database Layer in the client side code where you define the SQL's. So one function per type of query that you do. i.e. 2 functions for fetching the results from Db for the above 2 tables. Next, the return types of these functions should ideally be UI wrappers classes/entities. So that you can directly bind the return value to the ComboBox.ItemsSource and set appropriate DisplayValue (for WinForms). Diff ball game if its WPF/ASP.NET – Prateek Shrivastava Sep 10 '19 at 03:48
  • Why you need another way? You want to retrieve two sets in a single query? – Circle Hsiao Sep 10 '19 at 04:42

0 Answers0