Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have three text boxes - roll,registration,name ;and a submit button

Now the user can provide input in the text boxes

there is a possibility of few textboxes being blank. So a empty ( "" ) string goes into the where clause.

I want a select query in such a way that it should search the data in database according to the values entered by the user, and ignore the the values not entered
Posted
Updated 22-Jan-16 2:41am
v2
Comments
F-ES Sitecore 22-Jan-16 8:44am    
Are you using a stored procedure? ADO.net? Entity Framework? Something else?
Member 12270086 22-Jan-16 8:51am    
no....
F-ES Sitecore 22-Jan-16 8:53am    
You must be using something to access the database. If we don't know what technology you're using we can't give you a solution.
HardikPatel.SE 22-Jan-16 8:47am    
You have to use Dynamic Query for it...
Member 12270086 22-Jan-16 8:51am    
how could i use it??

1 solution

Then you need to adjust your select clause to ignore the blank entries. A few if statements and a StringBuilder should do the trick.
C#
StringBuilder select = new StringBuilder("SELECT * from datatable WHERE");
if (!textRoll.IsEmpty && textRoll != "")
{
// add the details for the roll being present
// and the parameter value(s) for the query
}
...
// repeat for the other fields
 
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