2013-03-04 86 views
0

Triyng來檢索和顯示圖像從數據庫... 我已在其中我有此一個HttpHandler:顯示圖像(來自數據庫)基於僱員的圖像控制的圖像控制

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

    'context.Response.ContentType = "text/plain" 
    'context.Response.Write("Hello World!") 

    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" 
    ' You can retrieve this also from the database 
    context.Response.BinaryWrite(imageData) 

End Sub 

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click 
    bind() 
    GridView1.Visible = "True" 
    ProcessRequest(Context) 
End Sub 

錯誤:The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event. 我哪裏錯了?我需要做什麼改變?

這是窗體上的圖像控制:

<asp:Image ID="Image1" runat="server" imageUrl="HttpHandler.ashx?employeeId=5"/> 

@Stefano阿爾鐵:

這是Employee.aspx

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click 
    bind() 
    GridView1.Visible = "True" 
    Image1.ImageUrl = "~/HttpHandler.ashx?EmployeeID='" & EmailIDTextBox.Text & "'" 
End Sub 

,這是上HttpHandler.ashx

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

    'context.Response.ContentType = "text/plain" 
    'context.Response.Write("Hello World!") 

    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" 
    ' You can retrieve this also from the database 
    context.Response.BinaryWrite(imageData) 

End Sub 
+1

你知道哪裏有異常被拋出行? – 2013-03-04 09:42:03

+0

當我打電話ProcessRequest(即,當它應該去httphandler(.ashx頁面)) – adityawho 2013-03-04 09:44:05

+0

另外...我有點困惑...這是一個頁面或HTTP處理程序?它不能都是..你必須在一個單獨的ashx文件中實現http處理程序 – 2013-03-04 09:44:19

回答

0

通用H andler五月幫助你..

+0

我已經在那裏使用通用處理程序.... – adityawho 2013-03-04 09:48:49

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")) 
Dim con As SqlConnection("your connection string")  
Dim cmd As SqlCommand("select image_colum from <table_name> where employeeID = "+employeeID) 
con.Open() 
Dim dr As SqlDataReader = cmd.ExecuteReader() 
dr.Read() 
Dim picture As Byte() = dr[0] 
context.Response.ContentType = "image/jpeg" 
context.Response.BinaryWrite(picture) 
End If 
End Sub 
+0

錯誤 - 「響應未聲明」 – adityawho 2013-03-04 10:32:09

+0

檢查現在!使用上下文。響應 – 2013-03-04 10:54:00

+0

「queryoutput沒有聲明」 – adityawho 2013-03-04 10:55:06