Retreving images from sql to datagrid View windows Forms

Hi i have two colums in table, id and image i have stored my images in the Table ImgTB but the problem is while retreving the images. I just get a red cross.It would be great if some one can help me out
hears my code(a lot confused:))

private void disbtn_Click(object sender, EventArgs e)
        {
            
            SqlDataAdapter adpat = new SqlDataAdapter();
            adpat.SelectCommand = new SqlCommand("select * from ImgTB", con);
            con.Open();
            DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
            imageColumn.Name = "Picture";
            imageColumn.HeaderText = "picture";
            imageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom;
            try
            {
                imageColumn.Image = byteArrayToImage((byte[])dt.Tables["ImgTB"].Rows[1]["picture"]);
                
            }
            catch(Exception ex)
            {

                MessageBox.Show(e.ToString());
            }
            dataGridView1.Columns.Insert(0, imageColumn);

            adpat.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
       
        }


private Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

Try to bind data. I mean, fill dataTable and bind it to dgv:

//retreive data from db to datatable:
            SqlDataAdapter adpat = new SqlDataAdapter();
            adpat.SelectCommand = new SqlCommand("select * from ImgTB", con);
            DataTable table = new DataTable("myTable");
            adpat.Fill(table);
            
            //create image column: 
            DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
            photoColumn.DataPropertyName = "Picture";
            photoColumn.Width = 200;
            photoColumn.HeaderText = "Picture column";
            photoColumn.ReadOnly = true;
            photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
            dataGridView1.Columns.Add(photoColumn);           
            //bind data to dgv:
            dataGridView1.DataSource = new BindingSource(table, null);

Try to bind data. I mean, fill dataTable and bind it to dgv:

//retreive data from db to datatable:
            SqlDataAdapter adpat = new SqlDataAdapter();
            adpat.SelectCommand = new SqlCommand("select * from ImgTB", con);
            DataTable table = new DataTable("myTable");
            adpat.Fill(table);
            
            //create image column: 
            DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
            photoColumn.DataPropertyName = "Picture";
            photoColumn.Width = 200;
            photoColumn.HeaderText = "Picture column";
            photoColumn.ReadOnly = true;
            photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
            dataGridView1.Columns.Add(photoColumn);           
            //bind data to dgv:
            dataGridView1.DataSource = new BindingSource(table, null);

hi i get the follwing error i have attacted a picture of the error in the attactment

I cant open the file. Can you create an image? Print screen, cut the image (with photoshoop or something) and paste it here (or on some free server), and paste link here.

I cant open the file. Can you create an image? Print screen, cut the image (with photoshoop or something) and paste it here (or on some free server), and paste link here.

Now its in pdf format

solved IT

@prethum, how did you solve it, especially when some datarow has empty images (blob)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.