1

I am trying to return a binary from the DB using linq for display in the browser. The method below using ado.net works but I am trying to ypgrade to linq but the linq version returned the error.

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) 
    Dim imageId As String = context.Request.QueryString("id")
    Dim ret As DataTable = Nothing
    ret = DPGetImageData.GetImageById(Convert.ToInt64(imageId))

        For Each dt As DataRow In ret.Rows
            For Each c As DataColumn In ret.Columns
                If Not (dt(c) Is Nothing) Then
                    context.Response.Clear()
                    context.Response.BufferOutput = False
                    context.Response.OutputStream.Write(CType(dt.Table.Rows(0).Item("imageData"), Byte()), 0, CInt(dt.Table.Rows(0).Item("imageSize")))
                    context.Response.End()
                End If

            Next
        Next


End Sub

Working Linq Version:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    If Not String.IsNullOrEmpty(Current.Request.QueryString("Id")) Then
        Dim imageId = HttpContext.Current.Request.QueryString("Id")
        Dim result = repository.FindById(Convert.ToInt64(imageId)).imageData.ToArray
        HttpContext.Current.Response.BinaryWrite(result)
        context.Response.End()

    End If
End Sub

2 Answers 2

2

You should be able to just call Response.BinaryWrite(result.ToArray()). Notice the parantheses, ToArray is a method call.

Sign up to request clarification or add additional context in comments.

1 Comment

Good catch, I dont know VB though :)
0

You should not need the cast as Binary.ToArray, returns a byte array already.

An alternative is to use a byte array directly. You can modify it in the designer.

UPDATE:

I am not fully sure, but it could be that your DataContext has been disposed already. I think the Binary type uses deferred loading.

If you add a stacktrace, we could see if that is the case.

1 Comment

I was getting a nullReference all the time because the data was not in the model. I just don't know how preferment it will be with large files. I hope someone will speak to that. I also found this article tinyurl.com/ck39un It fills in more of the blanks for me.

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.