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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executeScalar returning error 1

Status
Not open for further replies.

amberlynn

Programmer
Joined
Dec 18, 2003
Messages
502
Location
CA
I'm using the code pasted below to return a single value from SQL (in .NET 2003).
If STR_BGC_ZONE is null, I get an error "Object reference not set to an instance of an object." when the executeScalar line is called.
Is there some way to code around this?

Try
Dim strConn As String = SqlDbManager.GetConnectionString
Dim strSQL As String = "SELECT TOP 1 STR_BGC_ZONE FROM TDT_CUT_BLOCK_ECO_UNIT WHERE '" & SPID & "' = ID_SILV_PRES AND '" & SU & "' = STR_STANDARDS_UNIT"

Dim Conn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand(strSQL, Conn)
Conn.Open()
BECzone = Cmd.ExecuteScalar.ToString

Conn.Close()

Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try

Thanks!
Amber
 
this is really a .NET question. What line does the error occur on?

Jim
 
The easiest way around this would be to change your SQL, use ISNULL:
Code:
Dim strSQL As String = "SELECT TOP 1 [b]ISNULL(STR_BGC_ZONE,'')[/b] FROM TDT_CUT_BLOCK_ECO_UNIT WHERE '" & SPID & "' = ID_SILV_PRES AND '" & SU & "' = STR_STANDARDS_UNIT"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top