I'm getting a system null reference error on this code. It searches for a random filename which may or may not exist. It works fine if a filename exists. If none exist then I get this error, it doesn't seem to want to do the 'ELSE' part where I assign filename="no results"
VB.Net...
Public Function GetFeaturedItem(ByVal accountID As Guid) As String
Dim DBConnect55 As New DBConn
Dim filename As String
Using db As DbConnection = DBConnect55.Conn("DBConnectionString")
Dim cmd As SqlCommand = DBConnect55.Command(db, "SelectFeaturedItem")
cmd.Parameters.Add(New SqlParameter("accountID", SqlDbType.Uniqueidentifier, ParameterDirection.Input)).Value = accountID
db.Open()
Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While DR.Read
If NOT IsDbNull(DR("filename")) Then
filename = DR("filename")
Else
filename="no results"
End If
End While
DR.Close()
DR = Nothing
cmd.Dispose()
cmd = Nothing
db.Dispose()
db.Close()
End Using
Return filename
End Function
SQL...
SELECT TOP 1 filename FROM tblItems WHERE accountID=@accountID AND valid='1'
ORDER BY NEWID()
I CALL IT LIKE THIS..
featuredItem(noOfResults) = getFeaturedItem(DR("accountID")).ToString