Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VB and MySQL help please
#1
hello, after a few year of in activeness in VB I'm back trying to make a program for myself to track some data, I followed along with the tutorial that on you tube, it seems to run ok with a couple exceptions. an 1 error that I have no clue how to fix or what it is.

first thing:
forgive me but I dont remember what it's called but if I click on the side bar to step by step the code on the While and the name is not in the database it bypassed the if statement, and if the name is in the database if pops up the msg box but if I hit it again I get an error Invalid attempt to read when reader is closed.

Second thing:
I get error MySqlException, on data2.command2.ExeciteReader
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Case, Powder, Bullet, Primer, Date, Comments)VALUES ('ert','ert','rt','et','erte' at line 1"

full Code below
Code:
Imports MySql.Data.MySqlClient

Public Class frmMain
    Public conn As MySqlConnection
    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim NAME As Boolean = True
        conn = New MySqlConnection("server=db4free.net;Port=3306;user id=PRIVATE;password=PRIVATE;database=PRIVATE")
        conn.Open()
        Dim sqlquery As String = "SELECT * FROM Reloads WHERE Name = '" & txtName.Text & "';"
        Dim data As MySqlDataReader
        Dim adapter As New MySqlDataAdapter
        Dim command As New MySqlCommand
        command.CommandText = sqlquery
        command.Connection = conn
        adapter.SelectCommand = command
        data = command.ExecuteReader
        While data.Read()
            If data.HasRows() = True Then
                MsgBox("Name Already in use!")
                NAME = False
                data.Close()
            Else
                data.Close()
                NAME = True
            End If
        End While
        data.Close()
        If NAME = True Then
            Dim sqlquery2 As String = "INSERT INTO Reloads (Name, Case, Powder, Bullet, Primer, Date, Comments)VALUES ('" & txtName.Text & "','" & txtCase.Text & "','" & txtPowder.Text & "','" & txtBullet.Text & "','" & txtPrimer.Text & "','" & txtDate.Text & "','" & txtComments.Text & "')"
            Dim data2 As MySqlDataReader
            Dim adapter2 As New MySqlDataAdapter
            Dim command2 As New MySqlCommand
            command2.CommandText = sqlquery2
            command2.Connection = conn
            adapter2.SelectCommand = command2
            data2 = command2.ExecuteReader
            MsgBox("Data Added")
        Else
            MsgBox("No Data Entered")
        End If
    End Sub
End Class
#2
update,

have not fixed first problem but I can get the code to post with a single database entry but if I try multiple I get the original error.

Code:
Imports MySql.Data.MySqlClient

Public Class frmMain
    Public conn As MySqlConnection
    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim NAME As Boolean = True
        conn = New MySqlConnection("server=db4free.net;Port=3306;user id=PRIVATE;password=PRIVATE;database=PRIVATE")
        conn.Open()
        Dim sqlquery As String = "SELECT * FROM reloads WHERE Name = '" & txtName.Text & "';"
        Dim data As MySqlDataReader
        Dim adapter As New MySqlDataAdapter
        Dim command As New MySqlCommand
        command.CommandText = sqlquery
        command.Connection = conn
        adapter.SelectCommand = command
        data = command.ExecuteReader
        While data.Read()
            If data.HasRows() = True Then
                MsgBox("Name Already in use!")
                NAME = False
                data.Close()
            Else
                data.Close()
                NAME = True
            End If
        End While
        data.Close()
        If NAME = True Then
            Dim sqlquery2 As String = "INSERT INTO reloads (name) VALUES(1)"
            MsgBox(sqlquery2)
            Dim data2 As MySqlDataReader
            Dim adapter2 As New MySqlDataAdapter
            Dim command2 As New MySqlCommand
            command2.CommandText = sqlquery2
            command2.Connection = conn
            adapter2.SelectCommand = command2
            data2 = command2.ExecuteReader
            MsgBox("Data Added")
        Else
            MsgBox("No Data Entered")
        End If
    End Sub
End Class
#3
Code:
Imports MySql.Data.MySqlClient

    Public Class frmMain
        Public conn As MySqlConnection
        Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub

        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            Dim NAME As Boolean = True
            conn = New MySqlConnection("server=db4free.net;Port=3306;user id=PRIVATE;password=PRIVATE;database=PRIVATE")

            Dim sqlquery As String = "SELECT * FROM reloads WHERE Name = '" & txtName.Text & "';"
            Dim data As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = sqlquery
            command.Connection = conn
            conn.Open()
            adapter.SelectCommand = command
            data = command.ExecuteReader
            While data.Read()
                If data.HasRows() = True Then
                    MsgBox("Name Already in use!")
                    NAME = False
                    data.Close()
                Else
                    data.Close()
                    NAME = True
                End If
            End While
            data.Close()
            conn.close()
            If NAME = True Then
                Dim sqlquery2 As String = "INSERT INTO reloads (name) VALUES(1)"
                MsgBox(sqlquery2)

                Dim data2 As MySqlDataReader
                Dim adapter2 As New MySqlDataAdapter
                Dim command2 As New MySqlCommand
                command2.CommandText = sqlquery2
                command2.Connection = conn
                conn.open()
                adapter2.SelectCommand = command2
                data2 = command2.ExecuteReader
                MsgBox("Data Added")
                conn.close()
            Else
                MsgBox("No Data Entered")
            End If
        End Sub
    End Class

Try closing and opening your connection before you execute new pieces of code. I have inserted these segments into your code if you simply want to copy paste.
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I read a link from mysql databse??? mnxford 9 30,660 04-12-2013, 06:27 PM
Last Post: brandonio21
  How do I use Listview to delete MySQL DB records kismetgerald 10 31,638 11-28-2012, 05:19 PM
Last Post: brandonio21
  [SOLVED] MySQL Update Query - What am I doing wrong? kismetgerald 11 39,942 10-18-2012, 07:16 PM
Last Post: brandonio21
  How do I fill my form with MySQL data based on Combobox? kismetgerald 8 24,959 10-14-2012, 09:05 PM
Last Post: brandonio21
  Help with creating an INSERT statement using MySQL kismetgerald 4 15,818 08-30-2012, 03:03 PM
Last Post: brandonio21
  MySQL Database Issue Moldraxian 7 27,559 08-13-2012, 08:58 PM
Last Post: brandonio21
  MySql Database Queries in VB.Net Moldraxian 6 20,736 07-26-2012, 03:44 PM
Last Post: Moldraxian
  How to Package MySql Connector and MySql Program? Moldraxian 3 13,873 07-20-2012, 12:23 PM
Last Post: brandonio21
  MySql Help Blackrobot 3 13,600 04-06-2012, 03:49 AM
Last Post: xolara
  MySQL Help -Kid- 6 23,445 02-20-2012, 11:57 AM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)