Hi Frazier:
Are you firing another query each time the index changes? you definitely
don't want to do this. You can just load the data into a datatable and bind
it to the combobox. Also, you're getting a SQLException which means
specifically it's a problem w/ the db.
Cor is exaclty right about the SelectedIndexChange and that's ostensibly
causing this even though it's a db error. Also, don't use Dynamic SQL, use
Parameters. Nonetheless, you'll want to grab two tables, one for the
parent, one for the child, and just set the bindings of the grid and the
CombBox. You'll need to use a DataRelation
http://www.knowdotnet.com/articles/datarelation.html to make this easy. If
you do this, you won't have to trap anything on SelectedIndexChanged to get
the child records to show up in a grid. This is pretty common and if hit a
search engine for Master Detail ADO.NET there will be tons of examples.
Let me know if you need any elaboration.
HTH,
Bill
--
W.G. Ryan MVP Windows - Embedded
http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
Frazer said:
hi
i have a combo box which i populate with all tables from northwind database
and when the user selects an item from the combo, i want to populate the
datagrid with data from that table.
private void FillComboBox()
{
DataSet allTablesDataSet = new DataSet("AllTables");
string sqlString;
this.tableComboBox.Items.Clear();
sqlString = "Select * from sysobjects where type ='u'";
this.sqlDataAdapter = new SqlDataAdapter();
this.sqlDataAdapter.SelectCommand = new SqlCommand(sqlString,
this.sqlConnection);
this.sqlDataAdapter.Fill(allTablesDataSet,"AllTables");
this.tableComboBox.DataSource = allTablesDataSet.Tables[0];
this.tableComboBox.DisplayMember = "name";
this.tableComboBox.ValueMember = "name";
}
private void OnTableComboBoxSelectedIndexChanged(object sender,
System.EventArgs e)
{
if(this.tableComboBox.SelectedIndex == -1)
return;
this.tableDataSet = new DataSet();
this.sqlDataAdapter.SelectCommand = new SqlCommand("Select * from [" +
this.tableComboBox.SelectedValue + "]" ,this.sqlConnection);
this.sqlDataAdapter.Fill(this.tableDataSet, "Table");
this.dataGrid1.DataSource = this.tableDataSet;
this.dataGrid1.DataMember = "Table";
}
however the first time when the program is run i get the foll error. how do
i resolve that?
thnx
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred
in system.data.dll
Additional information: System error.
on this line
this.sqlDataAdapter.Fill(this.tableDataSet, "Table");