3 Replies - 548 Views - Last Post: 10 May 2013 - 11:19 AM Rate Topic: -----

#1 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 171
  • Joined: 07-October 12

Looping MySQL Results in Vb.net

Posted 10 May 2013 - 10:34 AM

In PHP you can have a Query Result Loop and Show each query. Is this possible within VB.net? Essentially I want it to pull up name, type and description from a SQL Table and then it's going to hopefully Loop.
IE.
Dim query As String = "SELECT * FROM games WHERE type = 'FPS' ORDER BY vote_count"
        Dim connStr As String = "*************"
        Dim connection As New MySqlConnection(connStr)
        Dim cmd As New MySqlCommand(query, connection)
        connection.Open()
        Dim reader As MySqlDataReader
        reader = cmd.ExecuteReader()
        While reader.Read()
            Label1.Text = (reader.GetString("name"))
            Label2.Text = (reader.GetString("description"))
        End While


I know this would show the correct information if I was trying to display one result, but what I'm trying to do is get it to pull all results and show them seperately and in order. Is this even possible?

Is This A Good Question/Topic? 0
  • +

Replies To: Looping MySQL Results in Vb.net

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Looping MySQL Results in Vb.net

Posted 10 May 2013 - 10:39 AM

Well, very quickly Label1 and lable2 is going to update until the reader runs out of results which will basically appear to you that the labels are being set to the values of the last row in the reader.

Perhaps, adding these two pieces of information into an object and filling a listbox with the object would be a better solution. Then when the item is clicked in the listbox, you could fill the labels with the properties of that selected item.

Or even setting a timer to move through the list(of yourobject) at a given interval (depending on what you're actually trying to achieve)

You could also use a datagridview and display both columns of each row in a tabular fashion.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,360
  • Joined: 12-December 12

Re: Looping MySQL Results in Vb.net

Posted 10 May 2013 - 10:41 AM

You are already looping through the resultset(?) it is just that you are continually over-writing the same labels' text. If you want to collect all the data then you could concatenate:

Dim sResultName As String = String.Empty
Dim sResultDesc As String = String.Empty

'in the loop
sResultName &= reader.GetString("name") & vblf
sResultDesc &= reader.GetString("description") & vblf

'then later
Label1.Text = sResultName

(or use a ListBox)

.. and, of course, rename your labels to something useful :whistling:
Was This Post Helpful? 0
  • +
  • -

#4 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 171
  • Joined: 07-October 12

Re: Looping MySQL Results in Vb.net

Posted 10 May 2013 - 11:19 AM

I'm essentially just going to have a list of games, but since I don't like the datagrid view I'm going to have a webbrowser integrated into it that retrieves a page that performs the loop array via PHP. Now to find out how to send a setting to a vb.net application from PHP :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1