Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have two comboboxes and both comboboxes are being populated with the same tables from sql server.(T1,T2& T3). I want to dynamically choose a table from each combobox and JOIN or combine the selected tables together and display the result in a DataGridView. All Tables have the same columns.

This small project is a c# winforms application. Can somebody please help me?
Posted

1 solution

In this case we can combine the select query from all three tables and use the UNION operator to get the unique values:

select * from T1
union
select * from T2
union
select * from T3;

This query is passed to the dataset and bind the dataset with gridview.
 
Share this answer
 
Comments
mikybrain1 8-Oct-14 7:24am    
Hi MukeshSagar thankx for your suggestion but i wanna choose each table from each combobox and then click a btn to display the result
I thougt something like this:
("SELECT * FROM" + combobox1.selectedValue
UNION ALL + "SELECT * FROM" + combobox2.selectedValue)
and maybe an IF Statement that checks the selected item and display the UNION solution in DataGridView if the submit btn is clicked
Can you please help me out?
MukeshSagar 8-Oct-14 7:56am    
("select * from '" + ComboBox1.SelectedValue.ToString() + "'
union
select * from '" + ComboBox2.SelectedValue.ToString() + "'
union
select * from '" + ComboBox3.SelectedValue.ToString() + "')
mikybrain1 8-Oct-14 8:06am    
Hi. Thats what i so far have:

/* CREATE A FUNCTION: Code to fill in Combobox1.
And have to call the funcktion later in the InitializedComponent */

private void FillCombobox1()
{
// Sql Connection and statement
string C = (@"Data Source=local;Initial Catalog=PracticeDatabase;Integrated Security=True");
SqlConnection con = new SqlConnection(CS);

// Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
try
{
// Open connection
con.Open();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

// Save the results in the DT. Call the adapter statement and fill it in the DT
DataTable dt = new DataTable();
adapter.Fill(dt);

//Fill combobox with data in DT
comboBox1.DataSource = dt;

//Display dbo Tables in cb
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";

// Empty bzw. clear the combobox
comboBox1.SelectedIndex = -1;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}


/* CREATE a 2. FUNCTION: Code to fill in Combobox2. I can always write my own function.
And have to call the funcktion later in the InitializedComponent */

private void FillCombobox2()
{
// Sql Connection and statement
string C = (@"Data Source=local;Initial Catalog=PracticeDatabase;Integrated Security=True");
SqlConnection con = new SqlConnection(CS);

// Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
try
{
// Open connection
con.Open();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

// Save the results in the DT. Call the adapter statement and fill it in the DT
DataTable dt = new DataTable();
adapter.Fill(dt);

//Fill combobox with data in DT
comboBox2.DataSource = dt;

//Display dbo Tables in cb
comboBox2.DisplayMember = "TABLE_NAME";
comboBox2.ValueMember = "TABLE_NAME";

// Empty bzw. clear the combobox
comboBox2.SelectedIndex = -1;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}


// Populate dgv with the two comboboxes
private void button7_Click(object sender, EventArgs e)
{
// Sql Server Connection and statement
string CS = (@"Data Source=SOPHIE\SQLEXPRESS;Initial Catalog=PracticeDatabase;Integrated Security=True");
SqlConnection con = new SqlConnection(CS);




// IF STATEMENT & Sql Server Connection and TSQL Statement
if (comboBox1.SelectedValue != null && comboBox2.SelectedValue != null)
{


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT * FROM " + comboBox1.SelectedValue UNION ALL "SELECT * FROM" +comboBox2.SelectedValue);

// adapter is return the result in Datagridview
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

try
{
// Open connection
con.Open();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900