[VB.NET] MySQL Listbox Problem [Can't Load]

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3073 Views - Last Post: 08 February 2012 - 07:44 AM Rate Topic: -----

#1 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

[VB.NET] MySQL Listbox Problem [Can't Load]

Posted 06 February 2012 - 12:16 PM

Hello guys and gals!
I need some help :(
Basically i need a code to put some links in a listbox, one after another. For example:

This is my table

ID-----URL--------
1 Google.com
2 Yahoo.com
and so on.

I have already opened a mysql connection, and i just need to put all the URLs in a listbox, one after another.
I tried this:
ListBox1.Items.Add("URL")
, and obviousely didn't work.

Any other ideas? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: [VB.NET] MySQL Listbox Problem [Can't Load]

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 06 February 2012 - 12:26 PM

Why not use a dataset, fill it with a sql SELECT call to your database, and use that?
Was This Post Helpful? 0
  • +
  • -

#3 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 10:31 AM

View Postmodi123_1, on 06 February 2012 - 12:26 PM, said:

Why not use a dataset, fill it with a sql SELECT call to your database, and use that?

I tried to do that, but it didn't work for me ;(
By the way, if it helps, this is my code:
Imports MySql.Data.MySqlClient
Public Class Form1
    Dim LinksAdapter As New MySqlDataAdapter
    Dim LinksData As New DataTable
    Dim LinksCommand As New MySqlCommand
    Dim SQL2 As String
    Dim MysqlConn As MySqlConnection
    Dim UserCommand As New MySqlCommand
    Dim SQL As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MysqlConn = New MySqlConnection()

        ' Define the SQL to grab data from table.
        SQL = "UPDATE users SET points= points +1 WHERE username = 'aristide'"
        SQL2 = "SELECT * FROM users"
        'Connection String
        MysqlConn.ConnectionString = "server=localhost;" _
        & "user id=aristide;" _
        & "password=xxxxxx;" _
        & "database=aristide"

        ' Try, Catch, Finally
        Try
            MysqlConn.Open()

            UserCommand.Connection = MysqlConn
            UserCommand.CommandText = SQL
            UserCommand.ExecuteNonQuery()

            LinksCommand.Connection = MysqlConn
            LinksCommand.CommandText = SQL2
            LinksCommand.ExecuteNonQuery()

            LinksAdapter.SelectCommand = LinksCommand
            LinksAdapter.Fill(LinksData)

           ListBox1.Items.Add(LinksData


        Catch myerror As MySqlException
            MessageBox.Show("Cannot connect to database: " & myerror.Message)
        Finally
            MysqlConn.Close()
            MysqlConn.Dispose()
        End Try
    End Sub
End Class


What can i do ?!?
Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 10:36 AM

Quote

I tried to do that, but it didn't work for me ;(

In what way did it not work?
Was This Post Helpful? 0
  • +
  • -

#5 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 10:42 AM

View Postmodi123_1, on 07 February 2012 - 10:36 AM, said:

Quote

I tried to do that, but it didn't work for me ;(

In what way did it not work?

That basically the listbox remains idle without doing anything. Is there perhaps a wrong piece of code in my code?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 11:10 AM

   ListBox1.Items.Add(LinksData

This looks pretty broken.. perhaps setting the list box's DATASOURCE to the dataset might prove helpful.
Was This Post Helpful? 0
  • +
  • -

#7 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 11:49 AM

View Postmodi123_1, on 07 February 2012 - 11:10 AM, said:

   ListBox1.Items.Add(LinksData

This looks pretty broken.. perhaps setting the list box's DATASOURCE to the dataset might prove helpful.

Yeah, i copied it uncorrectly, but its right.
And... i dont really understand what you mean! Sorry! Can you please explain a bit further?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 11:56 AM

*sigh* Have you even looked at the documentation on the list box's "data source" property?

http://msdn.microsof...=vs.100%29.aspx
Was This Post Helpful? 0
  • +
  • -

#9 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 12:19 PM

View Postmodi123_1, on 07 February 2012 - 11:56 AM, said:

*sigh* Have you even looked at the documentation on the list box's "data source" property?

http://msdn.microsof...=vs.100%29.aspx

I have done now, even though i still can't connect it to MySQL (And still didn't understand much of it) :(
Sorry, but i have never worked with MySQL in vb.net before!
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 12:26 PM

Wait - what? Are you not able to connect to MYSQL or you are not able to assign the data to the list box?

Post #3 - why are you using 'execute no query' on a query statment? You query a database with a select statement right?


You may want to read up on database interactions in our tutorials..
OleDb Basics in VB.Net Rate Topic

A Really Simple Database Create a Database using Access & VB.net Express 2008
Was This Post Helpful? 0
  • +
  • -

#11 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 01:10 PM

View Postmodi123_1, on 07 February 2012 - 12:26 PM, said:

Wait - what? Are you not able to connect to MYSQL or you are not able to assign the data to the list box?

Post #3 - why are you using 'execute no query' on a query statment? You query a database with a select statement right?


You may want to read up on database interactions in our tutorials..
OleDb Basics in VB.Net Rate Topic

A Really Simple Database Create a Database using Access & VB.net Express 2008

I need to assign data to the listbox.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 01:22 PM

you would want to set up something like this:
        ListBox1.DataSource = <data set>
        ListBox1.DisplayMember = <column name to show the user>
        ListBox1.ValueMember = <column name to be the value when an item is selected>


You would still want to read why the 'execute not query' won't work with a sql query...
Was This Post Helpful? 0
  • +
  • -

#13 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 01:31 PM

View Postmodi123_1, on 07 February 2012 - 01:22 PM, said:

you would want to set up something like this:
        ListBox1.DataSource = <data set>
        ListBox1.DisplayMember = <column name to show the user>
        ListBox1.ValueMember = <column name to be the value when an item is selected>


You would still want to read why the 'execute not query' won't work with a sql query...

hmmm not really, dont look at the user thingy, that is a separate part of my program. No, i have a different table, with rows wich have URls in them. Every row has an URL. I want every URL in that table to be displayed in the listbox. And i cant find a way to do that!
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,841
  • Joined: 12-June 08

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 01:38 PM

You are not listening to what I am telling you. Either you need to take your collection of strings and add them manually to the item array in the list box or you assign a datasource and specify columns and a dataaset.

.. but all of this is irrelevant if you cannot pull your data back right.. adn 'execute non query' is not the way to go for a select query.

example:
        Dim bar As New ListBox


        Dim sTempArr As New List(Of String)
        sTempArr.Add("you")
        sTempArr.Add("not")
        sTempArr.Add("listening")
        sTempArr.Add("to")
        sTempArr.Add("me")

        bar.Items.Clear()

        For Each temp As String In sTempArr
            bar.Items.Add(temp)
        Next



Was This Post Helpful? 0
  • +
  • -

#15 aristide   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-December 11

Re: [VB.NET] MySQL Listbox Problem [Can't Load]

Posted 07 February 2012 - 11:49 PM

View Postmodi123_1, on 07 February 2012 - 01:38 PM, said:

You are not listening to what I am telling you. Either you need to take your collection of strings and add them manually to the item array in the list box or you assign a datasource and specify columns and a dataaset.

.. but all of this is irrelevant if you cannot pull your data back right.. adn 'execute non query' is not the way to go for a select query.

example:
        Dim bar As New ListBox


        Dim sTempArr As New List(Of String)
        sTempArr.Add("you")
        sTempArr.Add("not")
        sTempArr.Add("listening")
        sTempArr.Add("to")
        sTempArr.Add("me")

        bar.Items.Clear()

        For Each temp As String In sTempArr
            bar.Items.Add(temp)
        Next



Thanks, but again, my question is similiar. How can i add a cell of my table in stead of "you" and all the others? Beacause everytime i connect the table changes, and i need it to be automatic..
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2