views:

43

answers:

3

Hi,

I am trying to do a SQL query to see if an Address already exists in my database. My addresses are structure like this:

line1
line2
line3
city
state
zipcode
country

Sometimes line2 and line3 are NULL values in my database.

I am using .NET TableAdapter and DataTable to make my query. When I try to pass in my line2 parameter (@line2) it is equal to a string with value = nothing. My database does not interpret this as NULL and says the records don't match, but I know they do?

How do I pass in a string = nothing and have it compare to a NULL value in the database as being equal?

I have tried string = DBNULL.value but won't compile. I have done similar comparisons with integers etc. using integer = Nullable(of integer) but this does not exist for strings.

Thanks.

+1  A: 

You need to use DBNull.Value

Joel Coehoorn
I cannot set string = DBNULL.VALUE. They are different types and won't compile
Hunter
Don't set it to your string variable. You said you're using a datatable for the query, so set it to your the cell in your datatable or the parameter value in your dataadapter update or delete command directly.
Joel Coehoorn
Don't know how? My TableAdapter Query requires a String to be passed in as parameter TableAdapter.CheckAddressExists(Line1 as string, line2 as string, etc.)
Hunter
A: 

Null is special in most queries. Usually the way to specify matching null is "line2 is NULL"

Thomas Jones-Low
Cannot pass "line2 IS NULL" as a parameter. IN my query if would look like:WHERE line2 = line2 IS NULL
Hunter
the Parameter should be "<value> OR line2 is null"
Thomas Jones-Low
A: 

Use DBNull.Value is a start, but I don't think it will work because when it will do NULL = NULL, it will always return false.

If an empty string is the same as NULL for you, then you can change your query to to ISNULL(line2,"") as line2, etc... and pass "" as parameter when your string is Nothing.

If you can't do that, you can modify your query to check if your parameter is null and if so, then verify that line2 IS NULL and not when it's not.

Hope that helps.

David Brunelle
Using VB.NET. Empty string not same as NULL. Was hoping I didn't have to modify my SQL query on the backend to do this.
Hunter