Hey guys. i am trying to insert data from a form to a sql database, but this error keeps coming up.

Incorrect syntax near ','

Here is the code.

Dim AddSwimmer_comm As New SqlCommand( _
        "INSERT INTO [MasterRecords] ([APASCID], [FullName], [Gender], [DOB], [21m], [25m], [50m], [100m], [4x21m], [4x50m]) VALUES ( " & apascidbox.Text & ", " & txt_fullname.Text & ", " & txt_birthdate.Text & ", " & gender.Text & ", " & _21.Checked & ", " & _25.Checked & ", " & _50.Checked & ", " & _100.Checked & ", " & medley_4X21.Checked & ", " & medley_4X50.Checked & "))", AddSwimmer_conn)
        AddSwimmer_conn.Open()
        AddSwimmer_comm.ExecuteNonQuery()
        AddSwimmer_conn.Close()

cheers, michael

Recommended Answers

All 7 Replies

Firstly Sql Server requires single quotes to delimit text. Example:

VALUES ( " & apascidbox.Text & ",

needs to be:

VALUES ( '" & apascidbox.Text & "',

see you need to concatenate a single quote just before the closing double quote and just after the next openeing double-quote, do same for all text fields in the SQL string.

Also what is the data type in the SQL Table for your measurement fields ? is it bit? I'm not sure if ADO/SQL is able to implicitly convert boolean true/false to a bit 0/1.

yes, it is a bit. i have also been getting errors for the false and true statements. thanks for the reply.

:)

Here's an example of what to do with boolean statements hope it helps.

sqlcmd = New SqlCommand("INSERT INTO [Employees] (" _
& "[Id], [Name], [Rehire] ) " _
& "VALUES ( " _
& " & idvalue & ", " _
& "'" & namevalue &"', " &
& "'" & rehireboolean.tostring & "')", con)

I'm new to all this but that line has worked me hope it helps.

Hi All,
I am experiencing the same problems and thanks for the suggestions. I got it fixed following the samples.


Cheers,
Lennie

sqlcmd = New SqlCommand("INSERT INTO tablename (field1,field2,field3,..)values('" & textbox1.text & "','" & textbox2.text & "','" & textbox3.text & "',...),con)
cmd.ExecuteNonQuery()

Protected Sub cmdPrihvati_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPrihvati.Click '''///Action on click on the buton cmdPrihvati in your webForm
''' You must connect to database first.
''' you must heve 3 feilds (txtNaziv, txtAdresa, txtMesto) language of the form is Serbian (english is: txtName, txtAddres, txtTown)
''' you must have Table in the Database. Table's name is KlijentiUpload and she has 3 fields (Naziv, adresa, Mesto)
Dim Naziv As String '' text variable
Dim Adresa As String '' text variable
Dim Mesto As String '' text variable
Dim UpSql As String '' text variable for your new SQL string
Dim Source As AccessDataSource

Naziv = txtNaziv.Text.ToString '' variable /Naziv/ recive txtField's value
If Naziv = "" Then
Response.Write("Niste uneli podatak u Obavezno polje - Naziv." & vbCrLf & "Ponovite unos") ''MsgBox if required field does not value
txtNaziv.BorderWidth = 2 '' change border if required field does not value
txtNaziv.BorderColor = Drawing.Color.Red '' change BorderColor if required field does not value
Exit Sub
Else
txtNaziv.BorderWidth = 1
txtNaziv.BorderColor = Drawing.Color.Gray

End If
Adresa = txtAdresa.Text.ToString '' variable /Adresa/ recive txtField's value
If Adresa = "" Then
Response.Write("Niste uneli podatak u Obavezno polje -Adresa." & vbCrLf & "Ponovite unos")
Exit Sub
End If
Mesto = txtMesto.Text.ToString '' variable /Mesto/ recive txtField's value
If Mesto = "" Then
Response.Write("Niste uneli podatak u Obavezno polje - Mesto." & vbCrLf & "Ponovite unos")
Exit Sub
End If

'' if all ok, you can recived values from text feilds and write into sql

UpSql = "INSERT INTO KlijentiUpload(Naziv, Adresa, Mesto) VALUES ('" + Naziv.ToString + "','" + Adresa.ToString + "', '" + Mesto.ToString + "')"

AccessDataSource1.SelectCommand = UpSql
txtNaziv.Text = ""
txtAdresa.Text = ""
txtMesto.Text = ""
Response.Write("Podaci su uspešno uneti u bazu podataka- Hvala") ''MsgBox if all OK


End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.