2013-03-04 49 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