Incorrect syntax near '-' in Insert

Asked

Viewed 308 times

0

I developed an application for registration of Clients "Transport" linked to a database "MDF", when trying to save to the database gives error:

"Incorrect syntax near '-'"

I tried everything, changed the codes, changed several things but the error prevails.

Code:

 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Try
        con = New SqlConnection(cs)
        con.Open()
        Dim ct As String = "select id from Clientes where id='" & IdTextBox.Text & "'"

        cmd = New SqlCommand(ct)
        cmd.Connection = con
        rdr = cmd.ExecuteReader()

        If rdr.Read() Then
            MessageBox.Show("Id ja existe", "Erro", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            IdTextBox.Text = ""
            IdTextBox.Focus()
            If (rdr IsNot Nothing) Then
                rdr.Close()
            End If
            Return
        End If

        con = New SqlConnection(cs)
        con.Open()

        Dim cb As String = "insert into Clientes(id, nome, cidade, matricula, marca, lotacao, partida, chegada, capacidade, estado, Tipo-Licenca, Licenca-emissao, licenca-expira, seguros-emissao, seguros-expira, ficha-emissao, ficha-expira, data-registado) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17,@d18)"
        cmd = New SqlCommand(cb)
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@d1", IdTextBox.Text)
        cmd.Parameters.AddWithValue("@d2", NomeTextBox.Text)
        cmd.Parameters.AddWithValue("@d3", CidadeComboBox.Text)
        cmd.Parameters.AddWithValue("@d4", MatriculaTextBox.Text)
        cmd.Parameters.AddWithValue("@d5", MarcaComboBox.Text)
        cmd.Parameters.AddWithValue("@d6", LotacaoComboBox.Text)
        cmd.Parameters.AddWithValue("@d7", PartidaComboBox.Text)
        cmd.Parameters.AddWithValue("@d8", ChegadaComboBox.Text)
        cmd.Parameters.AddWithValue("@d9", CapacidadeComboBox.Text)
        cmd.Parameters.AddWithValue("@d10", EstadoComboBox.Text)
        cmd.Parameters.AddWithValue("@d11", Tipo_LicencaComboBox.Text)
        cmd.Parameters.AddWithValue("@d12", Licenca_emissaoDateTimePicker.Text)
        cmd.Parameters.AddWithValue("@d13", Licenca_ExpiraDateTimePicker.Text)
        cmd.Parameters.AddWithValue("@d14", Seguros_EmissaoDateTimePicker.Text)
        cmd.Parameters.AddWithValue("@d15", Seguros_ExpiraDateTimePicker.Text)
        cmd.Parameters.AddWithValue("@d16", Ficha_EmissaoDateTimePicker.Text)
        cmd.Parameters.AddWithValue("@d17", Ficha_ExpiraDateTimePicker.Text)
        cmd.Parameters.AddWithValue("@d18", Now)
        cmd.ExecuteReader()
        con.Close()
        MessageBox.Show("Cliente registado com sucesso", "Cliente", MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnSave.Enabled = False
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub

Note: The Clients form has Combobox’s that pull data from other tables: Example: The Combobox 'Capacidadecombobox' pulls the data from the table 'Capacity'

  • This error occurs when you do the search?

  • 1

    I think the problem is using the - in the name of the fields Type-License, License-Issue, etc. Try to put them between pelicas: 'Tipo-Licenca'.

  • @Denervgarlic not when trying to save data to database

  • 2

    Even if that is not the problem I suggest that you use Tipolicenca instead of Type-License or Type-License.

  • @ramaral put, and the error changed to: Incorrect syntax near 'Tipo-Licenca' '-'

  • 1

    @Thanked, I found the solution... [Type-Licenca]

Show 1 more comment

1 answer

1


Solution

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
    con = New SqlConnection(cs)
    con.Open()
    Dim ct As String = "select id from Clientes where id='" & IdTextBox.Text & "'"

    cmd = New SqlCommand(ct)
    cmd.Connection = con
    rdr = cmd.ExecuteReader()

    If rdr.Read() Then
        MessageBox.Show("Id ja existe", "Erro", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        IdTextBox.Text = ""
        IdTextBox.Focus()
        If (rdr IsNot Nothing) Then
            rdr.Close()
        End If
        Return
    End If

    con = New SqlConnection(cs)
    con.Open()

    Dim cb As String = "insert into Clientes(id, nome, cidade, matricula, marca, lotacao, partida, chegada, capacidade, estado, [Tipo-Licenca], [Licenca-emissao], [licenca-expira], [seguros-emissao], [seguros-expira], [ficha-emissao], [ficha-expira], [data-registado]) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17,@d18)"
        cmd = New SqlCommand(cb)
    cmd.Connection = con
    cmd.Parameters.AddWithValue("@d1", IdTextBox.Text)
    cmd.Parameters.AddWithValue("@d2", NomeTextBox.Text)
    cmd.Parameters.AddWithValue("@d3", CidadeComboBox.Text)
    cmd.Parameters.AddWithValue("@d4", MatriculaTextBox.Text)
    cmd.Parameters.AddWithValue("@d5", MarcaComboBox.Text)
    cmd.Parameters.AddWithValue("@d6", LotacaoComboBox.Text)
    cmd.Parameters.AddWithValue("@d7", PartidaComboBox.Text)
    cmd.Parameters.AddWithValue("@d8", ChegadaComboBox.Text)
    cmd.Parameters.AddWithValue("@d9", CapacidadeComboBox.Text)
    cmd.Parameters.AddWithValue("@d10", EstadoComboBox.Text)
    cmd.Parameters.AddWithValue("@d11", Tipo_LicencaComboBox.Text)
    cmd.Parameters.AddWithValue("@d12", Licenca_emissaoDateTimePicker.Text)
    cmd.Parameters.AddWithValue("@d13", Licenca_ExpiraDateTimePicker.Text)
    cmd.Parameters.AddWithValue("@d14", Seguros_EmissaoDateTimePicker.Text)
    cmd.Parameters.AddWithValue("@d15", Seguros_ExpiraDateTimePicker.Text)
    cmd.Parameters.AddWithValue("@d16", Ficha_EmissaoDateTimePicker.Text)
    cmd.Parameters.AddWithValue("@d17", Ficha_ExpiraDateTimePicker.Text)
    cmd.Parameters.AddWithValue("@d18", Now)
    cmd.ExecuteReader()
    con.Close()
    MessageBox.Show("Cliente registado com sucesso", "Cliente", MessageBoxButtons.OK, MessageBoxIcon.Information)
    btnSave.Enabled = False
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try

End Sub

  • 2

    You were right to post the solution. However, instead of putting all this code, I suggest you just explain the reason for the problem and how to solve it. The way it is, it’s hard to figure out the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.