Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
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 25-Feb-13 20:39pm
Comments
Rahul Dhoble at 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_ at 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

Rate this: bad
good
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 | :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!
  Permalink  
Comments
Rahul Dhoble at 26-Feb-13 2:18am
   
Good explanation
srishti_ at 26-Feb-13 3:43am
   
Please Show a short example of query
OriginalGriff at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
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