Problem binding DataSet to a ListBox

This is a discussion on Problem binding DataSet to a ListBox within the CSharp forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > CSharp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 03:05 AM
JB
Guest
 
Default Problem binding DataSet to a ListBox

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
Reply With Quote
  #2  
Old 08-27-2008, 04:15 AM
Alberto Poblacion
Guest
 
Default Re: Problem binding DataSet to a ListBox

"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".

Reply With Quote
  #3  
Old 08-27-2008, 08:24 AM
JB
Guest
 
Default Re: Problem binding DataSet to a ListBox

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];
}
Reply With Quote
  #4  
Old 08-27-2008, 11:48 AM
Alberto Poblacion
Guest
 
Default Re: Problem binding DataSet to a ListBox

"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?

Reply With Quote
  #5  
Old 08-28-2008, 03:13 AM
JB
Guest
 
Default Re: Problem binding DataSet to a ListBox

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.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:33 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.