I was working on a simple Address Book for my office using Sql Server and ASP.NET.
I created the table that has the fields: FirstName, LastName, Code, StaffNo and Phone.
I prepaired the web page with five TextBoxes and a GridView.
Now... writing the code...
I want when the user enters the FirstName, he gets all matching records for this FirstName... when he enters FirstName and LastName he gets all records with the matching FirstName and LastName...
This means that if a field was not supplied, it shouldn't be included in the Where clause...
This means that I will need to customize the search on the web page to check the supplied fields, build the SQL Command dynamically then run the query... !@#$%^&*
That didn't sound profissional...
I did some Google until I found this...
Select FirstName, LastName, Code, StaffNo, Phone From AddressBook Where
((@FirstName is null) or (@FirstName = FirstName)) and
((@LastName is null) or (@LastName = LastName)) and
((@Code is null) or (@Code = Code)) and
((@StaffNo is null) or (@StaffNo = StaffNo)) and
((@Phone is null) or (@Phone = Phone))
The where clause checks each parameter to see if it is NULL which means we don't want to filter our results by that parameter. If the parameter is not null it then checks the fields value with the parameter.
Remember Me
a@href@title, strike
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.