Can't Get Enough RSS 2.0
 Sunday, December 16, 2007

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.

Sunday, December 16, 2007 4:37:37 PM UTC  #    Comments [0] -
SQL Server
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Navigation
Categories
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Ghazi Sarhan
Sign In
Statistics
Total Posts: 13
This Year: 0
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2010, Ghazi Sarhan
DasBlog theme 'Business' created by Christoph De Baene (delarou)