I am filling a ComboBox via a SQL Data Adapter and I have run into an issue where the adapter is bringing back a count of four tables. This is odd because there are only three tables within the database, and it is filling the final table (ds.Tables[3]) instead of the initial table (ds.Tables[0]) with the appropriate rows.
The following code does NOT populate the ComboBox (Take note of cboCities.DataSource (the second to last line)):
private void Form1_Load(object sender, EventArgs e)
{
// Establishes a connection to the database.
SqlCeConnection cn = new SqlCeConnection(@"Data Source = C:\Users\user\Desktop\Projects\ParkSurvey WF\ParkSurvey WF\ParkSurvey.sdf; Persist Security Info = False; Password = *");
cn.Open();
// Gathering the names of cities from the Cities database to populate cboCities.
SqlCeCommand cmd = cn.CreateCommand();
cmd.CommandText = "SELECT CityId, Name FROM Cities ORDER BY Name ASC";
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
// Closing the connection and setting the data bindings for cboCities.
cn.Close();
cboCities.ValueMember = "CityId";
cboCities.DisplayMember = "Name";
cboCities.DataSource = ds.Tables[0];
cboCities.SelectedIndex = -1;
}
This DOES populate the ComboBox appropriately (take note again of cboCities.DataSource):
private void Form1_Load(object sender, EventArgs e)
{
// Establishes a connection to the database.
SqlCeConnection cn = new SqlCeConnection(@"Data Source = C:\Users\user\Desktop\Projects\ParkSurvey WF\ParkSurvey WF\ParkSurvey.sdf; Persist Security Info = False; Password = *");
cn.Open();
// Gathering the names of cities from the Cities database to populate cboCities.
SqlCeCommand cmd = cn.CreateCommand();
cmd.CommandText = "SELECT CityId, Name FROM Cities ORDER BY Name ASC";
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
// Closing the connection and setting the data bindings for cboCities.
cn.Close();
cboCities.ValueMember = "CityId";
cboCities.DisplayMember = "Name";
cboCities.DataSource = ds.Tables[3];
cboCities.SelectedIndex = -1;
}
What is causing my DataAdapter to bring back four tables instead of JUST Cities, and why is it populating the fourth table instead of the initial table? Please let me know if you require more code sample to help assist me with this issue. Thank you very much!
It should work if you fill a
DataTableinstead of aDataSetsince you’re selecting only one table anyway.Apart from that, i must admit that don’t know the reason for this behaviour.