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

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

anyone knows how to write some code to check if my binary data is null then continue check another data?

I've tried but seems that byte and string can't convert...

 (hope someone can provide me code in VB and where to insert them :))

 Here's my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ID As Integer = Convert.ToInt32(Request.QueryString("ID"))

Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

Const SQL As String = "SELECT [ImageData] FROM [RecentPost] WHERE [ID] = @ID"

Dim myCommand As New SqlCommand(SQL, myConnection)myCommand.Parameters.AddWithValue("@ID", ID)

myConnection.Open()

Dim myReader As SqlDataReader = myCommand.ExecuteReader

If myReader.Read Then

Response.BinaryWrite(myReader("ImageData"))

End If

myReader.Close()

myConnection.Close()

End Using

End Sub

0
mvpm
4/29/2008 6:46:40 AM
asp.net.object-datasource 16182 articles. 0 followers. Follow

1 Replies
1312 Views

Similar Articles

[PageSpeed] 12
Get it on Google Play
Get it on Apple App Store

Try this snippet below

 

Dim ID As Integer = Convert.ToInt32(Request.QueryString("ID"))
Dim conn As New SqlConnection("YOUR CONNECTION STRING")

conn.Open()
Dim sql As String = "SELECT [ImageData] FROM [RecentPost] WHERE [ID] = @ID"
Dim cmd As New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@ID", ID)
cmd.CommandType.Text
cmd.ExecuteNonQuery()
Dim ad As New SqlDataAdapter(cmd)
ad.Fill(dt)


Dim dt As DataTable
If dt.Rows.Count > 0 Then
    For i As Integer = 0 To dt.Rows.Count - 1
        context.Response.BinaryWrite(CByte(dt.Rows(i)("ImageData")))
    Next
End If 


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
4/29/2008 8:48:28 AM
Reply: