Start New Discussion within our Software Development Community

Dear Experts

I use following codes to display image from sql server.

On this line of codes

Dim ms As New IO.MemoryStream(CType(row("user_img"), Byte()))

this error messages appears

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

How to get rid of this error message.
This message appears because image column is NULL.

Dim cmd As New SqlClient.SqlCommand("select user_img from login where user_sno =" & Val(Me.TextBox1.Text), con)
        Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(dr)
        Dim row As DataRow = dt.Rows(0)

        If dt.Rows.Count > 0 Then

            Dim ms As New IO.MemoryStream(CType(row("user_img"), Byte()))
            Dim img As Image = Image.FromStream(ms)
            PictureBox1.Image = img
        End If

Please Help

This question has already been answered. Start a new discussion instead.