Hi
I have 6 comboboxes and a button. I also have two tables in my sql database.
First of all i have displayed all the tables in cb1 using:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC
The Columns of these two tables are exactly the same BUT they do have different records.
Now i wanna populate the remaining 5 comboboxes(cb2,cb3,cb4,cb5 & cb6) each with a column from the tables in the database
(PrdtName, PrdtCat, Origin, Price, Transtype, Continent)
Afterwards i wanna choose a tbl (dynamic)in cb1 and then selects the column names from the other comboboxes and then display its records on button click.
Here my code so far: 1. stored procedure (this also changes depending on the column name)
SELECT DISTINCT [PrdtName] FROM ' + @TableName + ' WHERE [Continent] = ''AF''' +
' ORDER BY [PrdtName] ASC
2. my c# code. This code repeats 5 times depending on the combobox or the column name
private void FillcbPrdtName()
{
string S = ConnectionString;
SqlConnection cn = new SqlConnection(S);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = ("[dbo].[spPrdtName]");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Table_Name", cb2.SelectedValue.ToString());
if (cbTbl.SelectedValue != null)
{
try
{
cn.Open();
SqlDataReader name= cmd.ExecuteReader();
while (name.Read())
{
cb2.Items.Add(name["PrdtName"]);
}
cb2.SelectedIndex = -1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
}
There is no ERROR but the 5 comboboxes aren' getting any columns from the database.
Help!!!