8 Replies - 8120 Views - Last Post: 24 January 2012 - 09:02 AM Rate Topic: -----

#1 sahasraa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-January 12

Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 07:45 AM

It's simple question. I am confused since I am new to vb.net and windows form.
I would like to display combining of one column (lastname+firstname) in the list box control from the query.
My code display something.

But, it looks like

listviewitem.{ben}
lietviewitm.{sammy}

etc.

MY code:
<after all connection etc.>
do while reader.read()
dim newitem as new listviewitem()
newitem.text = reader("lastname")
listcustomer.items.add(newitem)

loop
reader.close()


thanks.

This post has been edited by modi123_1: 24 January 2012 - 08:37 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Display data in the list box from the query in windows form (vb.net)

#2 ash.burlaczenko  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 4
  • Joined: 24-January 12

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 07:54 AM

You need to join both fields together. How about something like this

Dim item as new ListViewItem()
Dim text as new String()
text = reader("firstname") + " " + reader("lastname")
item.text = text
listcustomer.items.add(item)


Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#3 sahasraa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-January 12

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 08:25 AM

Hello:


Thanks for your help. It displays the name correctly. But, Listcontrol still displays

listviewitem: {yang,Jon}
listviewtem: {huang.eugene}

Any thoughts why names displays with "listviewitem:{}" ? where it comes from?

thanks.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,777
  • Joined: 12-June 08

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 08:38 AM

Let's see all your code where this is being loaded and then displayed...
Was This Post Helpful? 0
  • +
  • -

#5 sahasraa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-January 12

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 08:46 AM

Here is the code below. Thanks for the help.
 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load



        Dim selectSQL = "Select * from DimCustomer"
        Dim connectionString = "Data Source=XXXX;Initial Catalog=AdventureWorksDW;Integrated Security=True"
        Dim Connection As New SqlConnection(connectionString)
        Dim cmd As New SqlCommand(selectSQL, Connection)
        Dim reader As SqlDataReader


        Try
            Connection.Open()
            reader = cmd.ExecuteReader()

            Do While reader.Read()
                Dim newItem As New ListViewItem()
                Dim texts As String
                texts = reader("LastName") & "," & reader("FirstName")
                newItem.Text = texts
                listCustomer.Items.Add(newItem)

            Loop
            reader.Close()
        Catch ex As Exception
            lblresult.Text = "Error reading list of names."
            lblresult.Text &= ex.Message
        Finally
            Connection.Close()
        End Try
    End Sub

This post has been edited by modi123_1: 24 January 2012 - 08:47 AM
Reason for edit:: please use code tags

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,777
  • Joined: 12-June 08

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 08:49 AM

How are you validating that "listviewtem: {huang.eugene}" is exactly what shows up in the list box?
Was This Post Helpful? 0
  • +
  • -

#7 sahasraa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-January 12

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 08:54 AM

Yes..It is..
Was This Post Helpful? 0
  • +
  • -

#8 ash.burlaczenko  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 4
  • Joined: 24-January 12

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 08:57 AM

The string you are seeing is the
toString()
method of the ListViewItem. If your not doing anything else with the item you would better to add just the string to the List.

texts = reader("LastName") & "," & reader("FirstName")
listCustomer.Items.Add(texts)


Was This Post Helpful? 2
  • +
  • -

#9 sahasraa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-January 12

Re: Display data in the list box from the query in windows form (vb.net)

Posted 24 January 2012 - 09:02 AM

Excellent, It works. Thanks, ash.burlazenko.

I have another question. where do we use 'fill' method?

thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1