How to retieve image from sql server c#

Posted on 2012-08-15
Last Modified: 2012-08-17
Hi All,

I've got a sql table called ImageTest that has a column called ImageData. I use the following stored procedure to return this data as follows: -

                        SqlCommand cmd2 = new SqlCommand("web.Get_Image", myConn);
                        SqlDataReader dr2 = null;
                        cmd2.CommandType = CommandType.StoredProcedure;
                        cmd2.Parameters.Add(new SqlParameter("@ImgID", cmbLoggedPartno.Text));

                        SqlParameter parm = new SqlParameter("@Image", SqlDbType.Image);
                        parm.Direction = ParameterDirection.Output; // This is important! 

                        dr2 = cmd2.ExecuteReader();

Open in new window

How do I set my imagebox imgPreview1 to display this image returned from the sql procedure please?

Many Thanks
Question by:Codingitup
    LVL 18

    Accepted Solution

    You  have to process the bytes returning from the db and convert them to image response  by calling handler in imageurl of the image control.

    Try this,


    Author Comment


    Sorry I'm new to this coding. I've tried part of the example as: -

                            SqlCommand cmd2 = new SqlCommand("web.Get_Image", myConn);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            SqlDataAdapter sda = new SqlDataAdapter();
                            DataTable dt = new DataTable();
                            sda.SelectCommand = cmd2;
                            Byte[] bytes = (Byte[])dt.Rows[0]["Image_File"];

    Open in new window

    But can't understand how to set the imagebox to display it.

    Many Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now