944,208 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 15122
  • VB.NET RSS
Jul 16th, 2006
0

SQL insert help!!

Expand Post »
Hey guys. i am trying to insert data from a form to a sql database, but this error keeps coming up.
VB.NET Syntax (Toggle Plain Text)
  1. Incorrect syntax near ','
Here is the code.
VB.NET Syntax (Toggle Plain Text)
  1. Dim AddSwimmer_comm As New SqlCommand( _
  2. "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)
  3. AddSwimmer_conn.Open()
  4. AddSwimmer_comm.ExecuteNonQuery()
  5. AddSwimmer_conn.Close()
cheers, michael
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mikkime23 is offline Offline
2 posts
since Jul 2006
Jul 16th, 2006
0

Re: SQL insert help!!

Firstly Sql Server requires single quotes to delimit text. Example:
VB.NET Syntax (Toggle Plain Text)
  1. VALUES ( " & apascidbox.Text & ",
needs to be:
VB.NET Syntax (Toggle Plain Text)
  1. 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.
Last edited by hollystyles; Jul 16th, 2006 at 4:37 pm.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 17th, 2006
0

Re: SQL insert help!!

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

Reputation Points: 10
Solved Threads: 0
Newbie Poster
mikkime23 is offline Offline
2 posts
since Jul 2006
Jun 13th, 2009
0

Re: SQL insert help!!

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

VB.NET Syntax (Toggle Plain Text)
  1. sqlcmd = New SqlCommand("INSERT INTO [Employees] (" _
  2. & "[Id], [Name], [Rehire] ) " _
  3. & "VALUES ( " _
  4. & " & idvalue & ", " _
  5. & "'" & namevalue &"', " &
  6. & "'" & rehireboolean.tostring & "')", con)

I'm new to all this but that line has worked me hope it helps.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
everettnewell is offline Offline
8 posts
since Jun 2009
Jun 16th, 2009
0

Re: SQL insert help!!

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


Cheers,
Lennie
Reputation Points: 10
Solved Threads: 0
Light Poster
LennieKuah is offline Offline
47 posts
since Mar 2006
Jun 24th, 2009
0

Re: SQL insert help!!

sqlcmd = New SqlCommand("INSERT INTO tablename (field1,field2,field3,..)values('" & textbox1.text & "','" & textbox2.text & "','" & textbox3.text & "',...),con)
cmd.ExecuteNonQuery()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
suresh bangaru is offline Offline
4 posts
since Jun 2009
Jul 23rd, 2009
0

Re: SQL insert help!!

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
Last edited by g@sha; Jul 23rd, 2009 at 9:07 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g@sha is offline Offline
3 posts
since Jul 2009
May 12th, 2011
0
Re: SQL insert help!!
Try This:

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

http://www.difficulty9.com/blog/8-sq...le-values.aspx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Joeromine is offline Offline
1 posts
since May 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Using CMD
Next Thread in VB.NET Forum Timeline: I can't generate reports in Visual studio 2010





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC