Problem binding DataSet to a ListBox

J

JB

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
 
A

Alberto Poblacion

JB said:
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".
 
J

JB

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];
}
 
A

Alberto Poblacion

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

JB

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top