Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a search text box. Now if i search
--------------
| Californa 87|
---------------
The Location and Location Number. There will be no output in my datagrid view.

 int i = 0;
 dgv_bene.Rows.Clear();
 cn.Open();
 cm = new MySqlCommand("SELECT `b_id`, concat_ws(' ', `b_lname`, `b_fname`) as `applicant_name`, concat_ws(' ', `b_slname`, `b_sfname`) as `spouse_name`, `b_type`, `b_location`, `b_block`, `b_lot`, `p_owner` from benefciiary where (b_location + ' ' +  b_block) like '"+tb_search.Text+"%' ", cn);


dr = cm.ExecuteReader();
 while (dr.Read())
 {
     i += 1;
     dgv_bene.Rows.Add(i, dr[0].ToString(), dr[1].ToString(),
     dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(),
     dr[6].ToString(), dr[7].ToString());
 }
 dr.Close();
 cn.Close();


What I have tried:

I have tried to concat_ws(). but its an error
Posted
Updated 4-Oct-20 19:15pm
v2
Comments
PIEBALDconsult 5-Oct-20 0:11am    
Please investigate parameterized queries.

1 solution

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Secondly, we can't do anything to help you with this: we have no access to your DB or to your user's input - and both are required to work out what it going on.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
I'd start by looking at the input tx_search.Text and seeing how many rows are returned, and then doing a manual DB SELECT using the same table and LIKE clause. I'd suspect that you have misspelled "California", but without your DB and inputs we can't tell!

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Johnny Sins 5-Oct-20 1:15am    
Thanks. I already have 2 Solutions in my head. Hope it works. haha

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900