Ali Shah
asked on
searching the database sql server dynamically
Hi there,
i have an asp.net website with the 3 tier, and i have connect sql server database with it. now im implementing the search crateria, i have put 3 checkboxlist controls on the page. and also have two text boxes one for searching postcode and other for any business like Mcdonalds or pizza hut etc, and a search button.
my question is that when a user enter a post code or a business name or any of them can be empty as well, and also when the user select any of the checkbox in the checkbox list , as he can select either one or two or as many as he wants, so how i will make a stored procedure in sql server depending on the checkboxes selected in the checkboxlist. like if two checkboxes selected then the stored procedure should return me the result only for them two checkboxes, if more then return for them more its depends on the checkboxlist.
Any help appreciated as i am stuck now.
Thanks
i have an asp.net website with the 3 tier, and i have connect sql server database with it. now im implementing the search crateria, i have put 3 checkboxlist controls on the page. and also have two text boxes one for searching postcode and other for any business like Mcdonalds or pizza hut etc, and a search button.
my question is that when a user enter a post code or a business name or any of them can be empty as well, and also when the user select any of the checkbox in the checkbox list , as he can select either one or two or as many as he wants, so how i will make a stored procedure in sql server depending on the checkboxes selected in the checkboxlist. like if two checkboxes selected then the stored procedure should return me the result only for them two checkboxes, if more then return for them more its depends on the checkboxlist.
Any help appreciated as i am stuck now.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi thanx brad and buggycoder for the reply.
is it possible i implement it on sql server side , in a stored procedure of sql server.
as i have mentioned im working on a 3 tier so i need to keep seperate the database from the business logic..so i need to implement it in sql server and then use the parameters to access it.
any help ?
is it possible i implement it on sql server side , in a stored procedure of sql server.
as i have mentioned im working on a 3 tier so i need to keep seperate the database from the business logic..so i need to implement it in sql server and then use the parameters to access it.
any help ?
CREATE Procedure usp_fetchdata
@PostCode varchar(10),
@BusinessName varchar(10),
@Val1 varchar(10),
@Val2 varchar(10),
@Val3 varchar(10),
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SelectStatement NVARCHAR(2000)
DECLARE @FullStatement NVARCHAR(4000)
SET @WhereStatement = '';
SET @SelectStatement = 'SELECT * FROM SalesHistory '
If(@PostCode<>null)
BEGIN
SET @WhereStatement = @WhereStatement + "PostCode='" + @PostCode + "' ";
END
If(@BusinessName<>null)
BEGIN
SET @WhereStatement = @WhereStatement + "AND BusinessName='" + @BusinessName + "'";
END
--And So On for other variables
if(len(@WhereStatement)>0)
BEGIN
SET @WhereStatement = " Where " + @WhereStatement
END
SET @SelectStatement=@SelectStatement + @WhereStatement;
print @SelectStatement;
EXECUTE sp_executesql @SelectStatement;
END
The code is for SP, make some syntax changes(if required) as i don't have SSMS here...
Try this
TableName = your table name
Column1,2,3 = your filter columns
--sp_selData 'demo','valutec','mercury'
CREATE Proc sp_selData
@Val1 nvarchar(50)='',
@Val2 nvarchar(50)='',
@Val3 nvarchar(50)=''
as
select * from <TableName>
where <Column1> like '%'+ @Val1 +'%'
or <Column2> like '%'+ @Val2 +'%'
or <Column3> like '%'+ @Val3 +'%'
Go
TableName = your table name
Column1,2,3 = your filter columns
ASKER
Thank you very much all it helped a lot.
regards
regards
Then build out a string with your select statment, somethign like this (SQL CODE):
DIM SQLCode varchar(MAX)
-- doing 1=1 because easier when adding dynamic "AND" statements to force a single where no matter what
Set SQLCODE = "Select FieldList From Table WHERE 1=1"
if ISNULL(checkboxvariable, '') <> ''
SET SQLCode = SQLCODE + 'AND fieldfromTable = ''' + checboxvariable + '''
-- using 3 single quotes above for escape sequence so it forces 1 single quote to appear int he string
-- do the same for all the variable fields above
Then
EXEC(SQLCode)