2013-03-05 62 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 

为我工作,可能是它会为你工作太...