0

This Oracle SQL query written in c# is giving me the following error : invalid character

qur = " select * from emp where name LIKE '%" + TextBox1.Text + "%'";

How can I solve this?

Hugo Dozois
  • 7,807
  • 12
  • 51
  • 57
Shanna
  • 743
  • 4
  • 14
  • 33

2 Answers2

4

The problem is your query is very open to Sql Injection attacks. Since you are not using parametrized queries anything entered in TextBox1 can crush your query.

for example if I enter : ' char in Textbox your query will be select * from emp where name LIKE '%'%' and it will throw error. And apart from that it is vulnerability and you should not use such queries.

You can change query to :

SqlCommand cmd= new SqlCommand( " select * from emp where name LIKE @myParam");
cmd.Parameters.AddWithValue("@myParam", "%" + TextBox1.Text + "%");

you missed @

How do parameterized queries help against SQL injection?

C# constructing parameter query SQL - LIKE %

Community
  • 1
  • 1
adt
  • 4,271
  • 5
  • 32
  • 51
-2

you should use it as below:

qur = " select * from emp where name LIKE '%'" + TextBox1.Text + "'%'";
Code Rider
  • 1,939
  • 4
  • 31
  • 50