views:

21

answers:

2

Hi,

I am using asp.net 2.0 and c#.

I have a dataset, which is getting the employee info. Now I want to filter the gridview based on the name user has put in the search textbox. I am doing this :

        DataSet ds = new DataSet("EmployeeInformation");
        ........ loading DataSet ds with emploee info
        string strExpr;
        strExpr = "Name LIKE %" + txtSearchEmployee.Text.Trim() + "%";
        ds.Tables[0].Select(strExpr);

I am getting error in the last step that the operator is missing.

Please guide me how can I achive this. Thanks in advance.

A: 

In SQL, LIKE requires quotation marks around its argument. So, "LIKE \"%foo\%"".

What you are doing is also a BAD IDEA. If a user sends you a search string with special characters in it, you will be vulnerable to a SQL injection attack.

Sanitize your input.

Borealid
AFAIK there's no risk of SQL Injection attacks when all you're doing is selecting from a pre-loaded DataSet.
GenericTypeTea
Am I doing it incorrectly ? Please suggest me
Rahul
Really? The user can select arbitrary information from your dataset, even if they can't DROP TABLES. At the very least, they can produce invalid SQL.
Borealid
Reference for general principles of SQL injection in ASP: http://msdn.microsoft.com/en-us/library/ff648339.aspx
Borealid
Borealid. I think you're misunderstanding what the Select method does in a DataTable. See http://msdn.microsoft.com/en-us/library/det4aw50.aspx. All the `Select` function does is to filter a DataTable that has already been loaded with data. There's no connection between the DataTable and the SQL database.
GenericTypeTea
I have checked the same and I am agree with generic TypeTea. As it's under disconnected architecture. Please share your feedback if you think I am incorrect.
Rahul
+1  A: 

You just need to add single quotes around your LIKE criteria:

strExpr = "Name LIKE '%" + txtSearchEmployee.Text.Trim() + "%'";
ds.Tables[0].Select(strExpr);
GenericTypeTea