页面加载时无法在数据库中显示图像?

时间:2015-01-17 07:22:34

标签: c# sql-server-2008 gridview

我正在尝试在网格视图中显示图像,但由于某些逻辑错误,它显示错误。请指出我在代码中完成的错误。我的示例代码是

 string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
 protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
    {
      DataTable dt = getData();
      GridView1.DataSource = dt;
      GridView1.DataBind();
      GridView1.Visible = true;     
      }
    }
    public DataTable getData()
    {
       SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
       SqlDataAdapter dap = new SqlDataAdapter("select Photo from View_NonactiveEmpDetails", conn);
       DataSet ds = new DataSet();
       dap.Fill(ds);
       return ds.Tables[0];
    }
    public void ProcessRequest(HttpContext context)
    {
       Int32 my_Id;
       if (context.Request.QueryString["getID"] != null)
        {
           my_Id = Convert.ToInt32(context.Request.QueryString["getID"]);
           context.Response.ContentType = "image/jpeg";
           Stream strm = ShowEmpImage(my_Id);
           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);
            }
        }
    }
    public Stream ShowEmpImage(int my_Id)
    {
        string conn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        SqlConnection connection = new SqlConnection(conn);
        string sql = "select Photo from View_NonactiveEmpDetails WHERE EmpCode= @ID";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", my_Id);
        connection.Open();
        object img = cmd.ExecuteScalar();
        return new MemoryStream((byte[])img);
    }

我的.Aspx页面是

<asp:GridView ID="GridView1" runat="server">
<Columns>
    <asp:TemplateField>
    <ItemTemplate>
      <asp:Image ID="Image1" runat="server" ImageUrl='<%#"ShowImage.ashx?getID="+Eval("EmpCode") %>' />

       </ItemTemplate>
    </asp:TemplateField>
</Columns>

当我运行项目时,如果条件不符合ProcessRequest函数。请帮我解决这个错误?

<asp:Image ID="Image1" runat="server" ImageUrl='<%#"ShowImage.ashx?getID="+Eval("EmpCode") %>' />

这个EmpCode填充的地方? &#34; public void ProcessRequest(HttpContext context)&#34;是什么?功能有用吗?

0 个答案:

没有答案