2013-06-27 29 views
0

我有两个表中的数据库中有字段类型“图像”,并在另一个表中有字段,我想在FormView中检索。 formView显示来自另一个表格的字段,但未在图像控件中显示图像。 aspx文件查询字符串不传递给处理程序

<asp:FormView runat="server" ID="ListStories" DefaultMode="ReadOnly" > 
<ItemTemplate> 
<table> 
<tr><td><%#Eval("Subject") %></td></tr> 
<tr><td><%#Eval("Story") %></td></tr> 
<tr><td><%#Eval("UserName")%> </td></tr> 

</table> 
<asp:Image ID="Image1" runat="server" ImageUrl = '~/ShowImage.ashx?Name=<%#Eval("UserName") %>' Width="150" Height="150" /> 
</ItemTemplate> 


</asp:FormView> 

背后代码:

protected void Page_Load(object sender, EventArgs e) 
    { 
     RetriveStories(); 

    } 

    protected void RetriveStories() 
    { 

     conn = new SqlConnection(connString); 
     cmdStories = new SqlCommand("SELECT UserName, Subject, Story FROM Stories", conn); 

     conn.Open(); 
     reader = cmdStories.ExecuteReader(); 


     ListStories.DataSource = reader; 
     ListStories.DataBind(); 

     conn.Close(); 

    } 

图像处理程序:

public class ShowImage : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     string userName = HttpContext.Current.Request.QueryString["Name"]; 

     context.Response.ContentType = "image/jpeg"; 
     Stream strm = ShowEmpImage(userName); 
     try 
     { 
      byte[] buffer = new byte[4096]; 
      int byteSeq = strm.Read(buffer, 0, 4096); 

      while (byteSeq > 0) 
      { 
       context.Response.OutputStream.Write(buffer, 0, byteSeq); 
       byteSeq = strm.Read(buffer, 0, 4096); 
      } 
     } 
     catch (NullReferenceException) 
     { 
      context.Response.WriteFile("img/default.png"); 
     } 
    } 

    public Stream ShowEmpImage(string name) 
    { 
     string connString = ConfigurationManager.ConnectionStrings["Alumnidb"].ConnectionString; 
     SqlConnection conn = new SqlConnection(connString); 
     SqlCommand cmdRetiveImage = new SqlCommand("SELECT Photo FROM UserProfile WHERE [email protected]", conn); 
     cmdRetiveImage.CommandType = CommandType.Text; 
     cmdRetiveImage.Parameters.AddWithValue("@UserName", name); 
     conn.Open(); 
     object img = cmdRetiveImage.ExecuteScalar(); 
     try 
     { 
      return new MemoryStream((byte[])img); 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

但图像处理程序一直执行这些线路

catch (NullReferenceException) 
      { 
       context.Response.WriteFile("img/default.png"); 
      } 

为什么它不工作的我传递查询字符串,但它仍然执行空值? 您的帮助将不胜感激。感谢

+0

Someone Plz指导我该代码有什么问题。 。 – user2517610

回答

0

替换此:

... ImageUrl = '~/ShowImage.ashx?Name=<%#Eval("UserName") %>' ... 

... ImageUrl = '<%# "~/ShowImage.ashx?Name=" + Server.UrlEncode(Eval("UserName").ToString()) %>' ... 

,它应该工作。

+0

它工作得很好。 .. 非常感谢你 :) – user2517610

0

tyy this code。将帮助您

public void ProcessRequest(HttpContext context) 
    { 
     string userName = context.Request.QueryString["Name"]; 

     context.Response.ContentType = "image/jpeg"; 
     Stream strm = ShowEmpImage(userName); 
     try 
     { 
      byte[] buffer = new byte[4096]; 
      int byteSeq = strm.Read(buffer, 0, 4096); 

      while (byteSeq > 0) 
      { 
       context.Response.OutputStream.Write(buffer, 0, byteSeq); 
       byteSeq = strm.Read(buffer, 0, 4096); 
      } 
     } 
     catch (NullReferenceException) 
     { 
      context.Response.WriteFile("img/default.png"); 
     } 
    } 
+0

这一行有问题=> HttpContext.Current.Request.QueryString [“Name”]; – Manish

+0

仍然是相同的结果 – user2517610

+0

正好,但我无法弄清楚,因为我手动添加用户名,它工作正常,但它不与查询字符串 – user2517610