Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following sp:

SQL
CREATE PROCEDURE SheriffSale.usp_combinedsearch
@SaleId nvarchar(25)   		= NULL,
@City nvarchar(50)    		= NULL,
@ZipCode nvarchar(10)  		= NULL,
@County nvarchar(25)   		= NULL,
@JudgementAmountMin money 	= NULL,
@JudgementAmountMax money 	= NULL,
@AssessedValueMin money   	= NULL,
@AssessedValueMax money   	= NULL,
@saledatestart Date 		= NULL,
@saledateend Date 			= NULL
AS
SELECT s.SaleId, s.CaseNumber, s.County, s.Mapsco, s.Plaintiff, s.Defendant, 
s.Address, s.City, s.State, s.ZipCode, s.JudgementAmount, s.YearOfConstruction,
s.LegalDescription, s.AssessedValue
FROM Sale s
WHERE (s.City = @City OR @City IS NULL)
AND (s.ZipCode = @ZipCode OR @ZipCode IS NULL)
AND (s.County = @County OR @County IS NULL)
AND (s.JudgementAmount >= @JudgementAmountMin OR @JudgementAmountMin IS NULL)
AND (s.JudgementAmount <= @JudgementAmountMax OR @JudgementAmountMax IS NULL)
AND (s.AssessedValue >= @AssessedValueMin OR @JudgementAmountMin IS NULL)
AND (s.AssessedValue <= @JudgementAmountMax OR @JudgementAmountMax IS NULL) 
AND (s.SaleDate >= @saledatestart OR @saledatestart IS NULL)
AND (s.SaleDate <= @saledateend OR @saledateend IS NULL)
ORDER BY s.SaleId
OPTION (RECOMPILE)


Using C# I would like to cause the sp to execute with parameters taken from user input from textboxes which may or may not contain a value. For example, I may have a value for city and for JudgementAmountMix and JudgementAmountMax only. I would like to have the query value the search variables from the textboxes.

I also need to have the result returned to the calling method for use in a WPF datagrid. I have spent days trying different examples on the web with no results and I am running into a deadline for delivery. I know that this has to be a pretty standard type of requirement for anyone working with SQL Server, I just have run out of time and run out of patience trying to find a solution. Any assistance would be appreciated and please provide as complete a solution as possible as I am fairly new at this.
Posted
Updated 23-Aug-15 11:18am
v3

1 solution

Personally I would keep the situation simple so that I would define a value for all parameters.

For each parameter I'd choose either the user input if it exists or if not, then pass System.DBNull.Value[^] to the procedure.

So setting the parameters could be something like (if saleId is a parameter passed to the method calling the procedure.
C#
...
command.Parameters.AddWithValue("@SaleId", saleId == null ? System.DBNull.Value : (object)saleId);
...
 
Share this answer
 
v3
Comments
Member 8345618 23-Aug-15 16:37pm    
Thanks for the response! Can you provide the structure that surrounds the command.Parameters or a link that covers the details in depth. Also, how is this run? Do I use ExecuteReader, ExeccuteScalar, ExecuteNonQuery. I have seen all kinds of examples using different commands.
Do the sp results return to a variable I provide: var result = cmd.ExecuteSomething or do I need to add to my stored procedure?
Wendelius 23-Aug-15 17:07pm    
There's an example quite close to your requirement in http://stackoverflow.com/questions/14944466/sql-server-stored-procedure-result-returned[^].

Since you return a result set, not a single value, you would use a reader. Have a look at the last example in https://msdn.microsoft.com/en-us/library/d7125bke.aspx[^]
Member 8345618 23-Aug-15 17:55pm    
Thanks for the help.
Tom
Wendelius 23-Aug-15 23:17pm    
Glad to be of service :)

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