Aly Ramadan
Aly Ramadan

Reputation: 31

how i fill 2 comboboxs from 2 different tables?

I need to fill 2 comboboxs from 2 different tables. This way doesn't work.

private void client_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.;Initial Catalog=SCP_DB;Integrated Security=True";
    con.Open();
    string query = "select  Operation_Type from Operations_Types";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "Operations_Types");
    comboOpType.DisplayMember = "Operation_Type";
    comboOpType.ValueMember = "Operation_Type";
    comboOpType.DataSource = ds.Tables["Operations_Types"];

    da.Fill(ds, "Payment_Type");
    comboPayType.DisplayMember = "Payment_Types";
    comboPayType.ValueMember = "Payment_Types";
    comboPayType.DataSource = ds.Tables["Payment_Type"];
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();
    con.Close();
}

Upvotes: 0

Views: 81

Answers (1)

Badiparmagi
Badiparmagi

Reputation: 1285

you should call each table in your sql instance and also use the TableMappings

change your code like this.

private void client_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.;Initial Catalog=SCP_DB;Integrated Security=True";
    con.Open();
    string query = "select  Operation_Type from Operations_Types; select ? from Payment_Type";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    da.TableMappings.Add("Table", "Operations_Types");
    da.TableMappings.Add("Table1", "Payment_Type");
    DataSet ds = new DataSet();
    da.Fill(ds, "Table");
    comboOpType.DisplayMember = "Operation_Type";
    comboOpType.ValueMember = "Operation_Type";
    comboOpType.DataSource = ds.Tables["Operations_Types"];

    da.Fill(ds, "Table1");
    comboPayType.DisplayMember = "Payment_Types";
    comboPayType.ValueMember = "Payment_Types";
    comboPayType.DataSource = ds.Tables["Payment_Type"];
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();
    con.Close();
}

Upvotes: 1

Related Questions