Click here to Skip to main content
15,884,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to retrieve an image from database to a picturebox and I am getting the error message: "Buffer cannot be null. Parameter name: Buffer" and "Parameter is invalid". Can someone help me to figure out how to avoid this exception or how to fix the issue? Here is my Code:
public void loadimage()
        {
            ConnectionStringSettings conSetting = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];

            string connectionString = conSetting.ConnectionString;

            conn = new SqlConnection(connectionString);
            conn.Open();

            

            try
            {
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    string sss = "SELECT ProductImage from ProductServices where ProductCode=@productCode";
                    SqlCommand cmd = new SqlCommand(sss, conn);
                    cmd.Parameters.AddWithValue("@productCode", txtCode.Text);

                    Byte[] getImg = (Byte[])cmd.ExecuteScalar();
                    MemoryStream stream = new MemoryStream(getImg);
                    Image img = Image.FromStream(stream);

                    pictureBoxProduct.Image = img;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                conn.Close();
            }
            
        }


What I have tried:

I have tried changing query and peremeter but still same problem.
Posted
Updated 11-Aug-17 3:50am

1 solution

 
Share this answer
 
Comments
Graeme_Grant 11-Aug-17 10:14am    
Something that you prepared a little earlier.... :P
5+
OriginalGriff 11-Aug-17 10:26am    
It isn't the first time I've seen this question! :laugh:
Graeme_Grant 11-Aug-17 10:34am    
I got that impression... ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900