9 Replies - 670 Views - Last Post: 07 March 2013 - 06:29 PM Rate Topic: -----

#1 aw350me89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 13

Error retreiving data from MySql to Label

Posted 07 March 2013 - 05:20 PM

Imports MySql.Data.MySqlClient
Public Class Form4

    Dim ServerString As String = "server=;Port=; user id=; password=; database="
    Dim SQLConnection As MySqlConnection = New MySqlConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SQLConnection.ConnectionString = ServerString

        Try
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                MsgBox("Successfully connected to MySQL database.")
            Else
                SQLConnection.Close()
                MsgBox("Connection is closed.")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub calibra_query(ByRef SQLStatement As String)
        Dim cmd As MySqlCommand = New MySqlCommand

        With cmd
            .CommandText = SQLStatement
            .CommandType = CommandType.Text
            .Connection = SQLConnection
        End With

        Dim myReader As MySqlDataReader = myCommand.ExecuteReader

        If myReader.Read Then
            Label1.Text = myReader.GetString(0)


        End If

        myReader.Close()
        SQLConnection.Close()
        MsgBox("Records Successfully Retrieved")
        SQLConnection.Dispose()
    End Sub 


I am trying to retrieve data from MySQL and display in label1.text but I get this error:
Error 1 'myCommand' is not declared. It may be inaccessible due to its protection level.

Regards
Marc

Is This A Good Question/Topic? 0
  • +

Replies To: Error retreiving data from MySql to Label

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 05:26 PM

The name of the MySqlCommand instance in your calibra_query method is cmd not myCommand.
Was This Post Helpful? 1
  • +
  • -

#3 aw350me89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 13

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 05:37 PM

Thanks I thought that just after I posted.

But after I fixed that, my Label1.text is still not showing the MySql value of username which is 'ADMIN'.
Any ideas?

Regards
Marc

Thanks I thought that just after I posted.

But after I fixed that, my Label1.text is still not showing the MySql value of username which is 'ADMIN'.
Any ideas?

Regards
Marc
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 05:40 PM

What are you passing into the routine as far as an SQLStatement? I don't see one in that code.
Was This Post Helpful? 0
  • +
  • -

#5 aw350me89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 13

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 05:54 PM

View PostCharlieMay, on 07 March 2013 - 05:40 PM, said:

What are you passing into the routine as far as an SQLStatement? I don't see one in that code.


Imports MySql.Data.MySqlClient
Public Class Form4

    Dim ServerString As String = "server=;Port=; user id=; password=; database=gamepsych"
    Dim SQLConnection As MySqlConnection = New MySqlConnection

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SQLConnection.ConnectionString = ServerString

        Try
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                MsgBox("Successfully connected to MySQL database.")
            Else
                SQLConnection.Close()
                MsgBox("Connection is closed.")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub gamepsych_query(ByRef SQLStatement As String)
        Dim cmd As MySqlCommand = New MySqlCommand

        With cmd
            .CommandText = SQLStatement
            .CommandType = CommandType.Text
            .Connection = SQLConnection
        End With

        Dim myReader As MySqlDataReader = cmd.ExecuteReader

        If myReader.Read Then
            TextBox1.Text = myReader.GetString(0)
        End If

        myReader.Close()
        SQLConnection.Close()
        MsgBox("Records Successfully Retrieved")
        SQLConnection.Dispose()
    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
        Form6.Show()
    End Sub


End Class


now I'm confused
SQL STATMENT?

(also I have removed server info)
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 05:59 PM

Yea, you see where you're passing in a string to that gamepsych_query? That needs to be a valid SQL statement that the database understands
SELECT * FROM MyTable WHERE SomeField = Something


I'm betting if you put this right before the If myReader.Read it will display False
MsgBox(myReader.HasRows)


OK, I was assuming you were passing this somehow but now I'm pretty sure you're not even running that piece of code.

Somewhere you will need a line something like:
gamepsych_query("SELECT * From SomeTable")


Otherwise, you're not even running the subroutine.
Was This Post Helpful? 0
  • +
  • -

#7 aw350me89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 13

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 06:05 PM

Im still confused what should my code kinda look like?
You Would think something as simple as this would be easy
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 06:09 PM

Oh it is but...

First. Do you understand SQL (Structured Query Language)? It's used to pull information from a database.

Most of what you have would appear to work but you
1. Have to know the SQL statement needed to pull the proper information from the database
2. Call your subroutine while passing it that sql statement.

If you don't know how to talk to the database in its own language, then you are going to have a hard time. I would suggest a site like http://www.w3schools...sql/default.asp to at least gain a basic understanding.

This post has been edited by CharlieMay: 07 March 2013 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#9 aw350me89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 13

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 06:20 PM

I Have this
SELECT * FROM MyTable WHERE SomeField = Something

Presuming 'MyTable' is my 'Users' table in PhpAdmin But what is SomeField and SomeWhere?
Where do I place this line of code?

and this
MsgBox(myReader.HasRows)

What is this?

and this
gamepsych_query("SELECT * From SomeTable")

what is SomeTable ? where do I place this line of code?

Sorry for all the questions and am very thankful
I have done fine with my project up to today lol.

Regards
Marc
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Error retreiving data from MySql to Label

Posted 07 March 2013 - 06:29 PM

View Postaw350me89, on 07 March 2013 - 08:20 PM, said:

I Have this
SELECT * FROM MyTable WHERE SomeField = Something

Presuming 'MyTable' is my 'Users' table in PhpAdmin But what is SomeField and SomeWhere?
Where do I place this line of code?
That is not code, it's a string that represents a format of a language that the database understands. Again, reference the link above. Until you have an understanding of basic SQL it's going to be very hard for someone to get you through this. Once you have that understanding, an answer should become apparent.


Ignore this, I made an assumption when I wrote that.
and this
MsgBox(myReader.HasRows)

What is this?

and this
gamepsych_query("SELECT * From SomeTable")

what is SomeTable ? where do I place this line of code?
Wherever you are wanting to trigger the routine to pull the information from the database. A button click or on form load. That is up to you and the design of your program.

Sorry for all the questions and am very thankful
I have done fine with my project up to today lol.

Regards
Marc

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1