Home All Groups Group Topic Archive Search About

How I populate listbox with dataset?

Author
9 Nov 2005 12:51 PM
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

Author
9 Nov 2005 6:52 PM
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.
__________________________________

Show quote
"Lasse Edsvik" <lasse@nospam.com> wrote in message
news:uxTuHvS5FHA.2604@TK2MSFTNGP11.phx.gbl...
> 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
>
>
>
>
>

AddThis Social Bookmark Button