Results 1 to 4 of 4

Thread: Selecting a single column from database table and populate it in a listbox

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    44

    Selecting a single column from database table and populate it in a listbox

    i have a code here that i want to show the data from the database of a certain column,
    HOWEVER THE LISTBOX SHOWS:
    System.Data.DataRow
    System.Data.DataRow
    System.Data.DataRow
    System.Data.DataRow
    System.Data.DataRow

    instead of the data in the database.

    this is my current code:

    Code:
    Dim conn As SqlCeConnection = Nothing
            Try
                conn = New SqlCeConnection("Data Source=" & userSpecifiedPath)
                conn.Open()
                Dim cmd As SqlCeCommand = conn.CreateCommand()
                Dim dAdp As SqlCeDataAdapter = New SqlCeDataAdapter()
                Dim ds As New DataSet
    
                cmd.CommandText = String.Format("SELECT STATUS FROM DELIVERY")
                dAdp.SelectCommand = cmd
                cmd.ExecuteReader()
    
                dAdp.Fill(ds)
                ListBox2.DataSource = ds.Tables(0)
                cmd.ExecuteNonQuery()
            Catch ex As Exception
            Finally
                conn.Close()
            End Try
    ALREADY SOLVED
    Last edited by bea19_ventura; May 1st, 2013 at 08:12 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Selecting a single column from database table and populate it in a listbox

    You need to set the DisplayMember of the ListBox to tell it which column or property of the bound list to display. If you don't, it simply calls ToString on each item to get a text value to display. While it's not essential, it should be set BEFORE setting the DataSource.

    By the way, your code is a bit all over the place. You are actually executing your query three times. Get rid of the ExecuteReader and ExecuteNonQuery calls. You might like to follow the CodeBank link in my signature and check out my Retrieving & Saving Data thread to learn when to use which options.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    44

    Re: Selecting a single column from database table and populate it in a listbox

    i have in my listbox data with spaces. i want to separate the data before the space and the data after the space. (ex. abc 123) how can i separate the data and put the other in another listbox.

    by the way thanks for the advice in #2.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Selecting a single column from database table and populate it in a listbox

    The topic you raised in your original post has been resolved. Please mark this thread as Resolved to indicate that and start a new thread for a new topic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width