想要将记录添加到我的数据库,但它给了我一个例外

时间:2015-01-05 10:34:36

标签: vb.net

所以基本上我有这个项目有一个注册表格。我已经完成了所有必要的步骤但是当我输入文本框时它给了我一个异常 "Syntax error (missing operator) in query expression 'Paula Angela'." 我想因为它有一个空格或者其他东西,因为当我再次运行它并使文本中没有任何空格时,它起作用了。我想知道我应该放什么代码,以便即使记录中有空格,项目也可以正常运行?

抱歉,这是我的代码

公共类Create_an_account

Dim cnn As New OleDb.OleDbConnection

 Private Sub txtConfirm_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtConfirm.TextChanged

    If Not txtConfirm.Text = txtPass.Text Then
        Label11.Text = "Passwords do not match"
    Else
        Label11.Text = ""
    End If
End Sub

Private Sub RefreshData()
    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If

    Dim da As New OleDb.OleDbDataAdapter("SELECT [First Name], " & _
                                 "[Last Name], [Address], [Contact Number], [Username], " & _
                                 "[Email Address], [Password], [Credit Card Number] " & _
                                 " FROM Accounts ORDER BY [First Name]", cnn)
    Dim dt As New DataTable

    da.Fill(dt)

    My_Account.dgvAccount.DataSource = dt

    cnn.Close()
End Sub



Private Sub btnRegister_Click(sender As System.Object, e As System.EventArgs) Handles btnRegister.Click

    If txtFirst.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label16.Text = "*"
    End If

    If txtLast.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label15.Text = "*"
    End If

    If txtAddress.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label14.Text = "*"
    End If

    If txtContact.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label13.Text = "*"
    End If

    If txtUsername.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label17.Text = "*"
    End If

    If txtEmail.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label18.Text = "*"
    End If

    If txtPass.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label19.Text = "*"
    End If

    If txtConfirm.Text = "" Then
        Me.Show()
        Label12.Text = "Some required information is missing. Please fill up marked label/s."
        Label20.Text = "*"
    End If

    If txtFirst.Text <> "" And txtLast.Text <> "" And txtAddress.Text <> "" And txtContact.Text <> "" And _
       txtUsername.Text <> "" And txtEmail.Text <> "" And txtPass.Text <> "" And txtConfirm.Text <> "" Then

        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If


        cmd.Connection = cnn
        cmd.CommandText = "INSERT INTO Accounts([First Name], [Last Name], [Address], " & _
                          "[Contact Number], [Username], [Email Address], [Password], [Credit Card Number]) " & _
                            " VALUES(" & txtFirst.Text & ",'" & txtLast.Text & "','" & _
                            txtAddress.Text & "','" & txtContact.Text & "','" & _
                            txtUsername.Text & "','" & txtEmail.Text & "','" & txtPass.Text & "','" & _
                            txtCredit.Text & "')"

        cmd.Parameters.AddWithValue("@p1", txtFirst.Text)
        cmd.Parameters.AddWithValue("@p2", txtLast.Text)
        cmd.Parameters.AddWithValue("@p3", txtAddress.Text)
        cmd.Parameters.AddWithValue("@p4", txtContact.Text)
        cmd.Parameters.AddWithValue("@p5", txtUsername.Text)
        cmd.Parameters.AddWithValue("@p6", txtEmail.Text)
        cmd.Parameters.AddWithValue("@p7", txtPass.Text)
        cmd.Parameters.AddWithValue("@p8", txtCredit.Text)

        cmd.ExecuteNonQuery()

        RefreshData()

        cnn.Close()





        MsgBox("Account created successfully.", MsgBoxStyle.Information, "Sign Up")
        Me.Hide()
        Login_Form.Show()
        Me.Close()

    End If


End Sub

Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
    Select Case MsgBox("Do you want to go back?", MsgBoxStyle.YesNo, "Cancel")
        Case MsgBoxResult.Yes
            Me.Hide()
            Login_Form.Show()
            Me.Close()
        Case MsgBoxResult.No
            Me.Show()
    End Select


End Sub

1 个答案:

答案 0 :(得分:0)

使用预准备语句时,您需要为占位符提供一个名称,以便稍后使用AddWithValue()过程引用它们。

另请参阅this question以及使用&#34; @ image&#34;标识符

相关问题