none
Retrieving Image file from table but occurs exception:Parameter is not valid. RRS feed

  • Question

  • Hello,

    I am creating an image(jpeg) file from store and Retrieve bytes[] in sql database, image datatype.I am using this jpeg image file is storing process is actually worked but I try to retrieve an image is not works also occurs a exception **Parameter is not valid**  what seems the problem and how to fix this problem?  Please provide codes. Thanks.

    Code is:

    Image retrieving 

    private void button1_Click_1(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection("Data Source=LAB-2\\SQLEXPRESS;Initial Catalog=HRPay;User ID=smart;Password=smart");
                con.Open();
    
                SqlCommand cmd = new SqlCommand("select Photo from EmployeeMasters where EmployeeFName='" + textBox1.Text + "'", con);
                try
                {
    
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        byte[] img_arr1 = (byte[])dr["Photo"];
                     
                        MemoryStream ms1 = new MemoryStream(img_arr1);
                        ms1.Seek(0, SeekOrigin.Begin);
                        MessageBox.Show(ms1.ToString());
    
    
                        pictureBox1.Image = Image.FromStream(ms1);//(Error : Parameter is not valid)
                    }
                    else
                    {
                        MessageBox.Show("Your Data is not inserted in database try again");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                }
            }


    • Edited by meena sundhar Wednesday, June 1, 2016 11:39 AM
    • Moved by DotNet Wang Thursday, June 2, 2016 1:39 AM ado.net related
    Wednesday, June 1, 2016 11:18 AM

All replies

  • Hi meena,

    Thank you for your post. Based on your description, your question is more related to ADO.NET. We will move your thread to ADO.NET Managed Providers forum for better support.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 2, 2016 1:41 AM
  • The exception indicates that the image data is not valid. How was the data stored (code)? Does it contain an OLE Object header?


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, June 2, 2016 12:41 PM
  • Hi meena sundhar,

    We could use Bitmap method to implement it, and I create a similar demo as below for your reference.

    string connstr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AspDotNetDemo;Integrated Security=True";
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("SELECT Photo FROM tblFiles WHERE Id = 1", conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
    
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["Photo"]);
                        pictureBox1.Image = new Bitmap(ms);
                    }
                }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, July 10, 2016 3:55 AM
    Moderator