Arul Selvan

Arul Selvan

  • NA
  • 26
  • 52.1k

Problem while Read a binary values from database

Feb 23 2011 10:11 PM

I used this coding for retrieve  image from database and bind it to the image control inside data list, this code already worked properly ,now i am used another one application it not read a binary values from database.. what the problem..



<asp:DataList ID="DataList1" RepeatColumns="2" RepeatDirection="Horizontal" CssClass="aligncenter"  runat="server">
       
              <ItemTemplate>
              
                <table>
                    <tr>
                    
                        <td>
                            <asp:Image ID="Image1" ImageUrl='<%# "ImageHandler.ashx?id=" + Eval("gname")%>' Width="300px" Height="220px" runat="server" /></td>
                    </tr>
                
                </table>
              </ItemTemplate>
          
    </asp:DataList>

server side coding:
Da_Layer DL = new Da_Layer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string query = "select * from gallery";
            DataSet ds = DL.Grid(query);
            DataList1.DataSource = ds.Tables[0];
            DataList1.DataBind();
        }
    }


ImageHandler.ashx coding:

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.Data.SqlClient;

public class ImageHandler : IHttpHandler {
    Da_Layer DL = new Da_Layer();
    
    public void ProcessRequest (HttpContext context) 
    {
        //context.Response.ContentType = "text/plain";


        //context.Response.Write("Hello World");





        string query = "select * from gallery where gname='"+context.Request.Params["id"]+"'";
        SqlDataReader dr = DL.ExecuteReader(query);

        dr.Read();

        Byte[] Imagedata = (byte[])dr["gphoto"];

       // context.Response.BinaryWrite(dr["gphoto"]);

        context.Response.BinaryWrite(Imagedata);
        
        dr.Close();
       

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

}

i change context.Request.Params["id"] and directly give input for select query and 
i run ImageHandler.ashx it give the Output System.byte[]
but correct answer it need to give some binary values in browser


What is the problem...... in this application i used both c# and vb


also i used this coding for convert binary value from database to image,it also not give correct answer,my question is if i want read binary value any change need web.config file

DL.OpenConnection();
        FileStream fs = new FileStream(@"c:\xx.jpg", FileMode.CreateNew, FileAccess.Write);
      
        cmd = new SqlCommand("select * from staffdetails where staff_id='" + txt_id.Text + "'", DL.connection);
        dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            byte[] b = (byte[])(dr["photo"]);
            fs.Write(b, 0, b.Length);
        }

        fs.Close();
        DL.CloseConnection();
        
        GridView1.Visible = false;
        DetailsView1.Visible = true;
        DetailsView1.DataBind();

Answers (3)