0

I'm assigning background images to every button according to my SQL Server database. If a byte is null I get this error:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

I want to allow buttons with no background images, but the error is preventing me.

Here is what I attempted:

Dim strsql As String
Dim ImgSql() As Byte

Using con As New SqlConnection("constring")
    con.Open()
    strsql = "SELECT Imagen FROM Inventario WHERE ID=@ID"
    Dim cmd As New SqlCommand(strsql, con)
    ItemID = 1
    cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ItemID
    Dim myreader As SqlDataReader

    myreader = cmd.ExecuteReader
    myreader.Read()
    ImgSql = myreader("Imagen")
    If ImgSql IsNot Nothing AndAlso ImgSql.Length > 0 Then

        Dim ms As New MemoryStream(ImgSql)
        btn1.BackgroundImage = Image.FromStream(ms)
        con.Close()
    Else
        'do nothing
    End If
End Using
4
  • 1
    Because your field is NULL in SQL so check for NULL first. Commented Sep 27, 2021 at 20:30
  • Is the ID column really a VarChar and not Integer? Commented Sep 27, 2021 at 21:08
  • @JoelCoehoorn whoops thanks for pointing that out. I have a bad habit to VarChar everything. Commented Sep 28, 2021 at 15:38
  • @djv I know its NULL, the point is to allow it to be NULL and still be able to run the program. Commented Sep 28, 2021 at 16:24

2 Answers 2

2
Public Function GetImage(ID As Integer) As Image
    Dim sql As String = "SELECT Imagen FROM Inventario WHERE ID=@ID"
    Using con As New SqlConnection("constring"), _
          cmd As New SqlCommand(sql, con)

        cmd.Parameters.Add("@ID", SqlDbType.Integer).Value = ID
        con.Open()
        Using myreader As SqlDataReader = cmd.ExecuteReader()
            If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then
                Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())         
                Using ms As New MemoryStream(ImgSql)
                     Return Image.FromStream(ms)
                End Using
            End If
         End Using
    End Using
End Function

' ...

btn1.BackgroundImage = GetImage(1)
Sign up to request clarification or add additional context in comments.

Comments

1

Based on this answer in C#, converted to vb.net and added NULL check

Using con As New SqlConnection("constring")
    Using cmd = con.CreateCommand()
        cmd.CommandText = "SELECT Imagen FROM Inventario WHERE ID=@ID"
        Dim ItemID = 1
        cmd.Parameters.AddWithValue("@ID", ItemID)
        con.Open()
        Dim res = cmd.ExecuteScalar()
        If res IsNot Nothing Then
            ImgSql = CType(res, Byte())
            If ImgSql IsNot Nothing AndAlso ImgSql.Length > 0 Then
                Dim ms As New MemoryStream(ImgSql)
                btn1.BackgroundImage = Image.FromStream(ms)
            End If
        End If
    End Using
End Using

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.