SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Sql syntax error..Please help..
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLNEWBIZ
Starting Member

India
26 Posts

Posted - 12/26/2012 :  07:55:33  Show Profile  Reply with Quote
Hello,all, here is my code.. I want to insert the textbox values ,both string and integer,direct to my sql DB itemlistingDB by hitting save button.this is the code i have written.
when the code is build,, its showing "Incorrect syntax near ','." when save button is pressed .
someone please help me,its urgent..

Private Sub saveFunc()

'
Dim con As New SqlConnection("Data Source=V-PC\WINCCPLUSMIG;Initial Catalog=ProductDB;Integrated Security=True")
'"Insert into Dealer" + "values('" + D_B1.Text + "'," + D_B2.Text + "'," + D_B3.Text + "'," + D_B4.Text + "'," + D_B5.Text + "'," + D_B6.Text + ")", connection_a);

con.Open()

Dim cmd As New SqlCommand
cmd.CommandText = "INSERT INTO ItemListingDB(ItemCode,ItemName,Cost,Price) Values('" & txtitemcode.Text & "','" & txtItemName.Text & "'," & txtcost.Text & "," & txtprice.Text & ")"

cmd.Connection = con

cmd.ExecuteNonQuery()


con.Close()
End Sub

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 12/26/2012 :  08:16:38  Show Profile  Reply with Quote
May be this for Dealer Table...... try once

"Insert into Dealer" + "values('" + D_B1.Text + "','" + D_B2.Text + "','" + D_B3.Text + "','" + D_B4.Text + "','" + D_B5.Text + "','" + D_B6.Text + "')",

--
Chandu
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/26/2012 :  08:24:02  Show Profile  Reply with Quote
In addition to fixing the syntax as Chandu suggested, also note that you have two strings that insert into Dealer table and ItemListingDB. The way you are doing this, it would insert only into ItemListingDB, even if the syntax was fixed

Using ExecuteNonQuery is very simple - you need a command object. The command object needs two things a connection string, and a command text. If you take a look at the example on this page and try to adapt that to your requirement, that may be easier: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx
Go to Top of Page

SQLNEWBIZ
Starting Member

India
26 Posts

Posted - 12/26/2012 :  08:32:21  Show Profile  Reply with Quote
sorry for the confusion,"Insert Into Dealer is not the part of my code,i mean the insert query below is what i want.all the time the code is built,the same syntax error is returing.
quote:
Originally posted by bandi

May be this for Dealer Table...... try once

"Insert into Dealer" + "values('" + D_B1.Text + "','" + D_B2.Text + "','" + D_B3.Text + "','" + D_B4.Text + "','" + D_B5.Text + "','" + D_B6.Text + "')",

--
Chandu

Go to Top of Page

SQLNEWBIZ
Starting Member

India
26 Posts

Posted - 12/26/2012 :  08:55:51  Show Profile  Reply with Quote
i cant find any change to be done on the below query.so while building,{"Incorrect syntax near ','." is showing.plz help someone. its urgent
cmd.CommandText = "INSERT INTO ItemListingDB(ItemCode,ItemName,Cost,Price) Values('" & Me.txtitemcode.Text & "','" & Me.txtItemName.Text & "'," & Me.txtcost.Text & "," & Me.txtprice.Text & ")"
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 12/26/2012 :  09:11:40  Show Profile  Reply with Quote
I think you may just be missing a space. From what you've written, your code will look like this
Insert Into Dealervalues('Text' etc.
Try adding a space
"Insert Into Dealer " + values(

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/26/2012 :  09:13:56  Show Profile  Reply with Quote
You have a few missing single quotes - see below. I broke it up into multiple lines for readability, but you don't have to.

An easy way to debug this is to print out the command text in the debugger after this statement and look at it (or post it, or run it in SQL Server Management Studio)
cmd.CommandText = "INSERT INTO ItemListingDB(ItemCode,ItemName,Cost,Price) Values('" _
	& txtitemcode.Text  _
	& "','"  _
	& txtItemName.Text  _
	& "','"  _
	& txtcost.Text  _
	& "','"  _
	& txtprice.Text & "')"
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/26/2012 :  13:11:19  Show Profile  Reply with Quote
One thing I forgot to mention:

If the values in the text boxes can have single quotes in them, you would need to replace single quotes with two single quotes for each occurrence. So something like shown below. That may be the issue you are running into. Be sure to do that for each text box that potentially can have single quotes in the data entered in it:
....
	& "','"  _
	& txtItemName.Text.Replace("'", "''")  _
	& "','"  _
...

Go to Top of Page

SQLNEWBIZ
Starting Member

India
26 Posts

Posted - 12/27/2012 :  02:18:05  Show Profile  Reply with Quote
Than you,for your help..I resolved the problem by addwithvalues without directly passing textbox data in the query..
Go to Top of Page

FredBailey
Starting Member

USA
1 Posts

Posted - 01/19/2013 :  04:31:57  Show Profile  Reply with Quote
I think you should write the insert command again.There is problem of space or double quotes.Check it
http://www.aerofixcycles.com/
Go to Top of Page

bluffy
Starting Member

India
2 Posts

Posted - 06/06/2013 :  01:48:44  Show Profile  Reply with Quote
how to use pivot table in mysql

Regards,
Bluffy,
Currency Trading in India: UFXMARKETS
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/06/2013 :  02:20:03  Show Profile  Reply with Quote
Hi bluffy,
This is SQL Server (MSSQL) forum...

Follow relevant database forums like http://forums.mysql.com/
Any way use the below links for Cross tabulation / PIVOT Tables in MySQL
http://www.codeproject.com/Articles/363339/Cross-Tabulation-Pivot-Tables-with-MySQL
http://buysql.com/mysql/12-how-to-pivot-tables.html

--
Chandu
Go to Top of Page

cursor system
Starting Member

2 Posts

Posted - 06/11/2013 :  05:15:42  Show Profile  Reply with Quote
The best way to debug this type of problem is by using one field one value. That is
cmd.CommandText = "INSERT INTO ItemListingDB(ItemCode) Values('" & txtitemcode.Text & "')"

After this is tested and okay, then

cmd.CommandText = "INSERT INTO ItemListingDB(ItemCode,ItemName) Values('" & txtitemcode.Text & "','" & txtItemName.Text & "')"

and so on untill all are done. With thisb you will easily know the one that is making the error.

Get helpful tips on technology @ http://www.bestpricebestdeals.blogspot.com


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000