Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
methodology explains

My Method link must check

I am using the above method in link to display image in ASP.NET grid but my data is in binary form i have to use same technique is their any API available to add in asp.net grid or web api config that display binary data into grid
here is my ERROR

System.Byte[]
Posted
Updated 8-Sep-15 11:39am
v2
Comments
Sergey Alexandrovich Kryukov 8-Sep-15 17:51pm    
Please tell me this secret: what, are there such images which are not binary? What are they?
—SA
Engr Nouman 8-Sep-15 19:00pm    
then why my grid shows system.byte[] instead of image i have binary data in database
Patrice T 8-Sep-15 20:11pm    
SVG ? :)
Sergey Alexandrovich Kryukov 8-Sep-15 21:35pm    
Vector format, based on text. But all files/streams are binary, even text. Can't you see that texts are always binary? Everything is binary.
—SA
Engr Nouman 8-Sep-15 19:00pm    
then why my grid shows system.byte[] instead of image i have binary data in database

You need to create a handler (.ashx) and use below code on your GridView

XML
<asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <asp:Image ID="imgPreview" ImageUrl='<%#
                            "ImageHandler.ashx?imgID="+ Eval("ImageID") %>' runat="server"
                                Height="80px" Width="80px" />
                        </ItemTemplate>
                    </asp:TemplateField>



and here is the code you need to write on your ImageHandler.ashx file

C#
public void ProcessRequest(HttpContext context)
    {      
            SqlDataReader rdr = null;
            SqlConnection conn = null;
            SqlCommand selcmd = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
                selcmd = new SqlCommand("select Image from tblImage where ImageID=" + context.Request.QueryString["imgID"], conn);
                conn.Open();
                rdr = selcmd.ExecuteReader();
                while (rdr.Read())
                {
                    context.Response.ContentType = "image/jpg";
                    context.Response.BinaryWrite((byte[])rdr["Image"]);
                }             
                rdr.Close();
            }
            finally
            {
                conn.Close();
            }  
}
 
Share this answer
 
v6
Comments
Engr Nouman 9-Oct-15 4:37am    
i have to show 4 images in my asp.net grid?
Ajay_Saini 9-Oct-15 8:03am    
You mean 4 images in a single cell?
Engr Nouman 11-Dec-15 5:42am    
no 4 different cell db also have null values how to insert images to other 3 remaining feilds by creating more ashx files solution not wors becase id is different for other images and this one is using other it for that id that above image handler is using i shall get other customer ids (using host id) the use them to display images what will be query or alternate way
Engr Nouman 12-Dec-15 4:28am    
no 4 different cell db also have null values how to insert images to other 3 remaining feilds by creating more ashx files solution not wors becase id is different for other images and this one is using other it for that id that above image handler is using i shall get other customer ids (using host id) the use them to display images what will be query or alternate way
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900