| | |
OLEDB QUERY If user input has ' query fails.
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
When an Access database is being updated and an apostrophy ' is embedded in a user input string, (txtArray[0].Text here), the query fails. I am using OLEDB. I am hoping I do not have to parse each user input searching for single quotes and probably other naughties like ". I tried searching past posts but it's hard to phrase a good search string looking for a little '
string SQL = "UPDATE Inventory SET" +
" Mask = " + Convert.ToInt32(mask) + "," +
" Field1= '" + txtArray[0].Text + "',"+ ...etc
string SQL = "UPDATE Inventory SET" +
" Mask = " + Convert.ToInt32(mask) + "," +
" Field1= '" + txtArray[0].Text + "',"+ ...etc
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
Well, seems I'll have to write to myself. After a bit of research and trial and error I came up with this code:
string SQL = "UPDATE Inventory SET" +
" Mask = " + Convert.ToInt32(mask) + "," +
" Field1= \"" + txtArray[0].Text + "\","+……
Works OK with ' ie if users enters text such as Ken's problem it now updates OK. However use of full quotes-eg inputing My "solution". in input string, still crashes it. So this is a partial fix!
string SQL = "UPDATE Inventory SET" +
" Mask = " + Convert.ToInt32(mask) + "," +
" Field1= \"" + txtArray[0].Text + "\","+……
Works OK with ' ie if users enters text such as Ken's problem it now updates OK. However use of full quotes-eg inputing My "solution". in input string, still crashes it. So this is a partial fix!
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
No one has picked this up, so I worked out a solution. There is a very useful “.Replace” method for strings which replaces all instances of a character eg \ or “. The code is correspondingly simple and short. You have to decide what characters to substitute for your naughty ones. I picked the “Thorn” character and the “Pilcrow” character.
See http://www.alanwood.net/demos/ansi.html
[code]
//Search for and replace characters in user input string that create errors in
//an SQL Update query. Replace Quote mark with Thorn character.
//Replace Backslash with Pilcrow character
private string ParseInput(string UserInput)
{
string sub1 = "\u00DE"; string sub2 = "\u00B6";
string search1 = "\""; string search2 = "\\";
string NewString = UserInput.Replace(search1,sub1);//Replace ALL quote marks
NewString = NewString.Replace(search2,sub2);//Replace ALL backslash characters
return NewString;
}
[\code]
There is a corresponding “inverseParse” method when reading from the database.
See http://www.alanwood.net/demos/ansi.html
[code]
//Search for and replace characters in user input string that create errors in
//an SQL Update query. Replace Quote mark with Thorn character.
//Replace Backslash with Pilcrow character
private string ParseInput(string UserInput)
{
string sub1 = "\u00DE"; string sub2 = "\u00B6";
string search1 = "\""; string search2 = "\\";
string NewString = UserInput.Replace(search1,sub1);//Replace ALL quote marks
NewString = NewString.Replace(search2,sub2);//Replace ALL backslash characters
return NewString;
}
[\code]
There is a corresponding “inverseParse” method when reading from the database.
![]() |
Other Threads in the C# Forum
- Previous Thread: HCF
- Next Thread: Help regarding images in datagridview
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion platform post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml





