0

その私のハンドラー (.ashx)

    Dim EmployeeID As Integer
If (Not (context.Request.QueryString("EmployeeID")) Is Nothing) Then EmployeeID = Convert.ToInt32(context.Request.QueryString("EmployeeID")) 
Else Throw New ArgumentException("No parameter specified") End If 
Dim imageData() As Byte = {}
' get the image data from the database using the employeeId Querystring context.Response.ContentType = "image/jpeg" 
  context.Response.BinaryWrite(imageData)

すべて正常に動作していimageDataます。長さのみが 0 であるため、画像を表示できません。

@Sean:そのelsewhr ..ここで、クエリ文字列は渡されたemployeeidを正しく取得します...

db アクセスのコードは次のとおりです。

    Public Sub bind()

    Dim ds1 As New DataSet()
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
    con.Open()
    Dim query As String = "select * from EmployeeTable"
    Dim cmd As New SqlCommand()
    Dim da1 As New SqlDataAdapter(query, con)
    da1.Fill(ds1, "EmployeeTable")
    GridView1.DataSource = ds1.Tables("EmployeeTable")
    GridView1.DataBind()
    con.Close()
End Sub
4

2 に答える 2

1

にデータのロードをロードしていますが、変数GridViewには何もロードされていません。imageDataそのため、データベースに接続してそのデータを引き出すだけです。画像列が呼び出されると想定していますimageDataが、適宜変更してください。

Dim EmployeeID As Integer

If (Not (context.Request.QueryString("EmployeeID")) Is Nothing) Then

    EmployeeID = Convert.ToInt32(context.Request.QueryString("EmployeeID")) 

Else

    Throw New ArgumentException("No parameter specified")

End If 

Dim imageData() As Byte = {}

' get the image data from the database using the employeeId Querystring

Using con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))

    Using cmd As New SqlCommand("SELECT imageData FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con) 'select imageData column, change column name as appropriate

        cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)

        Try

            con.Open()

            Using rdr As SqlDataReader = cmd.ExecuteReader()

                If rdr.Read() Then

                    imageData = CType(rdr("imageData"), Byte()) 'convert imageData column from result set to byte array and assign to variable

                End If

            End Using

        Catch ex As Exception

            'do any error handling here

        End Try

    End Using

End Using

context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(imageData)
于 2013-03-05T12:38:09.723 に答える
0

ハンドラーのコードを次のように変更しました。

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest


    Dim EmployeeID As Integer

    If (Not (context.Request.QueryString("EmployeeID")) Is Nothing) Then

        EmployeeID = Convert.ToInt32(context.Request.QueryString("EmployeeID"))

    Else

        Throw New ArgumentException("No parameter specified")

    End If

    Dim Image() As Byte = {}

    ' get the image data from the database using the employeeId Querystring

    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)

    Dim cmd As New SqlCommand("SELECT Image FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con)
    'select imageData column, change column name as appropriate

    cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)

    Try

        con.Open()

        Dim rdr As SqlDataReader = cmd.ExecuteReader()

        While rdr.Read()


            Image = CType(rdr("Image"), Byte())
            'convert imageData column from result set to byte array and assign to variable
        End While

    Catch ex As Exception

        'do any error handling here

    End Try

    context.Response.ContentType = "image/jpeg"
    context.Response.BinaryWrite(Image)

End Sub

私のために働いた、それはあなたのためにも働くかもしれません...

于 2013-03-06T10:48:45.227 に答える