473,466 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sql insert Question

Hi,
I have got this SQL below updating a textbox and a checkBox.

strSql = "Update Products Set Discontinued=" & chkBoxChecked & ",ProductName
= '" & ProductName & "' Where ProductID=" & ProductID

it outputs error :- Incorrect syntax near 's'. Unclosed quotation mark
before the character string ' Where ProductID=4'.

I can't see what is wrong can somebody just look through this..
Maybe tired:(
Thx
Nov 18 '05 #1
3 1402
found my error i noticed i was inserting an apostrophe for example the word
(code's) in into the DB...
Whats the best way to replace this when inserting and editing and updating
this..
This problem come up especially when updating a field!!
"Patrick.O.Ige" wrote:
Hi,
I have got this SQL below updating a textbox and a checkBox.

strSql = "Update Products Set Discontinued=" & chkBoxChecked & ",ProductName
= '" & ProductName & "' Where ProductID=" & ProductID

it outputs error :- Incorrect syntax near 's'. Unclosed quotation mark
before the character string ' Where ProductID=4'.

I can't see what is wrong can somebody just look through this..
Maybe tired:(
Thx

Nov 18 '05 #2
What you are seeing is a classic example of a vulnerability to a SQL
Injection attack. Obviously you want to fix this here, however you
really need to fix this wherever you have concatenated SQL statements or
you risk users (or potential hackers) really messing with your database
(I won't preach but PLEASE!!! go look up information on SQL Injection
and cross site scripting attacks).

To fix this there are four possibilities.

The best way to correct this is to migrate your sql statements into
parameterized stored procedure calls.

If your database doesn't support parameterized stored procedures or you
don't want to use stored procedures you should implement parameterized
queries. To do this you would structure your query like:

string sql = "Update Products Set Discontinued=@Discontinued,
ProductName=@ProductName Where ProductId=@ProductId";
SqlCommand cmd = new SqlCommand(sql);
cmd.Parameters.Add("@Discontinued", SqlDbType.Bit).Value = chkBoxChecked;
cmd.Parameters.Add("@ProductName", SqlDbType.VarChar, 255).Value =
ProductName;
cmd.Parameters.Add("@ProductId", SqlDbType.Int).Value = ProductID;
cmd.ExecuteNonQuery();

This will fix the single quote issue. Not I realize it is in C# instead
of VB.Net but I think you'll translate it with out a problem. Also it is
setup for SQL Server but the concept should translate to whichever
database object type you are working with.

Third if your database supports it you can try to replace each single
quote with two single quotes (not double quotes but literally two
single quotes). To do this you could do a

strSql.Replace("'", "''")

Finally, and probably worst of all you could try to filter out invalid
characters but this could remove important data and/or miss things.

Hope this helps.

Have A Better One!

John M Deal, MCP
Necessity Software
Patrick.O.Ige wrote:
found my error i noticed i was inserting an apostrophe for example the word
(code's) in into the DB...
Whats the best way to replace this when inserting and editing and updating
this..
This problem come up especially when updating a field!!
"Patrick.O.Ige" wrote:

Hi,
I have got this SQL below updating a textbox and a checkBox.

strSql = "Update Products Set Discontinued=" & chkBoxChecked & ",ProductName
= '" & ProductName & "' Where ProductID=" & ProductID

it outputs error :- Incorrect syntax near 's'. Unclosed quotation mark
before the character string ' Where ProductID=4'.

I can't see what is wrong can somebody just look through this..
Maybe tired:(
Thx

Nov 18 '05 #3
In the SQL language, the single quote is a string delimiter, used to
identify the data type of tokens in the SQL string. You can escape it by
doubling it (use 2 single quotes together).

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
found my error i noticed i was inserting an apostrophe for example the word (code's) in into the DB...
Whats the best way to replace this when inserting and editing and updating
this..
This problem come up especially when updating a field!!
"Patrick.O.Ige" wrote:
Hi,
I have got this SQL below updating a textbox and a checkBox.

strSql = "Update Products Set Discontinued=" & chkBoxChecked & ",ProductName = '" & ProductName & "' Where ProductID=" & ProductID

it outputs error :- Incorrect syntax near 's'. Unclosed quotation mark
before the character string ' Where ProductID=4'.

I can't see what is wrong can somebody just look through this..
Maybe tired:(
Thx

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Bedford | last post by:
I've a textarea and would like to save the content in a mysql table each time a user click on a form. How can I do for avoiding error when the user put a " or a ' in the message, or any other...
8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
5
by: TThai | last post by:
HI, I'm trying to insert records to a table using bcp command. The problem is the input file to the bcp is a text file that looks like this: Text file data: 1234 abc def ghi jkl mno ...
11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
25
by: Andreas Fromm | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, Im building an user database with many tables keeping the data for the Address, Phone numbers, etc which are referenced by a table where I...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
1
by: javedna | last post by:
Can PHP help with the following as I have tried in the MYSQL Forums and cant get any help Thanks Nabz ---------------------------------------- Hi I am developing a PHP MYSQL questionnaire...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
24
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.