Is there a way to concatenate info from 2 database columns to display  together in the same listbox control?

Posted on 2006-05-21
Last Modified: 2010-08-05
I use VS 2005. I am building a windows app. Is there a way to take info from 2 different database columns, like last name and first name, concatenate them into a listbox that will display in the form LastName, FirstName?

Question by:grouchyfd
    LVL 5

    Accepted Solution


    "SELECT Id, FirstName & ' ' & LastName As FullName, Address FROM yourTableName"


    "SELECT ID, FirstName + ' ' + LastName AS FullName, Address FROM yourTableName"

     listbox.DisplayMember = "FullName"
     listbox.ValueMember = "Id"
     listbox.DataSource = yourDataSet.Tables(0)
    LVL 34

    Assisted Solution


    You can either do it in the Select statement by which you get your data from the database to your app's dataset/datatable or, having got the data in its separate parts you can add an expression column to the datatable to do the concatenation.

    For the first approach the SQL Select statement would be on these lines

       "SELECT LastName + ", " + FirstName AS FullName FROM MyTable"

    although I imagine you would want other fields in there as well.

    For the second approach the code would go something like this (assuming that MyTable had in it columns named FirstName and LastName)

            Dim dc As New DataColumn
            With dc
                .ColumnName = "FullName"
                .DataType = GetType(String)
                .Expression = "LastName + ', ' + FirstName"
            End With

    In either case the column you would want to bind to to get the display you want would be FullName.


    Maralans had posted while I was typing this, but I still posted it as (a) it includes the comma between the LastName and FirstName and (b) it includes the alternative approach.  But, I think, any points should be his ;-)
    LVL 5

    Expert Comment

    you are better off binding in VB.  If you move from SQL Server to Oracle (or other way), the concatenation changes from "+" to "||".  You could write a function on SQL Server that concatenates and then re-create it on Oracle if you change databases.

    However, if you bind in VB - you won't have to worry about swapping database back-ends.

    Sancler's suggestion shows how to bind in VB

    Author Comment

    It's has taken me a while to get it to work through the VB side. Thank you all for your help. I was able to use the sql statement earlier thru the QA. I wanted to make it work thru the app. side so it wouldn't be dependant on the database.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now