0

Forgive if the question is repeated, I'm new to this site.

Been trying to create a search text box for a program that saved and organized documents. The software is database-based, and it can search for only one column at a time. I need it to look for any info in any column (expect date).

Here is the code:

private void textBox2_TextChanged(object sender, EventArgs e)
{
        DataView vista = new DataView(tablaSql);
        vista.RowFilter = string.Format("asunto_corres LIKE'%{0}%'", textBox2.Text);
        dgTodo.DataSource = vista;
}        

It works but only with the name of the column specified.

Any help to make this textbox look for info in any field/column.

Thanks

4
  • You have to build that SQL-like column by column (with a long OR). BUT: 1) you can do it inspecting schema (no need to hard code all columns) and 2) use SQL parameters, do not build SQL commands with user inputs (what about SQL injection?). Commented Feb 19, 2014 at 14:52
  • @Adriano in this context the SQL Injection is not a concern (the RowFilter works on the in memory view) Commented Feb 19, 2014 at 14:56
  • You can use CONTAINS and FREETEXT clauses of Full-Text Search engine, but that can be overhead if your search is a small piece of app. Commented Feb 19, 2014 at 14:56
  • @Steve the point here is not (just) malicious SQL code but text that will make code fail (for example a search for a string like Andrew's cat). I mentioned SQL Injection because it's an easy search topic for Google if he need to search for that. Commented Feb 19, 2014 at 14:58

2 Answers 2

2

try:

vista.RowFilter = string.Format("{0} LIKE'%{1}%'", fieldName, textBox2.Text);
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it by adding some "OR" operators:

vista.RowFilter = string.Format("asunto_corres LIKE'%{0}%' OR Column2 LIKE'%{0}%' OR ...", textBox2.Text);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.