Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am young vb programmer, i want to retrieve data from sql database, its works fine but when i try to retrieve data that was saved without picture it gives me this error: Unable to cast object of type 'System.DBNull' to type 'System.Byte[]


These are my code:

VB
Dim cmd As New SqlCommand("SELECT EmpID,Fname,Oname,Lname,Date_hired,Branch,Department,Grade,Pictures FROM Staff_Information WHERE EmpID = '" & txtempID.Text & "'", con)
      con.Open()
      Dim sdr As SqlDataReader = cmd.ExecuteReader()

      If sdr.HasRows Then
          While sdr.Read()

              txtfname.Text = sdr.Item("Fname").ToString
              txtothername.Text = sdr.Item("Oname").ToString
              txtlname.Text = sdr.Item("Lname").ToString
              dtpempl.Value = sdr.Item("Date_hired").ToString
              txtbranch.Text = sdr.Item("Branch").ToString
              txtdepartment.Text = sdr.Item("Department").ToString
              txtgrade.Text = sdr.Item("Grade").ToString

              Dim data As Byte() = DirectCast(sdr("Pictures"), Byte())
              Dim ms As New MemoryStream(data)
              PictureBox.Image = Image.FromStream(ms)

          End While
          sdr.Close()
          btnsearch.Enabled = False
          txtempID.ReadOnly = True
          btnclear.Enabled = True
      Else
          MsgBox("Staff ID: " & txtempID.Text & " " & "not found.")
          txtempID.Clear()
          txtempID.ReadOnly = False
          btnsearch.Enabled = False
          btnclear.Enabled = False
      End If
      con.Close()


What I have tried:

tried this code:

If sdr("Pictures") IsNot System.DBNull Then
                   Dim data As Byte() = DirectCast(sdr("Pictures"), Byte())
                   Dim ms As New MemoryStream(data)
                   PictureBox.Image = Image.FromStream(ms)
               End If


but,System.DBNull is Underline red and gives this message:DBNull is class and can not be used as an expression
Posted
Updated 10-Aug-17 3:44am
v3

You therefore need to check if the value returned from your DB is System.DBNull first. If it is not System.DBNull then you can process your images as normal.

VB
If sdr("Pictures") IsNot System.DBNull
  Dim data As Byte() = DirectCast(sdr("Pictures"), Byte())
  Dim ms As New MemoryStream(data)
  PictureBox.Image = Image.FromStream(ms)
End If
 
Share this answer
 
Comments
Antwi 10-Aug-17 6:41am    
System.DBNull is Underline red and gives this message:DBNull is class and can not be used as an expression
Try System.DBNull.Value . As the error says, Syste.DBNull is a class and Value is a boolean property in it.

If sdr("Pictures") IsNot System.DBNull.Value
  Dim data As Byte() = DirectCast(sdr("Pictures"), Byte())
  Dim ms As New MemoryStream(data)
  PictureBox.Image = Image.FromStream(ms)
End If
 
Share this answer
 
Start by fixing the SQL Injection[^] vulnerability in your code.

You'll also want to wrap the connection, command, and data reader objects in Using blocks, to ensure that they're always cleaned up properly.

Since you're only displaying the results for a single record, you don't need a loop to read the record.
VB.NET
Using con As New SqlConnection("...")
    Using cmd As New SqlCommand("SELECT EmpID, Fname, Oname, Lname, Date_hired, Branch, Department, Grade, Pictures FROM Staff_Information WHERE EmpID = @EmpID", con)
        
        cmd.Parameters.AddWithValue("@EmpID", txtempID.Text)
        
        con.Open()
        Using sdr As SqlDataReader = cmd.ExecuteReader()
            If sdr.Read() Then
                txtempID.ReadOnly = True
                txtfname.Text = Convert.ToString(sdr.Item("Fname"))
                txtothername.Text = Convert.ToString(sdr.Item("Oname"))
                txtlname.Text = Convert.ToString(sdr.Item("Lname"))
                dtpempl.Value = Convert.ToDateTime(sdr.Item("Date_hired"))
                txtbranch.Text = Convert.ToString(sdr.Item("Branch"))
                txtdepartment.Text = Convert.ToString(sdr.Item("Department"))
                txtgrade.Text = Convert.ToString(sdr.Item("Grade"))
                
                Dim pictureIndex As Integer = sdr.GetOrdinal("Pictures")
                If sdr.IsDBNull(pictureIndex) Then
                    PictureBox.Image = Nothing
                Else
                    Dim data As Byte() = DirectCast(sdr(pictureIndex), Byte())
                    Dim ms As New MemoryStream(data)
                    PictureBox.Image = Image.FromStream(ms)
                End If
            Else
                MsgBox("Staff ID: " & txtempID.Text & " " & "not found.")
                txtempID.Clear()
                txtempID.ReadOnly = False
            End If
        End Using
    End Using
End Using

btnsearch.Enabled = False
btnclear.Enabled = True


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900