How I populate listbox with dataset?

L

Lasse Edsvik

Hello

I've created a class that has a method that returns a dataset like this:

public DataSet GetAllCustomers()

{

Open();

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand("CustListAll", dbconn);

da.Fill(ds);

return ds;

}



Then I added a new project and then tried to do this:



public Form1()

{

InitializeComponent();

Northwind n = new Northwind();

listBox1.DataSource = n.GetAllCustomers();

listBox1.DisplayMember = "CustomerName";

listBox1.Text = "CustomerName";



}



When I run it only has a text
"System.Data.DataViewManagerListItemType.Descriptor" in it

errrm? :) what to do here to get it to show in listbox?



/Lasse
 
W

William \(Bill\) Vaughn

How about this? It's a bit easier and it works for 1.1 or 2.0. It shows a
way to create a binable arraylist instead of a DataSet/DataTable.

hth

Private Sub BuildAndExecuteCommand()

Try

cmd = New SqlCommand("SELECT TOP 50 Au_ID, Author FROM Authors", cn)

Dim dr As SqlDataReader = Nothing

Dim alRows As New ArrayList

cn.Open()

dr = cmd.ExecuteReader

If dr.HasRows Then

For Each rec As Common.DbDataRecord In dr

alRows.Add(rec) ' Move rows to ArrayList

Next

DataGridView1.DataSource = alRows

ListBox1.DataSource = alRows

ListBox1.DisplayMember = "Author"

Else

MsgBox("No rows returned from query...")

End If

Catch ex As Exception

MessageBox.Show(ex.ToString)

Finally

cn.Close()

End Try

End Sub


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

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