| Author |
Topic  |
|
|
SQLNEWBIZ
Starting Member
India
17 Posts |
Posted - 12/26/2012 : 07:55:33
|
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
1419 Posts |
Posted - 12/26/2012 : 08:16:38
|
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 |
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 12/26/2012 : 08:24:02
|
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 |
 |
|
|
SQLNEWBIZ
Starting Member
India
17 Posts |
Posted - 12/26/2012 : 08:32:21
|
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
|
 |
|
|
SQLNEWBIZ
Starting Member
India
17 Posts |
Posted - 12/26/2012 : 08:55:51
|
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 & ")" |
 |
|
|
jimf
Flowing Fount of Yak Knowledge
USA
2866 Posts |
Posted - 12/26/2012 : 09:11:40
|
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 |
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 12/26/2012 : 09:13:56
|
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 & "')" |
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 12/26/2012 : 13:11:19
|
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("'", "''") _
& "','" _
...
|
 |
|
|
SQLNEWBIZ
Starting Member
India
17 Posts |
Posted - 12/27/2012 : 02:18:05
|
| Than you,for your help..I resolved the problem by addwithvalues without directly passing textbox data in the query.. |
 |
|
|
FredBailey
Starting Member
USA
1 Posts |
Posted - 01/19/2013 : 04:31:57
|
I think you should write the insert command again.There is problem of space or double quotes.Check it http://www.aerofixcycles.com/ |
 |
|
| |
Topic  |
|
|
|