954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SQL insert help!!

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

mikkime23
Newbie Poster
2 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

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

:)

mikkime23
Newbie Poster
2 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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.

everettnewell
Newbie Poster
8 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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


Cheers,
Lennie

LennieKuah
Light Poster
47 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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

suresh bangaru
Newbie Poster
4 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

g@sha
Newbie Poster
3 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Try This:

sqCmd.CommandText = "INSERT INTO [RF_DB].[dbo].[RFID_TABLE] Values('" & Scan & "', '" & Tag & "','" & Toggle & "')"

http://www.difficulty9.com/blog/8-sql-insert-multiple-values.aspx

Joeromine
Newbie Poster
1 post since May 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You