Reputation: 1355
I am using visual studio2010, working with asp.net. I want a query to work like example but mine doesn't work!
I have these rows in database:
Fullname=joe.address=earth,work=5656,home=23,mobile=55
Fullname=Michel.address=earth,work=5,home=2343,mobile=5435
User just enter:
Fullname=joe ,address=earth
The program should return JUST the first row. 0 is my default value for textBoxes.
SELECT fullName, address, work, home, mobile, registrationNo
FROM contacts
WHERE (fullName = @fullName OR fullName = '0')
AND (address = @address OR address = '0')
AND (work = @work OR work = '0')
AND (home = @home OR home = '0')
AND (mobile = @mobile OR mobile = '0')
Upvotes: 0
Views: 178
Reputation: 239784
You're checking the columns to be equal to '0'
, when I think you meant to check the parameters:
SELECT fullName, address, work, home, mobile, registrationNo
FROM contacts
WHERE (fullName = @fullName OR @fullName = '0')
AND (address = @address OR @address = '0')
AND (work = @work OR @work = '0')
AND (home = @home OR @home = '0')
AND (mobile = @mobile OR @mobile = '0')
As pointed out in the comments though, you really ought to use NULL
for the absence of a value - that's the main reason it exists in SQL - and also means you can use appropriate data types (where necessary), rather than always treating things as strings. (In this case, however, everything does appear to be a string).
Upvotes: 6