Click here to Skip to main content
15,917,321 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have 20 text boxes created to search in thousands of records in database. I have a parameterized search and I need help on how I can disregard an empty textbox from the searcch query that uses AND operator. Example SELECT name,location,age.. FROM tbl1 WHERE name=@name AND location=@location AND age=@age...
cmd.Parameters.AddWithValue("@name", textboxname.Text);
cmd.Parameters.AddWithValue("@location", textboxlocation.Text);
cmd.Parameters.AddWithValue("@age", textboxage.Text);....

If I searched only using name, there will be no results because the other textboxes are empty.. I cannot use OR or like I need exact values from the textboxes used for searching. thanks in advance

What I have tried:

tried using

SELECT name,location,age.. FROM tbl1 WHERE name=@name AND location=@location AND age=@age...
Posted
Updated 6-Sep-17 9:34am

Compile it together.
Call a method, and pass it a stringbuilder, a List<SqlParameter>, an integer index, the feild name, the textbox string, and the parameter value.
If the text box is empty, return the index unchanged.
Otherwise, check the index. If it's non zero, appends " AND " to the string Builder.
Append the field name, then " = @P", then the index value to the string builder.
Create a new SqlParameter called "@P" and the index number, and give it the value. Add that to the list.
Increment the index, and return it.
Once you've tried to assemble all parameters, you have a StringBuilder containing the necessary commands, a collection of parameters to add to the SqlCommand object, and the number of commands in the system.
 
Share this answer
 
Comments
aaaaaaaaaaa222 6-Sep-17 11:02am    
something like this? https://stackoverflow.com/questions/41722502/c-sharp-sql-server-searching-all-database-columns-and-populating-datagrid
OriginalGriff 6-Sep-17 11:09am    
You can use LIKE and OR, but... it's not the most efficient SQL, particularly for big datasets. Plus, it only works for string based columns - that may not be a problem for you now, but who knows what will happen next week? :laugh:
You can build the criteria part of the query on fly as long as you keep parameters.
Try something like:
C#
criteria="";
if (textbox.text.length != 0)
{
    if (criteria.lentgh != 0)
        criteria += " AND";
    criteria += " name=@name";
    cmd.Parameters.AddWithValue("@name", textbox.Text);
}
...
query= "SELECT name,location,age.. FROM tbl1 WHERE" + criteria;

It is just the principle, I let you deal with C# syntax.
 
Share this answer
 
Comments
aaaaaaaaaaa222 18-Sep-17 9:10am    
It make sense, thank you very much. I'll try to do it in C# and please continue being a blessing to others! Thank you.
You could also consider looking for indexing tools like Lucene.Net and rolling your own search page. Or even a stand alone search server like Solr.

These products would actually create indexes based on your data and how you configure it. If you have 20 fields you need to search on these products can really help.

Apache Lucene.Net[^]

Lucene.Net ultra fast search for MVC or WebForms site => made easy![^]

Apache Solr - Features[^]
 
Share this answer
 

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