简体   繁体   中英

SYNTAX ERROR : Insert SQL VB.NET

I am a very beginner in coding, changing career and doing self learning+enrolled to a self funded course to get into programming. I posted some similar question earlier but now I have more bigger issue in my code! It was partially working until I mess it up :(.. I have a form which the user will fill then store it into Microsoft Access database, when it was working the first detailed user entered was perfectly stored into my table but second deatiled it skipped the first field (firstname in my case) and stored the firstname to Surname(my second field) basically every time it was skiping one field and starting from the following field ( Hope I am clear!) .. Now that I changed my code, It keeps giving me Syntax error and I checked different tutorials and different syntax ( they all use different syntax) but this error is VERY persistant!

Please, help me.. below is mycode

Imports System.Data.OleDb

Public Class FirstCode
    Inherits System.Web.UI.Page
    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles save.Click
        provider = "Provider= Microsoft.ACE.OLEDB.15.0;Data Source = "
        dataFile = "C:\Users\Data.accdb"
        connString = provider & dataFile
        myConnection.ConnectionString = connString
        myConnection.Open()
        Dim str As String

        str = "INSERT INTO RegisterList (FirstName,Surname,Email,Location,ZipCode,Num,[Password]) + VALUES (@FirstName,@Surname,@Email,@Location,@ZipCode,@Num,@Pass)"

        str = " Insert into RegisterList (FirstName) Values(@FirstName)"
        str = " Insert into RegisterList (Surname) Values(@Surname)"
        str = " Insert into RegisterList (Email) Values(@Email)"
        str = " Insert into RegisterList (Location) Values(@Location)"
        str = " Insert into RegisterList (ZipCode) Values(@ZipCode)"
        str = " Insert into RegisterList (Num) Values(@Num)"
        str = " Insert into RegisterList (Password) Values(@Pass)"

        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text)
        cmd.Parameters.AddWithValue("@Surname", TextBox3.Text)
        cmd.Parameters.AddWithValue("@Email", TextBox3.Text)
        cmd.Parameters.AddWithValue("@Location", DropDownList1.Text)
        cmd.Parameters.AddWithValue("@CourtName", TextBox4.Text)
        cmd.Parameters.AddWithValue("@Homecare", TextBox5.Text)
        cmd.Parameters.AddWithValue("@Pass", TextBox6.Text)

        cmd.ExecuteNonQuery()
        myConnection.Close()

    End Sub

    Protected Sub Button1_Click1(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub
End Class
  1. This looks like some flavor of VB, not VBScript. Asp-classic uses either VBScript or JavaScript, not VB.

  2. Think about what's happening here:

     ... str = " Insert into RegisterList (FirstName) Values(@FirstName)" str = " Insert into RegisterList (Surname) Values(@Surname)" ...

    What is the value of str after the second line? So when your code gets to

    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

    what is the value of str ? (The above is definitely VB, not VBScript. VBScript can't do dim x As , nevermind New .)

  3. Why is there a + sign in the following line? (I've broken it into multiple lines for readability.)

     str = "INSERT INTO RegisterList" _ & " (FirstName,Surname,Email,Location,ZipCode,Num,[Password])" _ & " + VALUES (@FirstName,@Surname,@Email,@Location,@ZipCode,@Num,@Pass)" ' ^ that plus sign - why is it there?
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles save.Click
provider = "Provider= Microsoft.ACE.OLEDB.15.0;Data Source = "
dataFile = "C:\Users\Data.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String

str = "INSERT INTO RegisterList (FirstName,Surname,Email,Location,ZipCode,Num,[Password]) VALUES(@FirstName,@Surname,@Email,@Location,@ZipCode,@Num,@Pass)"

Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text)
cmd.Parameters.AddWithValue("@Surname", TextBox3.Text)
cmd.Parameters.AddWithValue("@Email", TextBox3.Text)
cmd.Parameters.AddWithValue("@Location", DropDownList1.Text)
cmd.Parameters.AddWithValue("@ZipCode", TextBox4.Text)
cmd.Parameters.AddWithValue("@Num", TextBox5.Text)
cmd.Parameters.AddWithValue("@Pass", TextBox6.Text)


cmd.ExecuteNonQuery()
myConnection.Close()




End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM