我有两个表中的数据库中有字段类型“图像”,并在另一个表中有字段,我想在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");
}
为什么它不工作的我传递查询字符串,但它仍然执行空值? 您的帮助将不胜感激。感谢
Someone Plz指导我该代码有什么问题。 。 – user2517610