2013-03-05 61 views
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) 

一切工作fine.Only的imageData長度爲0,因此該圖像是無法顯示。

@Sean:其elsewhr ..這裏的查詢字符串正確地採取通過僱員...

繼承人的數據庫訪問代碼:

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 
+2

這裏沒有代碼數據庫的訪問。 ..你填充字節數組的方式肯定有問題,否則它不會有數據。 – Sean 2013-03-05 11:46:25

+0

its elsewhr ..這裏查詢字符串正確接受employeeid傳遞... 繼承人的數據庫訪問代碼: – adityawho 2013-03-05 11:52:16

+0

好了,所以實際上並沒有將任何數據放入'imageData'變量。你在數據庫中存儲圖像數據的數據類型是什麼? – Sean 2013-03-05 12:01:55

回答

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) 
+0

不工作...圖像長度0 ... n我的圖像列名稱是圖像..我將imageData更改爲圖像無處不在... – adityawho 2013-03-06 08:59:57

+0

n jst btw,hws gridview在此.. m沒有試圖顯示它在一個gridview ..我想在圖像控件中顯示圖像... – adityawho 2013-03-06 09:01:32

+0

控件永遠不會去rdr.Read() – adityawho 2013-03-06 10:03:51

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 

爲我工作,可能是它會爲你工作太...