Below is the code for an enhanced MS Access DLookup function.
My ultimate goal is to enable this function to return any type of data from a given database, my imediate goal is to get it to return binary data.
My two big problems are that:
(1) Even though I can detect that command.ExecuteScalar.GetType.name is "Byte" I get an
InvalidCastException "Conversion from type 'Byte()' to type 'Byte' is not valid."
(2) I get the warning "Function 'Dlookup' doesn't return a value on all code paths. A null reference
exception could occur at run time when the result is used."
Public Function Dlookup(ByVal Field As String, ByVal Table As String, ByVal Condition As String) As Object
Dim commandText As String = "SELECT " & Field & " FROM " & Table & " WHERE " & Condition
Using command As New OleDbCommand(commandText, dbConn)
If IsDBNull(command.ExecuteScalar()) Then
If command.ExecuteScalar.GetType.Name = "Byte" Then
Dim ByteData As Byte = command.ExecuteScalar()
ElseIf command.ExecuteScalar.GetType.Name = "String" Then
Dim StringData As String = command.ExecuteScalar()
Catch ex As InvalidCastException