Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > WinForms/Console Application Design
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the WinForms/Console Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 1st, 2009, 11:34 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default How to bind a ListBox to datadase

I need a way to display 3 columns data in ListBox

I Have this code:

Code:
 
Dim cnn As SqlConnection = New SqlConnection("Data Source=SQL1;Initial Catalog=Tracking;Integrated Security=True")
' Connect to the database
' Create a new ad hoc query to retrieve customer names
Dim cmd As SqlCommand = cnn.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT CNAM05 , CUSN05 , PCD105  FROM table1 Where (CNAM05 Like '" & TextBox1.Text & "%')"
' Dump the data to the user interface
cnn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
DoWhile dr.Read()
ListBox1.Items.Add(dr.GetString(0))
Loop
' Clean up
dr.Close()
cnn.Close()
this returns data from 1 column, I need to display Name -- Address--Post Code. but when i select an item only the value of name should be past



thanks in advance
__________________
bx

Last edited by bex; July 1st, 2009 at 03:39 PM..
 
Old July 4th, 2009, 04:01 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can concatenate the values you need in the query and then display them:

SELECT Name + ' (' + Address + ')' AS FullDescription, Name FROM Table

Then you can use FullDescription as the text for the drop down and Name for the value.

You can also query them as separate columns (Name, Address) and then concatenate them in your VB code.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
bex (July 4th, 2009)
 
Old July 4th, 2009, 10:35 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

Thanks Imar
that works fine,
now it dysplays like this

Name ------------------Address------------------------ Postcode
Name --------------------------------Address------------------------------ Postcode
Name ---------Address -----------------------------------Postcode.....

it will be nice if i can disply the query in columns:

Name Address Postcode
Name Address Postcode
Name Address Postcode
__________________
bx

Last edited by bex; July 4th, 2009 at 03:57 PM..
 
Old July 5th, 2009, 04:09 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi bex,

This is not so easy to implement. Browsers don't support multi columns in a drop down list and strip multiple spaces from the items in the list so you can't use spaces to fill up the items.

There are a number of solutions available; some free and some paid:

http://www.google.com/search?hl=en&q...n+drop+&aqi=g5

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
bex (July 5th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple Listbox values in another listbox terryv Excel VBA 0 June 27th, 2007 07:01 AM
Bind a Label cp75 ASP.NET 1.0 and 1.1 Basics 19 January 10th, 2007 10:53 AM
Grid Bind msbsam ASP.NET 2.0 Basics 1 December 19th, 2006 07:03 AM
how to bind a data() lsxx Classic ASP Components 4 April 20th, 2005 02:35 AM
I'm back :) Listbox var from listbox MichaelTJ .NET Web Services 2 October 21st, 2003 07:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.