Databinding to a listview

A

Ant

Hi,
I'm trying to populate a list box using a dataset. The data set is tested
fine to have data in it but when I run the app, the listbox does not get
populated. Below is what I'm doing:

Class1 myclass = new Class1();

myclass.Server = "LAPTOP";
myclass.DB = "Northwind";

DataSet ds = myclass.ReturnDS();

ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "CustomerID";
ListBox1.DataBind();

// works ok, returns field so Dataset contains data
Page.Response.Write(ds.Tables[0].Rows[0][0].ToString();


Why doesn't this work?
Many thanks for any ideas in advance
Ant
 
C

Cowboy \(Gregory A. Beamer\)

Are you sure Tables[0] is the correct table? If you want to test this, try:

ListBox1.DataSource = ds.Tables["Customers"].DefaultView;
ListBox1.SelectedIndex = 0;
ListBox1.DataTextField = "CustomerID";
ListBox1.DataBind();

I think that is correct for Northwind (the table name), but check it. You
can set the index where ever you desire. I snipped this code from another
source.

If the above does not work, you do not have your tables properly named. This
can be done on the adapter.

dataAdapter.TableMappings.Add("Table", "Customers")

Personally, I like strongly typing the DataSet, as you can then use
Intellisense. It also adds a small bit of performance.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 
A

Ant

Thanks Cowboy but unfortunately still no go.
I tried it exactly as you mentioned. Do i need to set something for the list
box to show the items?

Here is the method which passes out the dataset:
public DataSet ReturnDS()
{

SqlConnectionStringBuilder constring = new
SqlConnectionStringBuilder();
constring.DataSource = this.Server;
constring.InitialCatalog = this.DB;
constring.IntegratedSecurity = true;
SqlConnection con = new SqlConnection(constring.ToString());
SqlCommand com = new SqlCommand("SELECT CustomerID FROM Orders", con);
SqlDataAdapter adaptor = new SqlDataAdapter(com);
adaptor.TableMappings.Add("Table", "Orders");
con.Open();
adaptor.Fill(ds);
con.Close();

return ds;
}

Here is the code that binds it to the listbox:
Class1 myclass = new Class1();

myclass.Server = "myserver";
myclass.DB = "Northwind";
DataSet ds = myclass.ReturnDS();
ListBox1.DataSource = ds.Tables["Orders"].DefaultView;
ListBox1.SelectedIndex = 0;
ListBox1.DataTextField = "CustomerID";
// works ok. returns first field
Page.Response.Write(ds.Tables["Orders"].Rows[0][0].ToString());

while i can rturn a field from the ds ok (see response.write),
I still get no data in the list box.

Why is this so? Many thanks for your help
Ant




Cowboy (Gregory A. Beamer) said:
Are you sure Tables[0] is the correct table? If you want to test this, try:

ListBox1.DataSource = ds.Tables["Customers"].DefaultView;
ListBox1.SelectedIndex = 0;
ListBox1.DataTextField = "CustomerID";
ListBox1.DataBind();

I think that is correct for Northwind (the table name), but check it. You
can set the index where ever you desire. I snipped this code from another
source.

If the above does not work, you do not have your tables properly named. This
can be done on the adapter.

dataAdapter.TableMappings.Add("Table", "Customers")

Personally, I like strongly typing the DataSet, as you can then use
Intellisense. It also adds a small bit of performance.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
Ant said:
Hi,
I'm trying to populate a list box using a dataset. The data set is tested
fine to have data in it but when I run the app, the listbox does not get
populated. Below is what I'm doing:

Class1 myclass = new Class1();

myclass.Server = "LAPTOP";
myclass.DB = "Northwind";

DataSet ds = myclass.ReturnDS();

ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "CustomerID";
ListBox1.DataBind();

// works ok, returns field so Dataset contains data
Page.Response.Write(ds.Tables[0].Rows[0][0].ToString();


Why doesn't this work?
Many thanks for any ideas in advance
Ant
 

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