473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

this ' in my search

145 100+
I have a search box txtSearch and it works fine for both first or last name. but I try to break it by inputing o'brien and received following error. I using sql like syntax to do my search. How can I make search better and break free? (should I be using parameterized stored proc? ) Please suggest. What are the pros doing (i mean their approach to this)?

syntax error: Missing operand after 'brien' operator
Mar 19 '09 #1
3 1607
dorandoran
145 100+
I tried this and it seems to be working. But now in text box it shows O''Brien. Note: I put extra quotation mark.

here is code, works just puts extra ' in the search text
Expand|Select|Wrap|Line Numbers
  1.         txtNameSearch.Text = txtNameSearch.Text.Trim().ToString();
  2.         txtNameSearch.Text = txtNameSearch.Text.Replace("'","''").ToString();
  3.         if (txtNameSearch != null)
  4.         {
  5.             SetFilter("([first_name] LIKE '%" + txtNameSearch.Text + "%' or [last_name] LIKE '%" + txtNameSearch.Text + "%')");
  6.             GridView2.SelectedIndex = -1;
  7.         }
Mar 19 '09 #2
Frinavale
9,735 Expert Mod 8TB
This doesn't work because an apostrophe (') is special SQL syntax.
It is used to indicate string literals. So when you are searching for John Doe your SQL will look like

Expand|Select|Wrap|Line Numbers
  1. Select * from MyTable Where name='John Doe' 
Now look what happens when you enter O'Brien:

Expand|Select|Wrap|Line Numbers
  1. Select * from MyTable where name = 'O'Brien'
See? It breaks.

In fact, this is taken advantage of by SQL Injection Attacks.

You need to change how you are creating your SQL query.

Instead of using input directly from the user while creating your SQL query, use parameters instead.

So instead of creating your SQL query like this:
Expand|Select|Wrap|Line Numbers
  1. sqlCom.CommandText = "Select * From myTable where name = '"+myTextBox.Text +"'";
You would create your SQL query like this:
Expand|Select|Wrap|Line Numbers
  1. sqlCom.CommandText = "Select * From myTable Where name = @searchName"
  2. sqlCom.Parmaeters.Add("@searchName", SqlDbType.VarChar).Value =  myTextBox.Text;
For examples please see the article on how to use a database in your program.

-Frinny
Mar 19 '09 #3
dorandoran
145 100+
Thanks Frinny for your tips. I was sure someone would know what I am talking about. and the find me the best solution. I will try your suggesion. Again thanks a lot.
Mar 20 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: George | last post by:
Hi, Anyone has the background for explaining? I have made a search on my name and I have got a link to another search engine. The link's title was the search phrase for the other search engine...
12
by: ljungers | last post by:
I'm on the home streach of my project and found that my "Reset for New Search" command button not working as desired. What should happen is that when the button is clicked a Event Procedure is run....
1
by: cglewis03 | last post by:
Hello, I am trying to build a search form with several different options to choose from. Currently it is set up to open within the same window if a single option is selected and open within a...
4
by: bendlam | last post by:
I have a page that contains search criteria and when you click on the search button it causes a post back that populates a dataview on the same page. One of the gridview columns contains a link...
13
by: jfarthing | last post by:
Hi everyone! I am using the script below to search a db. If the is more than one match in the db, all goes well. But if there is only one match in the db, nothing gets displayed. Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.