IsDBnull Problem.

Hi.

I have following line of code that sometimes works, and sometimes doesn't.


Open in new window

If IsDBNull(adapter.SelectCommand.ExecuteScalar) = False Then ThereIsEntry = adapter.SelectCommand.ExecuteScalar Else ThereIsEntry = "0"

Open in new window


ThereIsEntry type is String

Sometimes ThereIsEntry is "0", sometimes is "ABCD", but the problem is when ThereISEntry shows as nothing when should be DBNull since there is 0 Rows inside table.

Table is empty, but it still recognize as valid row, but just as empty space and not DBNull.
GrogelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
Maybe the origin of the problem is that you're making 2 queries to the database by calling twice to .ExecuteScalar without a real need of it. So, maybe the 1st time that you call .ExecuteScalar (and therefore you execute your stored procedure or whatever you got there) it returns one value, and the 2nd time you call it returns another value.

You should change your code for something like this (I've seen that the returned value is a string):

Dim dbValue As String = String.Empty + adapter.SelectCommand.ExecuteScalar()
If String.IsNullOrEmpty(dbValue) Then ThereIsEntry = "0" Else ThereIsEntry = dbValue

With this code, you:
1.- Avoid to call twice to the database.
2.- Avoid NULL value by setting the output to an empty string and appending the returned value (if it's null then the final value will be the empty string).

Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrogelAuthor Commented:
Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.