Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Honors,

In my windows application am store and retrieve the images... There is no problem while store the image.. But When I fetch the image data from database the following error is rising...
System.ArgumentException: Parameter is not valid.

Here is my code...

If possible kindly alter my code....
C#
private void cmbID_SelectedIndexChanged(object sender, EventArgs e)
       {
           try
           {
               Query = "Select Photo from tblTest where ID = '" + cmbID.Text + "'";
               SqlCmd = new SqlCommand(Query, SqlCon);

                 SqlDR = SqlCmd.ExecuteReader();
                 if (SqlDR.Read())
                 {

                     byte[] imageData = (byte[])SqlDR["Photo"];


                     Image newImage;

                     using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                     {
                         ms.Write(imageData, 0, imageData.Length);

                         //Set image variable value using memory stream.
                         newImage = Image.FromStream(ms, true);
                     }

                     //set picture
                     pictureBox1.Image = newImage;
                 }
                 SqlDR.Dispose();


           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }
Posted
Updated 4-Jan-12 0:28am
v2

1 solution

Using debugger, try to narrow down from what line you're getting the exception.

One possible cause is because yuo have used literals in your SQL statement. You should use SqlParameter[^] in any case so your query would look like:
C#
SqlCmd = new SqlCommand(Query, SqlCon);
Query = "Select Photo from tblTest where ID = @id";
SqlCmd.Parameters.AddWithValue("@id,"cmbID.Text);
SqlDR = SqlCmd.ExecuteReader();
 
Share this answer
 

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



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