How to retrieve binary image from database and show in picture box

Last Reply 4 days ago By Mudassar

Posted 4 days ago

 

private void btnselect_Click(object sender, EventArgs e)
        {


            if (picemployee.Image != null)
            {
                picemployee.Image.Dispose();
            }


           
            try
            {
                EmpPhoto objEmpPhoto = new EmpPhoto();
                objEmpPhoto.EmployeeID = txtemployeeid.Text;
                objEmpPhoto.GetSingleEmpPhoto();
                //EmpPhoto.GetSingleEmpPhoto();
                SqlDataAdapter myAdapter1 = new SqlDataAdapter();
                DataTable dt = EmpPhoto.dtEmpPhoto;
                myAdapter1.Fill(dt);

               
                try
                {
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        if (dt.Tables[0].Rows[0][0] != System.DBNull.Value)
                        {
                            byte[] photo_aray = (byte[])dt.Tables[0].Rows[0][0];
                            System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                            Image img = (Image)converter.ConvertFrom(photo_aray);
                            pictureBox1.Image = img;
                            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                            pictureBox1.Refresh();

                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

I have facing with datatable with connection. I don't want to use sqlconnection in the code and using with external class. Please help me. How do I get the photo from the database and convert  binary to image..         The error line is at dt.Tables[0]...  Thanks in advance.  

You are viewing reply posted by: Mudassar 4 days ago.
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html