Click here to Skip to main content
13,141,576 members (35,184 online)
Rate this:
Please Sign up or sign in to vote.
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
Posted 25-Feb-13 19:39pm
Rahul Dhoble 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
srishti_ 26-Feb-13 2:35am
link is

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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 "

It's easier, tidier and more maintainable in .NET - at least you have arrays!
Rahul Dhoble 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100