p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > .NET > Other .NET > WinForms/Console Application Design
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read

Welcome to the p2p.wrox.com Forums.

You are currently viewing the WinForms/Console Application Design section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 1st, 2009, 12:34 PM
bex bex is offline
Friend of Wrox
Points: 643, Level: 9
Points: 643, Level: 9 Points: 643, Level: 9 Points: 643, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 153
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 04:39 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 4th, 2009, 05:01 AM
Imar's Avatar
Wrox Author
Points: 33,533, Level: 80
Points: 33,533, Level: 80 Points: 33,533, Level: 80 Points: 33,533, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,225
Thanks: 7
Thanked 202 Times in 200 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
bex (July 4th, 2009)
  #3 (permalink)  
Old July 4th, 2009, 11:35 AM
bex bex is offline
Friend of Wrox
Points: 643, Level: 9
Points: 643, Level: 9 Points: 643, Level: 9 Points: 643, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 153
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 04:57 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 5th, 2009, 05:09 AM
Imar's Avatar
Wrox Author
Points: 33,533, Level: 80
Points: 33,533, Level: 80 Points: 33,533, Level: 80 Points: 33,533, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,225
Thanks: 7
Thanked 202 Times in 200 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
bex (July 5th, 2009)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple Listbox values in another listbox terryv Excel VBA 0 June 27th, 2007 08: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 03:35 AM
I'm back :) Listbox var from listbox MichaelTJ .NET Web Services 2 October 21st, 2003 08:06 PM



All times are GMT -4. The time now is 02:22 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc