Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

How to retieve image from sql server asp.net c#

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! 
                        cmd2.Parameters.Add(parm);

                        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
Lee
0
Codingitup
Asked:
Codingitup
1 Solution
 
Rajar AhmedConsultantCommented:
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,
http://odetocode.com/articles/172.aspx
http://www.aspsnippets.com/Articles/Display-images-from-SQL-Server-Database-in-ASP.Net-GridView-control.aspx

Meeran03
0
 
CodingitupAuthor Commented:
Hi,

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();

                        myConn.Open();
                        sda.SelectCommand = cmd2;
                        sda.Fill(dt);
                        myConn.Close();
                        sda.Dispose();

                        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
Lee
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now