05 December, 2011

When SxecuteScalar() returns null

The Greek lowercase omega (ω) character is use...Image via Wikipedia
05-Dec-2011

I was working on a new web page today, and I decided to use ExecuteScalar() as my method to fetch a singular data that returns a string.

I completed the whole code, and during debugging, I encountered some issues. One of them is simply throwing an exception, and when I stepped through, I found that it was due to a null value being returned by the ExecuteScalar() procedure.

I then checked from the web on possible solutions to this problem, and after putting in all the patches, all scenarios can now be handled.

Here’s how it works. Detect first if the return value is null, then handle it appropriately:

if (cmd.ExecuteScalar() == null)
{
:
:
}

An else clause is then added to handle possible return values other than null:

else
{
bfrResult = cmd.ExecuteScalar().ToString();
:
:
}

And that’s all there is to it. Hope that this helps you also, as it did me.

Till then!


Enhanced by Zemanta

No comments:

Post a Comment