I am able to save an image from a picture box into the database of SQL. I used a byte array and filestream to read the image.

FileStream fs = new FileStream(imagename, FileMode.Open, FileAccess.Read)
byte[] picbyte = new byte[fs.Length]
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length))
fs.Close()

For some reason, I can't retrieve the image into a dataset to datagridview. Also to a picturebox. Need help

Take a look at this code I made some days ago. It gets the images from DB into ImageList:

List<ImageList> list;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            list = new List<ImageList>();
            using (SqlConnection sqlConn = new SqlConnection("connString"))
            {
                string query = String.Format(@"SELECT PictureName, MyImage FROM Images");
                SqlCommand cmd = new SqlCommand(query, sqlConn);
                cmd.Connection.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ImageList _image = new ImageList();
                        _image.imageName = (string)reader[0];
                        byte[] data = (byte[])reader[1];
                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
                        {
                            Image image = new Bitmap(ms);
                            _image.imagePicture = image;
                            list.Add(_image);
                        }
                    }
                }
            }
            if (list.Count == 0)
                MessageBox.Show("There are no pictures in the database.");
        }

Mitja

I successfully binded the image to a dataset using this

byte[] picbyte = ((byte[])rdr["visitorImage"]);

                        FileStream fs = new FileStream("image.bmp", FileMode.Create);
                        fs.Write(picbyte, 0, picbyte.Length);

                        displaypb.Image = Image.FromStream(fs);

How could I do this with datagridview? When setting the column properties manually I get a row value System.Byte[]. How to read the bytes to store it to datagridview.

This article has been dead for over six months. Start a new discussion instead.