Click here to Skip to main content
14,695,098 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
SqlConnection CN = new SqlConnection("server=localhost;Initial Catalog=ado2;user id=sa; password=sa");
               SqlDataAdapter ADAP = new SqlDataAdapter("Select * from ImagesStore", CN);
               DataSet DS = new DataSet();
               ADAP.Fill(DS, "ImagesStore");
               dataGridView1.DataSource = DS.Tables["ImagesStore"];

this(when i use this in window application it working) code show image from database which is save in byte[], but second one code(code is not show image in web application i have store image in database in byte[] form or image datatype is used in SQL server) do not show image in gridview why ??
SqlConnection CN = new SqlConnection("server=localhost;Initial Catalog=ado2;user id=sa; password=sa");
           SqlDataAdapter ADAP = new SqlDataAdapter("Select * from ImagesStore", CN);
           DataSet DS = new DataSet();
           ADAP.Fill(DS, "ImagesStore");
           GridView1.DataSource = DS.Tables["ImagesStore"];
           GridView1.DataBind();


<asp:GridView ID="GridView1" runat="server">
                   <Columns>
                                     <asp:TemplateField>
                                       <ItemTemplate>
                                             <asp:Image ID="Image1" Height="75px" Width="75px" ImageUrl='<%# bind("ImageData")%>' runat="server" />
                                       </ItemTemplate>
                                     </asp:TemplateField>
                                       <asp:TemplateField>
                                       <ItemTemplate>
                                       <asp:Label ID="l1" Height="75px" CssClass="allign"  runat="server" Text='<%# Bind("ImageId")%>'></asp:Label>
                                       </ItemTemplate>
                                         </asp:TemplateField>
                                      <asp:TemplateField  >
                                       <ItemTemplate>
                                          <asp:Label ID="l2" Height="75px" CssClass="allign"  runat="server" Text='<%# Bind("OriginalPath")%>'></asp:Label>
                                       </ItemTemplate>
                            </asp:TemplateField>
                         </Columns>
                   </asp:GridView>

thanx in advance...........
Posted
Updated 20-Sep-10 20:22pm
v5
Comments
Sandeep Mewara 18-Sep-10 9:25am
   
What is the error? What issue you see?
Toli Cuturicu 18-Sep-10 11:07am
   
No question
Henry Minute 18-Sep-10 14:07pm
   
I believe that someone has edited your question and removed the part that makes it sensible.
It made sense the last time I looked at it.
Can I suggest that you edit it again to restore the deleted text.

1 solution

Hey ... try this...

first u create a generic handler page with contents...

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

using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler {

public void ProcessRequest(HttpContext context)
{

context.Response.ContentType = "image/jpeg";

Stream strm = ShowEmpImage();
byte[] buffer = new byte[4096];
if (strm != null)
{
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()
{
SqlConnection conn = new SqlConnection((ConfigurationManager.ConnectionStrings["Conn"]).ToString());
string sql="select image from Item_image where id=1";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
conn.Open();
object img = cmd.ExecuteScalar();
conn.Close();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
conn.Close();
}
}

public bool IsReusable {
get {
return false;
}
}

}

this generic handler page will write byte squence..to the image as follows

Image1.ImageUrl = "~/ShowImage.ashx";
//u must specify the path correctly it is better to put this outside of all folders....like web.config

Please try this for a single image and
the rest is up to you...
   

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