Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello ,

I have a form on dotnet in which there are around 20 fields according to this filter data fetch in gridview so i want to make a stored procedude in which data fetching is done accordind to filter condition .........

in short for example there are fields like country, city,product, item,quality,location etc so i want that if we select only country than according to this data fetched and if we select two fields like country and city then data is fetch accoding to that and so on
and one more thing that field are not related to each other each field are separte....

Please Reply


Thanks & Regards
Srishti
Posted
Comments
RDBurmon 26-Feb-13 2:07am    
Could you please provide image of your form to get more clarity on your searching method ?
You can upload your image in free image hosting site and paste a link here

See this http://postimage.org/
srishti_ 26-Feb-13 2:35am    
link is
http://postimage.org/image/xdjmtnefx/

but in this link field are less i have some more fields around 20.......

1 solution

You could do it, but it's horribly messy, as you have probably seen when trying to do it in .NET - which is presumably why you want to move it to SQL so you don't have to think about it! :laugh:

There is no magic way to do it: It's actually easier to do in .NET rather than SQL because it's range of string manipulations is better, but the principle is the same in either case.

To do it in SQL you have to assemble a SELECT statement as a varchar string, then EXEC the string.
I'm not going to do it for you, because in SQL it's a monolithic CASE WHEN statement, using a prefix to separate clauses:
0) Set the query string to "SELECT <your fields list> FROM <Your table>"
1) Start with the prefix set to " WHERE "
2) Check the first parameter. If it is present, then append the prefix to the query string, then add your parameter data to that. Set the prefix to " AND "
3) Check the second parameter. If it is present, then append the prefix to the query string, then add your parameter data to that. Set the prefix to " AND "
Repeat.

It's easier, tidier and more maintainable in .NET - at least you have arrays!
 
Share this answer
 
Comments
RDBurmon 26-Feb-13 2:18am    
Good explanation
srishti_ 26-Feb-13 3:43am    
Please Show a short example of query
OriginalGriff 26-Feb-13 4:16am    
I can't - it would be meaningless. I know nothing about your fields so I can't do anything useful. And the final assembled query is just a SELECT...WHERE...AND...AND... so that doesn't help anyone either!

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