Click here to Skip to main content
Licence 
First Posted 16 Aug 2007
Views 14,992
Downloads 155
Bookmarked 29 times

Storing And Displaying Image From Database

By | 16 Aug 2007 | Article
This article describe how to add image directly to database and how to view the Image stored from Database.
 
Part of The SQL Zone sponsored by
See Also

Introduction

Actually it is not good to store Image file into Database But some time it is necessary to store it. Here I am giving solution to storing and displaying Image to Database.

Using the code

This will help you to storing and Displaying Image to Database -here Sql Server 2005.

First of all create page named AddEmployee.aspx and add repeater to it shown in source file.

The main idea is that: I am storing Image files into database directly into Binary Format. So storing it first convert it into Binary:

            SqlParameter prmFName = new SqlParameter("@_FName", txtFName.Text.Trim());
            cmdAddEmp.Parameters.Add(prmFName);
            SqlParameter prmLName = new SqlParameter("@_LName", txtLName.Text.Trim());
            cmdAddEmp.Parameters.Add(prmLName);
            int len = FIleUP.PostedFile.ContentLength;
            byte[] img = new byte[len];
            FIleUP.PostedFile.InputStream.Read(img, 0, len);
            SqlParameter prmImg = new SqlParameter("@_Image", img);
            cmdAddEmp.Parameters.Add(prmImg);
            int Status = cmdAddEmp.ExecuteNonQuery();  

Now at Image viewing time:

      <td id="tdImage" runat="server">
          <asp:Image ID="img" runat="server" AlternateText='<%# Eval("FirstName") %>' ImageUrl='<%# "~/displayimage.aspx?ID="  + DataBinder.Eval(Container.DataItem,"Id") %>'  Width="50px" Height="50px"/>
      </td>

At the DisplayImage.aspx Page:

            cmdAddEmp.CommandText = "GetImage";
            cmdAddEmp.CommandType = CommandType.StoredProcedure;
            SqlParameter prmId = new SqlParameter("@_Id", Convert.ToInt32(Request.QueryString["Id"].ToString()));
            cmdAddEmp.Parameters.Add(prmId);
            byte[] byteImg = (byte[])cmdAddEmp.ExecuteScalar();
            Response.BinaryWrite(byteImg); 

Response.BinaryWrite(byteImg) writes binary content of the Image and display Image.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

N a r e s h P a t e l

Web Developer

India India

Member

Hi

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralScrolling images in asp from database PinmemberSvtpooja23:40 29 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 17 Aug 2007
Article Copyright 2007 by N a r e s h P a t e l
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid