Ramu Eppakayala

Ramu Eppakayala

  • 1.9k
  • 24
  • 567

retreive bitmap image from database.

Feb 20 2020 2:35 AM
i am performing search operation and retreiving image from database to pictureEdit control. I stored image as varchar(4000). and images are storing in the format of System.Drawing.Bitmap.I am getting Error as -> "Unable to cast object of type 'System.String' to type 'System.Byte[]".
 
  1. private void btnSearch_Click(object sender, EventArgs e)  
  2.         {  
  3.             byte[] getImg = new byte[0];  
  4.             SqlDataAdapter dataAdapter = new SqlDataAdapter("select sid,sname,fathername,contactno,address,sphoto from Student where sclass='" + lukupClass.Text + "' and ssection='" + lukupSection.Text + "' and sname='" + textBox1.Text + "'", sqlConnection);  
  5.             DataTable dt = new DataTable();  
  6.             dataAdapter.Fill(dt);  
  7.             if (dt.Rows.Count > 0)  
  8.             {  
  9.                 txtID.Text = dt.Rows[0]["sid"].ToString();  
  10.                 txtStudentName.Text = dt.Rows[0]["sname"].ToString();  
  11.                 txtFatherName.Text = dt.Rows[0]["fathername"].ToString();  
  12.                 txtContactNo.Text = dt.Rows[0]["contactno"].ToString();  
  13.                 memoAddress.Text = dt.Rows[0]["address"].ToString();  
  14.                 getImg = ((byte[])dt.Rows[0]["sphoto"]);  
  15.                 byte[] imgData = getImg;  
  16.                 MemoryStream stream = new MemoryStream(imgData);  
  17.                 picStudent.Image = Image.FromStream(new MemoryStream(getImg));  
  18.             }  
  19.             else  
  20.             {  
  21.                 MessageBox.Show("No Data Found with this Name.");  
  22.             }  

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (7)