我在数据库中有两个表,其中有“图像”类型的字段,在另一个表中有我想在 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 UserName=@UserName", 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");
}
为什么它在我传递查询字符串时不起作用但它仍然执行空值?您的帮助将不胜感激。谢谢