Re: list box primary key vb .net by William
William
Wed Jan 30 11:37:12 CST 2008
The Listbox control can be bound to a single column via a DataReader in an
ASP.NET program, but not directly to the ListBox control in a Windows forms
application. That would take using code (something) like this
Private Sub BuildCommand()
Try
cmd = New SqlCommand("SELECT Au_ID, Author, Year_Born FROM
Authors " _
& " WHERE Year_Born = @YearWanted", cn)
cmd.Parameters.Add("@YearWanted", SqlDbType.Int).Value = 1947
cn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim tb As New DataTable
tb.Load(dr)
ListBox1.DisplayMember = "Author"
ListBox1.DataSource = tb
Catch exsql As SqlException
MessageBox.Show(exsql.ToString)
Catch ex As Exception
Debug.Assert(False, ex.ToString)
Finally
cn.Close()
End Try
End Sub
--
FMI see my 7th Edition.
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
<mcotter@frontiernet.net> wrote in message
news:62e9a10b-a3cc-4567-a322-4d810a91f2fd@e10g2000prf.googlegroups.com...
> Does anyone know how to populate a list box using SqlDataReader. I am
> using VB .NET 2005. Along with the list box value that is visible I
> need it's primary key value that is not visible.