03-20-2012, 10:41 PM
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
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