Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to Retrieve an Image from Database and Display it in WinForms?

$
0
0

Hello,

I have a WinForm application that requires saving images to and retrieving them from an SQL Server database. To cut it short, nothing is wrong with the SQL statements embedded in C# code. When returning data using the SqlDataReader, all data is read except for the binary data in image columns. Here's my code:

                rdr.Read();
                if (rdr.HasRows && rdr[0].ToString() == txt_id.Text)
                {
                    byte[] img = (byte[])rdr[4];
                    if (img != null)
                    {
                        MemoryStream ms = new MemoryStream(img);
                        pic1.Image = Image.FromStream(ms);
                    }
                }

The img is always null and I'm unable to convert the object at the specified column index to byte array. How can I read the image in order to display it in a picture box?

Thanks in advance


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>