我有兩個表中的數據庫中有字段類型「圖像」,並在另一個表中有字段,我想在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