Sunday, March 11, 2012

Yet another IsDBNull post. Stumped!


If cmdSelectData.ExecuteScalar() is dbnull.value then
strlogo = ""
else
strlogo = cmdSelectData.ExecuteScalar()
end if

Not sure though this is the best way.
Whilst it will work it requires two trips to the database and performance will take a hit.

Although as a general rule it is bad practice to use the base type, sometimes it is the more efficent way. (always open to critisism and a better offer, pending .Net 2.0)

You could try this


object logo = cmdSelectData.ExecuteScalar()
strLogo = logo.ToString()

This works because DBNull.ToString returns an String.Empty

Hope this works
Apologies for mixing VB and C#

the first line needs a Dim statement


Dim logo As Object = cmdSelectData.ExecuteScalar()

Thanks guys.
Should have mentioned this in my earlier post but a much better way of dealing with this situation, if it is under your control, is to ensure default values are set up for fields in the database. Once you do that you don't have to worry about Nulls

No comments:

Post a Comment