Returning Nothing
Returning Nothing
Returning Nothing
Returning Nothing
Returning Nothing
Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing
Returning Nothing Returning Nothing
Returning Nothing
Go Back  Xtreme Visual Basic Talk > > > Returning Nothing


Reply
 
Thread Tools Display Modes
  #1  
Old 08-10-2008, 08:27 AM
Jamesanthony's Avatar
Jamesanthony Jamesanthony is offline
Contributor
 
Join Date: May 2003
Location: Dublin
Posts: 502
Default Returning Nothing


Hi
This works when it finds a record but fails if doesn't find a record
It doesn't even use the try catch,
I get an error
Object reference not set to an instance of an object.
Use the New keyword to create an instance

I have tried to say if
isdbnull(cmdmylook.executescalar.tostring)
and
if (all above) is Nothing
But still not working

Any help appreciated

Function MyLook(ByVal SQL1 As String, ByVal RV As String) As String
Dim ConMyLookup As New SqlConnection(Application("ConnStr"))
ConMyLookup.Open()
Dim CmdMyLook As New SqlCommand(SQL1, ConMyLookup)
Try
RV = (CmdMyLook.ExecuteScalar.ToString)
Catch ex As SqlException
RV = "0"
End Try
ConMyLookup.Close()
ConMyLookup.Dispose()
CmdMyLook.Dispose()
Return RV
End Function
Reply With Quote
  #2  
Old 08-10-2008, 09:49 AM
gpraceman's Avatar
gpracemanReturning Nothing gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Quote:
Originally Posted by Jamesanthony View Post
I have tried to say if
isdbnull(cmdmylook.executescalar.tostring)
and
if (all above) is Nothing
Before calling ToString (or any other method) on a value that may be null or nothing, you first need to do the null or nothing check.

So, more like this:

Code:
If Not IsDBNull(cmdmylook.ExecuteScalar) Then
  sValue = cmdmylook.ExecuteScalar.ToString
End If
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #3  
Old 08-10-2008, 10:01 AM
Jamesanthony's Avatar
Jamesanthony Jamesanthony is offline
Contributor
 
Join Date: May 2003
Location: Dublin
Posts: 502
Default

Thanks you GPRaceMan
That worked a treat.
I thought the Idea of ToString was to stop nulls etc
What do I know !
Cheers
Reply With Quote
  #4  
Old 08-10-2008, 12:16 PM
gpraceman's Avatar
gpracemanReturning Nothing gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Quote:
Originally Posted by Jamesanthony View Post
I thought the Idea of ToString was to stop nulls etc
All ToString is for is to give a string representation of an object. If that object is null (nothing) then trying any operation on it will throw a null reference exception. Thus the need to check prior to doing the operation.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Returning Nothing
Returning Nothing
Returning Nothing Returning Nothing
Returning Nothing
Returning Nothing
Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing Returning Nothing
Returning Nothing
Returning Nothing
 
Returning Nothing
Returning Nothing
 
-->