Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying results using executescalar - check for null

Status
Not open for further replies.

SQLWilts

Programmer
Feb 12, 2001
651
GB
Hi,
I am writing an app to display images displayed in a SQL server varbinary table.

All is going well, as the guts of the work are happening within
Code:
byte[] content10 = (byte[])cmd10.ExecuteScalar();

Now, my issue is when the SQL command returns a null (by design).
My question is, how do I check for nulls being returned by the above command? I have tried all sorts unsuccessfully

thanks in advance


 
OK - whilst fiddling I get:
Code:
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
I understand why I am getting the error - the db query is returning null - as it should.

Problem is it will always return the above error.

As a newbie to VB.Net, am a little stumped - any ideas as to what I could do?

Returning nulls from the DB query can be changed - I can return another value - I will just write my DB query differently.
 
object oRet10 = cmd10.ExecuteScalar();

Then test if oRet10 = DBNull.Value. If not then:

byte[] content10 = (byte[])oRet10;


Sorry, I would have done better, but my C# isn't very sharp. LOL! I'm a VB.NET guy.

Senior Software Developer
 
Siruis,
Thanks for the reply. It is VB.NET!

Anyway, I'm getting a "cannot implicitly convert 'object' to 'bool' when I check for DBNull

I am running the SQL result into an object, and then want to check if that object is dbNull. the code I am using is:

Code:
         object oRet1 = cmd1.ExecuteScalar();
         if (oRet1 = DBNull.Value) goto HideImage;
any help gratefully received.
 
Wow is that the new syntax for VB.NET!!!!!!

No it isn't.


Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Chrissie,

will have to check the language settings on VS then - no wonder I'm having problems - I thought it was WAY different to VB6
 
Maybe try using IsDBNull on the first item in the byte array, as opposed to the whole thing.
 
RiverGuy,

Hhanks for your help. My forte is SQL server (MCDBA, MCITP) so I'm relatively new to this.

Sorted it though I think, and it is c#

I used

Code:
object oRet2 = cmd2.ExecuteScalar();

                if (oRet2 is DBNull)
                {
                    testtext = "2";
                    goto HideImage2;
                }

Not sure how elegant this is, as it will have to be repeated for each image, but it works, and that's all that I'm worried about.

Thanks for helping a newbie guys and girl :)
 
What you posted is C# not VB.NET.

Anywho, looks like you got it sort of figured out. To check for DBNull in VB.NET you would have used the following syntax.

Code:
If oRet2 Is DBNull.Value Then
    content02 = oRet2 'or what ever you need to do here.
End If


Also, most people would say that the use of GoTo is a bad practice and should not be used anymore. You are not working with a scripting language anymore and you can use things like "Exit For", "Exit Sub", make calls to other subroutines and functions. There are many better ways available to you than to use GoTo. Remember that all this is in VB.NET and you are using C# so you will need to translate it. If you need some simple translation help, there are many places on the web that can help you for free such as this one:

Senior Software Developer
 
Siruis,

You are absolutely right about it being C#. I installed VS and thought I would be programming in VB - no wonder I was having "issues" he he. I just thought that the language had changed in the MANY years since I had written anything in it.

Anyway, I know what you mean about goto. Hate it with avengence (going back to my mainframe days - prefered to use perform)

I have been having issues with the If Else statements, and, as this was supposed to be a "lunch time job" I thought I would use that because I got it to work.

Can you recommend a C# "brain dump" book - something short and sharp to get me started?
 
Are you using C# express?

If not, you should be able to code in VB.net from Visual Studio. You just need to select a project type from the Visual Basic category instead of C# when you create your project.

I have found the Sams Teach Yourself X in 24 Hours books to be pretty quick reads, and thorough enough to leave you with a solid grasp of the basics.

Hope this helps,

Alex

[small]----signature below----[/small]
Now you can go where the people are one!
Now you can go where they get things done!
 
Alex and Sirius - thanks guys - off to Amazon I go :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top