Populating listbox via dataset

evad4682

Well-known member
Joined
Nov 7, 2005
Messages
55
Programming Experience
Beginner
Whats up all

I am attempting to populate a listbox from a dataset. This is what I am trying to do:

VB.NET:
Dim query2 As String = "Select i.name From asset_apps a, identification i Where a.appid=i.applicationid and a.asset= '" & lblAsset.Text & "'"
        Dim ds2 As DataSet = New DataSet
        Dim da2 As SqlDataAdapter = New SqlDataAdapter(query2, cnxn)
        da2.Fill(ds2, "application")
        Dim dv2 As DataView = New DataView(ds2.Tables("application"))
ListBox1.DataBindings.Add("text", dv2, "name")

The above is not working. I was told that to populate the listbox I need add the following:

listBox1.DataSource = ds2.Tables("application)
lstBox1.DataTextField = "text"
listbox1.datavaluefield = "name"
listbox1.DataBind()

I recieve the error, "DataTextField not a member of System.windows.forms.listbox". The same happens for datavaluefield and databind. I am pretty new to this stuff so I am sure that I am missing something. All help is very much appreciated. Thanks.
 
I think you may already have an answer for this from another forum but just in case, the code you have been suggested is for WebForms. The WinForms version uses DisplayMember instead of DataTextField, ValueMember instead of DataValueField and get rid of the last line altogether.
 
Thanks a lot! Sorry if that info was in another post, I did a quick search prior to posting but I couldn't find anything. Thanks again for all your help.
 
Not in another thread on this site. I meant on another site altogether. I seem to recall having seen the same question so I thought that you may already have the answer. All's well that ends well. :)
 
Back
Top