Display image from sql server in datagrid or image control

i try to follow article from : http://odetocode.com/articles/172.aspx
and display image from sql server image datatype.

i retrieved a row with ChatterName and ChatterImage (broken link)

ImageDataGrid.aspx
------------------
 <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
       <Columns> 
           <asp:BoundColumn DataField="ID" HeaderText="ID"
           Visible=False></asp:BoundColumn>
           <asp:BoundColumn DataField="ChatterName" HeaderText="ImageName">
           </asp:BoundColumn>
          
           <asp:TemplateColumn HeaderText="Image">
           <ItemTemplate >
              <img runat="server" src='<%# "getImage.aspx?ID=" + 
              DataBinder.Eval(Container.DataItem, "ID")%>' ID="Img1"/> 
           </ItemTemplate>
           </asp:TemplateColumn>
           </Columns>
    </asp:DataGrid>

getimage.aspx
-------------
protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlDataReader Reader = null; 
        string ImageIdentity = Request.QueryString["ID"].ToString(); 
        SqlConnection sqlConnection = new SqlConnection(@"initial catalog=Articles;userid=sa;password=;server=myServer"); 
        sqlConnection.Open(); 
        SqlCommand sqlCommand = new SqlCommand("Select ChatterName, ChatterImage from ChatterTable where ID=" 
            + ImageIdentity, sqlConnection); 
        Reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); 
        Reader.Read();

        byte[] byteArray = (byte[])Reader["ChatterImage"];

        Response.ContentType = ".jpg;"; 
        Response.BinaryWrite(byteArray);

        System.IO.MemoryStream mstream = new System.IO.MemoryStream(byteArray, 0, byteArray.Length); 
        System.Drawing.Image dbImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(byteArray)); 
        System.Drawing.Image thumbnailImage = dbImage.GetThumbnailImage(100, 100, null, new System.IntPtr()); 
        thumbnailImage.Save(mstream, dbImage.RawFormat); 
        Byte[] thumbnailByteArray = new Byte[mstream.Length]; 
        mstream.Position = 0; 
        mstream.Read(thumbnailByteArray, 0, Convert.ToInt32(mstream.Length)); 
        Response.Clear(); 
        Response.ContentType = ".jpg;"; 
        Response.BinaryWrite(thumbnailByteArray);

    }

Open in new window

LVL 1
doramail05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
Please check the following working example
http://msdn.microsoft.com/en-us/library/aa479350.aspx
0
doramail05Author Commented:
not quite, the article is showing the picture from file path, my case is showing from sql server binary data column.
0
Om PrakashCommented:
Please check other examples which will help you:

http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
http://www.aspdotnetcodes.com/Insert_Images_Database.aspx

(Store and retreive images from DB in gridview)
0
doramail05Author Commented:
beansoftware gave some idea,
but still getting broken link in Image Field.
ImageGridView.aspx
------------------
 
<asp:GridView ID="GridView1" Runat="server" 
  AutoGenerateColumns="False" 
  BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None" 
  BorderColor="#CCCCCC" Font-Names="Arial">
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <PagerStyle ForeColor="#000066" HorizontalAlign="Left" 
      BackColor="White"></PagerStyle>
    <HeaderStyle ForeColor="White" Font-Bold="True" 
      BackColor="#006699"></HeaderStyle>
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="ID">
            <ItemStyle HorizontalAlign="Center" 
              VerticalAlign="Middle"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Name" DataField="ChatterName"></asp:BoundField>
                   
                   
                   
       <asp:TemplateField HeaderText="Image">
       
           <ItemTemplate > 
              <img runat="server" src='<%# "getImage.aspx?ID=" +  
              DataBinder.Eval(Container.DataItem, "ID")%>' ID="Img1"/>  
           </ItemTemplate> 
        </asp:TemplateField>
    </Columns>
    <SelectedRowStyle ForeColor="White" Font-Bold="True" 
       BackColor="#669999"></SelectedRowStyle>
    <RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>

getimage.aspx
-------------

System.Data.SqlClient.SqlDataReader Reader = null; 
        string ImageIdentity = Request.QueryString["id"].ToString(); 
        SqlConnection sqlConnection = new SqlConnection(@"initial catalog=Articles;userid=sa;password=;server=myServer"); 
        sqlConnection.Open(); 
        SqlCommand sqlCommand = new SqlCommand("Select ChatterName, ChatterImage from ChatterTable where ID=" 
            + ImageIdentity, sqlConnection); 
        Reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); 
        Reader.Read();

        byte[] arrContent;

        arrContent = (byte[])Reader["ChatterImage"];

        Response.ContentType = "jpg";

        Response.OutputStream.Write(arrContent, 0, 10);

        Response.End();

Open in new window

0
doramail05Author Commented:
worst come to worst, try using the relative path for image
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.