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 ThenResponse.BinaryWrite(myReader("ImageData")) End IfmyReader.Close()
myConnection.Close()
End Using End Sub
![]() |
0 |
![]() |
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 |
![]() |