Results 1 to 9 of 9

Thread: Help connecting to MySQL with .NET Plz :D

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    17

    Help connecting to MySQL with .NET Plz :D

    Hi.. I am trying to make a very simple program that connects to a MySQL database using VB.NET. My program only has one form and one label. I setup my MySQL database to have a table and one field (a VARCHAR) called "Tab1". I manually inserted the value "CLOSED" into the Tab1 field using PHPMyAdmin. I want my program to change the value of the field to OPEN/CLOSED and I also want the label text on my form to change too when it's clicked.

    So far, I've added the MySQL.data reference to my project and added:

    Code:
    Imports System
    Imports System.Data
    Imports MySql.Data.MySqlClient
    To the General_Declarations

    Also I declared a global variable containing a MySQL Connection:

    Code:
    Public Class Form1
    
        Dim conn As New MySqlConnection
    Here is my function that connects to MySQL:

    Code:
    Private Function Connect(ByVal server As String, ByRef user As String, ByRef password As String, ByRef database As String)
            ' Connection string with MySQL Info
            conn.ConnectionString = "server=" + server + ";" _
            & "user id=" + user + ";" _
            & "password=" + password + ";" _
            & "database=" + database + ";"
            Try
                conn.Open()
                Return True
            Catch ex As MySqlException
                Return MsgBox(ex.Message)
            End Try
        End Function

    I made the program connect to MySQL on Form_Load like this:
    Code:
    Connect("db4free.net", "boomun", "*******", "boomdb")
    And it connects fine but here is where I have the trouble.... I want the field to change from CLOSED to OPEN when I click Label1. It does, but it doesn't change back on the second click. Somehow I need a working code to Update the Tab1 field in my table.

    Here is what I have so far:

    Code:
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
            Dim myCommand As New MySqlCommand
            Dim myAdapter As New MySqlDataAdapter
            Dim myData As MySqlDataReader
            Dim SQL As String
    
            SQL = "SELECT * FROM `boomtable` WHERE `Tab1` = 'CLOSED'"
    
            myCommand.Connection = conn
            myCommand.CommandText = SQL
            myAdapter.SelectCommand = myCommand
            Try
                myData = myCommand.ExecuteReader()
                myData.Read()
                If myData.HasRows = 0 Then
                    Label1.Text = "CLOSED"
                    SQL = " UPDATE boomtable SET Tab1 = 'CLOSED' WHERE Tab1 = 'OPEN' "
                    SQL.ExecuteNonQuery()
                    myData.Close()
                Else
                    Label1.Text = "OPEN"
                    SQL = " UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED' "
                    SQL.ExecuteNonQuery()
                    myData.Close()
                End If
            Catch ex As MySqlException
                MsgBox(ex.Message)
            End Try
    
        End Sub

    VB gives me an error and highlights SQL.ExecuteNonQuery() and the error message is 'ExecuteNonQuery' is not a member of 'String'. Can anyone help???! Thanks



    Here is the entire code of the project all together:


    Code:
    Imports System
    Imports System.Data
    Imports MySql.Data.MySqlClient
    
    Public Class Form1
    
        Dim conn As New MySqlConnection
    
        Private Function Connect(ByVal server As String, ByRef user As String, ByRef password As String, ByRef database As String)
            ' Connection string with MySQL Info
            conn.ConnectionString = "server=" + server + ";" _
            & "user id=" + user + ";" _
            & "password=" + password + ";" _
            & "database=" + database + ";"
            Try
                conn.Open()
                Return True
            Catch ex As MySqlException
                Return MsgBox(ex.Message)
            End Try
        End Function
    
        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
            Dim myCommand As New MySqlCommand
            Dim myAdapter As New MySqlDataAdapter
            Dim myData As MySqlDataReader
            Dim SQL As String
    
            SQL = "SELECT * FROM `boomtable` WHERE `Tab1` = 'CLOSED'"
    
            myCommand.Connection = conn
            myCommand.CommandText = SQL
            myAdapter.SelectCommand = myCommand
            Try
                myData = myCommand.ExecuteReader()
                myData.Read()
                If myData.HasRows = 0 Then
                    Label1.Text = "CLOSED"
                    SQL = " UPDATE boomtable SET Tab1 = 'CLOSED' WHERE Tab1 = 'OPEN' "
                    SQL.ExecuteNonQuery()
                    myData.Close()
                Else
                    Label1.Text = "OPEN"
                    SQL = " UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED' "
                    SQL.ExecuteNonQuery()
                    myData.Close()
                End If
            Catch ex As MySqlException
                MsgBox(ex.Message)
            End Try
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Connect("db4free.net", "boomun", "boom123", "boomdb")
        End Sub
    
    End Class

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Help connecting to MySQL with .NET Plz :D

    Hi.. Welcome to the forums...

    You have declared SQL as a String and in the code, you are doing this:
    Code:
    SQL = " UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED' "
                    SQL.ExecuteNonQuery()
    where, the second line is invalid. Because, SQL is not a MySqlCommand object !. You've declared it as a String !


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    17

    Re: Help connecting to MySQL with .NET Plz :D

    Quote Originally Posted by akhileshbc View Post
    Hi.. Welcome to the forums...

    You have declared SQL as a String and in the code, you are doing this:
    Code:
    SQL = " UPDATE boomtable SET Tab1 = 'OPEN' WHERE Tab1 = 'CLOSED' "
                    SQL.ExecuteNonQuery()
    where, the second line is invalid. Because, SQL is not a MySqlCommand object !. You've declared it as a String !

    Any suggestions of how it should look instead?

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Help connecting to MySQL with .NET Plz :D

    Use a MySqlCommand object and with the SQL query, call the ExecuteNonQuery() method.

    For example:
    Code:
    SQL = " UPDATE boomtable SET Tab1 = 'CLOSED' WHERE Tab1 = 'OPEN' "
    myCommand.CommandText = SQL
    myCommand.ExecuteNonQuery()

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    17

    Re: Help connecting to MySQL with .NET Plz :D

    Thank you... It executes fine but I get a mysql error when clicking the label.. "There is already an open DataReader associated with this connection that must be closed first"

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Help connecting to MySQL with .NET Plz :D

    hmm... Try adding this line before that:
    Code:
    myAdapter.Close

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    17

    Re: Help connecting to MySQL with .NET Plz :D

    When I add it I get an error 'Close' is not a member of 'MySql.Data.MySqlClient.MySqlDataAdapter'.

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Help connecting to MySQL with .NET Plz :D

    Sorry. Try this:
    Code:
    myAdapter.Dispose

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  9. #9
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Help connecting to MySQL with .NET Plz :D

    The error:
    Quote Originally Posted by BooMGiRL View Post
    "There is already an open DataReader associated with this connection that must be closed first"
    Is telling you you have an open Reader. You opened a reader without closing it. check you code and close the open reader. You can not open a new one without first closing the prior open one.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

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