Search code examples
vb.netms-accessstring-concatenation

SQL Insert syntax error when I concatenate with "&"


I am using visual basic and access.

If I try concatenate with " +" , release the following error: Conversion from string "" to type 'Double' is not valid.

My code:

 Protected Friend Sub insertarProducto(ByVal codigo As String, ByVal modelo As String, ByVal serial As String, ByVal pallet As String, ByVal precio As Double, ByVal cantidad As Integer, ByVal descripcion As String)
    Try
        con.Open()
        adapter = New OleDbDataAdapter("Insert into Productos(Cod_Producto,Serial,Lote/pallet,Modelo,Descripcion,Precio,Cantidad)Values('" & codigo & "','" & serial & "','" & pallet & "','" & modelo & "','" & descripcion & "'," & precio & "," & cantidad & ")", con)
        adapter.Fill(tabla)
    Catch ex As Exception
        MsgBox("Problemas en la consulta: " + ex.Message(), MsgBoxStyle.Critical)
    End Try
    con.Close()
End Sub

The order of my columns is good. What's going on here?


Solution

  • (especially Ken ) I obeyed your advice. Finally I was able to successfully insert data to my database! First I modified the name of a column (Lote/pallet) and wrote this code:

    Protected Friend Sub insertarProducto(ByVal codigo As String, ByVal modelo As String, ByVal serial As String, ByVal pallet As String, ByVal precio As Double, ByVal cantidad As Integer, ByVal descripcion As String, ByVal imagen As String)
        Dim cmd As String = "Insert into Productos(Cod_Producto,Serial,Lotpallet,Modelo,Descripcion,Precio,Cantidad,Imagen)Values(@Cod_Producto,@Serial,@Lotpallet,@Modelo,@Descripcion,@Precio,@Cantidad,@Imagen)"
        Try
            con.Open()
            comando = New OleDbCommand(cmd, con)
            comando.Parameters.AddWithValue("@Cod_Producto", codigo)
            comando.Parameters.AddWithValue("@Serial", serial)
            comando.Parameters.AddWithValue("@Lotpallet", pallet)
            comando.Parameters.AddWithValue("@Modelo", modelo)
            comando.Parameters.AddWithValue("@Descripcion", descripcion)
            comando.Parameters.AddWithValue("@Precio", precio)
            comando.Parameters.AddWithValue("@Cantidad", cantidad)
            comando.Parameters.AddWithValue("@Imagen", imagen)
            comando.ExecuteNonQuery()
            comando.Dispose()
        Catch ex As Exception
            MsgBox("Problemas en la consulta: " + ex.Message(), MsgBoxStyle.Critical)
        End Try
        con.Close()
    End Sub
    

    This web site help me a lot: http://www.codeguru.com/columns/vb/using-parameterized-queries-and-reports-in-vb.net-database-applications.htm

    I dont know if was the best way to do it...Thank you so much for your advice!