SIGN UP MEMBER LOGIN:    
ARTICLE

Save and retrieve image from SQL 2005 in ASP.Net

Posted by Gomathi Palaniswamy Articles | ASP.NET Programming August 16, 2011
In this article you will learn how to save and retrieve image from SQL 2005 in ASP.Net
Reader Level:
Download Files:
 

How to save image in database

Data base design:

  1. Create SQL table with the name of "ImageExample" and the create column with image datatype as Image.

    sql table

Store Image in Grid:

To store image in database first we have to open and read the file and convert the file into byte array. For saving to database

The C# coding to save image:

    SqlConnection con = new SqlConnection("Connection string");
                                con.Open();
            
    SqlCommand oc=new SqlCommand("insert into TableName values(@Image)",con);
    string path=@"Path\imagename.JPG";
                                FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read);
                                BinaryReader br=new BinaryReader(fs);
                                 FileInfo fi=new FileInfo(path);
                                byte[] imagedata = br.ReadBytes((int)fi.Length);
                                oc.Parameters.Add("@Image", imagedata);
                                oc.ExecuteNonQuery();
                                con.Close();


Retrieve image from database using handler.

  1. Right click on solution explorer and select add new item.In that select hanler.ashx and rename as ImageHandler.ashx

Code used in Imagehandler.aspx


    public
class Handler1 : IHttpHandler
    { 
        public void ProcessRequest(HttpContext context)
        {
            string imageid = context.Request.QueryString["Image"];
            SqlConnection con = new SqlConnection("Connectionstring");
            con.Open();
            SqlCommand command = new SqlCommand("select Image from ImageExample", con);
            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])dr[0]);
            con.Close();
            context.Response.End();
        }
 
        public bool IsReusable
        {
           
get
            {
                return false;
            }
        }
    }


Code used in aspx page

    <
asp:GridView ID="GridView1" runat="server" Width="500px" AutoGenerateColumns="false">
    <Columns>      
           <asp:TemplateField HeaderText="Image">
           <ItemTemplate>
           <asp:Image ID="Image" runat="server" ImageUrl='<%#"ImageHandler.ashx"%>'/>
           </ItemTemplate>
           </asp:TemplateField>
    </Columns>
    </asp:GridView>


Code used in Retrieve image using C#

   SqlConnection con = new SqlConnection("Connnection string");
   con.Open();
   SqlCommand oc = new SqlCommand("Select * from ImageExample", con);
   SqlDataAdapter da = new SqlDataAdapter(oc);
   DataTable dt = new DataTable();
   da.Fill(dt);          
   GridView1.DataSource = dt;
   GridView1.DataBind();

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks a lot sir..i am just searching for the same. :-)

Posted by surender bhyan Sep 01, 2011

nice article its very useful thank u

Posted by Sujitha Aug 18, 2011

Good article for beginners. thanks

Posted by Rohatash Kumar Aug 16, 2011

Good Article

Posted by Dea Saddler Aug 16, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor