| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I am trying to bind a ListBox to a dataset and am having some troubles getting it working right. I have source something like so: //query and store the data into the DataSet SqlCommand command = new SqlCommand(); string numToSearchFor = Properties.Settings.Default.NumProductsToSearch; command.CommandText = "SELECT TOP " + numToSearchFor + "[Items] FROM PriIMS_db.dbo.Product ORDER BY Items DESC"; command.Connection = myConnection; SqlDataAdapter dataAdapter = new SqlDataAdapter(); dataAdapter.SelectCommand = command; DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet,"Product"); //bind the data listBoxVsnProducts.ValueMember = "Items"; listBoxVsnProducts.DisplayMember = "Items"; listBoxVsnProducts.DataSource = products.Tables[0]; However all i'm getting is a blank listbox at the end of it. It has the right number of entries (determined by the value 'numToSearchFor') but they are all blank. It's probably something nooby i'm doing at the binding stage, the dataset itself is ok because I can do something like so: foreach (DataRow dataRow in products.Tables[0].Rows) //add item dataRow[0] into the listbox and it works fine |
|
#2
| |||
| |||
| "JB" <jamesb457@gmail.com> wrote in message news:3c8d65e6-9993-4d46-8a01-a11e89ca745d@e39g2000hsf.googlegroups.com... > DataSet dataSet = new DataSet(); > dataAdapter.Fill(dataSet,"Product"); > [...] > listBoxVsnProducts.DataSource = products.Tables[0]; Something is wrong here. You are filling a DataSet called "dataSet", but you are then databinding with another DataSet named "products". |
|
#3
| |||
| |||
| On 27 Aug, 09:15, "Alberto Poblacion" <earthling- quitaestoparacontes...@poblacion.org> wrote: > "JB" <jamesb...@gmail.com> wrote in message > > news:3c8d65e6-9993-4d46-8a01-a11e89ca745d@e39g2000hsf.googlegroups.com... > > > DataSet dataSet = new DataSet(); > > dataAdapter.Fill(dataSet,"Product"); > > [...] > > listBoxVsnProducts.DataSource = products.Tables[0]; > > * * *Something is wrong here. You are filling a DataSet called "dataSet", > but you are then databinding with another DataSet named "products". Well spotted. That was a typo on my part, because the two chunks of code are in different methods. So in actuality the code is: public DataSet GetListOfProducts() { SqlCommand command = new SqlCommand(); string numToSearchFor = Properties.Settings.Default.NumProductsToSearch; command.CommandText = "SELECT TOP " + numToSearchFor + "[ProductCode] FROM PrimeurIMS_db.dbo.Product ORDER BY ProductCode DESC"; command.Connection = visionConnection; SqlDataAdapter dataAdapter = new SqlDataAdapter(); dataAdapter.SelectCommand = command; DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet,"Product"); return dataSet; } private void TabPage_Page2Load(object sender, EventArgs e) { DataSet products = GetListOfProducts(); listBoxVsnProducts.ValueMember = "ProductCode"; listBoxVsnProducts.DisplayMember = "ProductCode"; listBoxVsnProducts.DataSource = products.Tables[0]; } |
|
#4
| |||
| |||
| "JB" <jamesb457@gmail.com> wrote in message news:f51ac631-5ab5-4bca-b4a9-4d6ba69a9047@k30g2000hse.googlegroups.com... On 27 Aug, 09:15, "Alberto Poblacion" <earthling- > [...] in actuality the code is: > > public DataSet GetListOfProducts() > { > SqlCommand command = new SqlCommand(); > string numToSearchFor = > Properties.Settings.Default.NumProductsToSearch; > command.CommandText = "SELECT TOP " + numToSearchFor + > "[ProductCode] FROM PrimeurIMS_db.dbo.Product ORDER BY ProductCode > DESC"; > command.Connection = visionConnection; > > SqlDataAdapter dataAdapter = new SqlDataAdapter(); > dataAdapter.SelectCommand = command; > > DataSet dataSet = new DataSet(); > dataAdapter.Fill(dataSet,"Product"); > > return dataSet; > } > > > private void TabPage_Page2Load(object sender, EventArgs e) > { > DataSet products = GetListOfProducts(); > listBoxVsnProducts.ValueMember = "ProductCode"; > listBoxVsnProducts.DisplayMember = "ProductCode"; > listBoxVsnProducts.DataSource = products.Tables[0]; > } The code looks perfectly fine to me, and it should be working as expected. The only thing that comes to mind that could be causing some trouble is an unintended databinding written in the Properties Window of the listBox in design mode. Have you checked that this is blank, and your only databinding is the one being done in code? |
|
#5
| |||
| |||
| On 27 Aug, 16:48, "Alberto Poblacion" <earthling- quitaestoparacontes...@poblacion.org> wrote: > "JB" <jamesb...@gmail.com> wrote in message > > news:f51ac631-5ab5-4bca-b4a9-4d6ba69a9047@k30g2000hse.googlegroups.com... > On 27 Aug, 09:15, "Alberto Poblacion" <earthling- > > > > > [...] in actuality the code is: > > > public DataSet GetListOfProducts() > > *{ > > * * * * * * SqlCommand command = new SqlCommand(); > > * * * * * * string numToSearchFor = > > Properties.Settings.Default.NumProductsToSearch; > > * * * * * * command.CommandText = "SELECT TOP " + numToSearchFor + > > "[ProductCode] FROM PrimeurIMS_db.dbo.Product ORDER BY ProductCode > > DESC"; > > * * * * * * command.Connection = visionConnection; > > > * * * * * * SqlDataAdapter dataAdapter = new SqlDataAdapter(); > > * * * * * * dataAdapter.SelectCommand = command; > > > * * * * * * DataSet dataSet = new DataSet(); > > * * * * * * dataAdapter.Fill(dataSet,"Product"); > > > * * * * * * return dataSet; > > } > > > private void TabPage_Page2Load(object sender, EventArgs e) > > { > > * * *DataSet products = GetListOfProducts(); > > * * *listBoxVsnProducts.ValueMember = "ProductCode"; > > * * *listBoxVsnProducts.DisplayMember = "ProductCode"; > > * * *listBoxVsnProducts.DataSource = products.Tables[0]; > > } > > * * The code looks perfectly fine to me, and it should be working as > expected. The only thing that comes to mind that could be causing some > trouble is an unintended databinding written in the Properties Window of the > listBox in design mode. Have you checked that this is blank, and your only > databinding is the one being done in code? Everything under DataBinding in design view is either blank or '(none)'. Same with the DataSource, ValueMember and DisplayMember properties. It's odd because it is creating an item for each row in the dataset. I have just done a little research and testing, basically debugging to show the selected entry. The listBox.SelectedItem.ToString() always equals "System.Data.DataRowView" and the listBox.SelectedValue.ToString() is correct! It equals the value from the DataSet. So it seems its just a problem with displaying it. Does that narrow it down enough to help offer me a solution? Cheers. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.