Hi this is my first time using this site, so please bare with me. I'm also pretty new at coding and got thrown into connecting to a database in one of my classes. But anywho, I keep receiving a syntax error and I was wondering if anyone could help me figure out why. Thank you!

Imports System.Data
Imports System.Data.SqlClient

Public Class PatientForm1

    Dim con As New SqlClient.SqlConnection("deleted for privacy")
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles lname.TextChanged

    End Sub

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

    End Sub

    Private Sub Label14_Click(sender As Object, e As EventArgs) Handles Label14.Click

    End Sub

    Private Sub Label10_Click(sender As Object, e As EventArgs) Handles Label10.Click

    End Sub

    Private Sub TextBox12_TextChanged(sender As Object, e As EventArgs) Handles TextBox12.TextChanged

    End Sub

    Private Sub TextBox20_TextChanged(sender As Object, e As EventArgs) Handles TextBox20.TextChanged

    End Sub

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

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles bn1.Click

        con.Open()
        Dim query As New SqlCommand(("INSERT INTO Table VALUES ('" & PatientID.Text & "', '" & fname.Text & "'," & lname.Text & "')"), con)
        'Dim mycommand As New SqlCommand(query, con)

        If query.ExecuteNonQuery() Then
            MessageBox.Show("Patient Added")
        Else
            MessageBox.Show("Error patient wasn't added.")
        End If

        con.Close()

        'EditAdd.Show()
        'Me.Close()
    End Sub

    Private Sub Label21_Click(sender As Object, e As EventArgs) Handles Label21.Click

    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles fname.TextChanged

    End Sub

    Private Sub bn2_Click(sender As Object, e As EventArgs) Handles bn2.Click
        Me.Close()
        EditAdd.Show()
    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub

End Class

Recommended Answers

But which line has the syntax error? VB has always shown the line in question which when you place code here you can speed things along by posting the code, preview your post and write which line along with the error text.

Also, lines 11 through 33, 52 to 59 …

Jump to Post

I suggest you do something like

dim qry as string = "INSERT INTO Table VALUES ('" & PatientID.Text & "', '" & fname.Text & "'," & lname.Text & "')"

and examine the contents of qry. There is no way for us to see what the query is without knowing the …

Jump to Post

All 2 Replies

But which line has the syntax error? VB has always shown the line in question which when you place code here you can speed things along by posting the code, preview your post and write which line along with the error text.

Also, lines 11 through 33, 52 to 59 along with 66 to 68 do nothing. Those were not needed here.

commented: So I believe the error is line 41, but it also says something along the lines of there's an error: "syntax near the string Table" +0

I suggest you do something like

dim qry as string = "INSERT INTO Table VALUES ('" & PatientID.Text & "', '" & fname.Text & "'," & lname.Text & "')"

and examine the contents of qry. There is no way for us to see what the query is without knowing the values of the text fields that make it up.