bucketblast
bucketblast

Reputation: 447

No value given for one or more required parameters for pictureBox1.Image != null code

I would like to improve my code. I am receiving this error because it is asking for picture to be added in the database and I think it has something to do with if statement for cmd parameters for images. The following code say if those textboxes are empty then highlight them and show a warning message. It works when all the fields are empty but when I enter details for ID and First name I will get an error message of "No value given for one or more required parameters" then it crashes. When I re-run the program and carry out the test again but this time add the image then it works as it should be.

Ideally, I would like to enter details and have the option to inset images later. Can this be done? Thank you in advance if anyone can help me.

This is the partial code I have...

if (Convert.ToInt32(cmdCheck.ExecuteScalar()) == 0)
 {
      if (!string.IsNullOrEmpty(txtID.Text) && 
      !string.IsNullOrEmpty(txtFirstName.Text) && (pictureBox1.Image == null))
      {
        cmd.CommandText = (@"INSERT INTO MainDetails (ID, FirstName, Photo)
        VALUES(@ID, @FirstName, @Photo)");

        cmd.Parameters.AddWithValue("@ID", txtID.Text);
        cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);


           // I think this where the problem is and if I change to 
           // pictureBox1.Image == null it will still give me problems.   

           if (pictureBox1.Image != null)
           {
              ms = new MemoryStream();
              pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
              byte[] photo_aray = new byte[ms.Length];
              ms.Position = 0;
              ms.Read(photo_aray, 0, photo_aray.Length);
              cmd.Parameters.AddWithValue("@Photo", photo_aray);
            }
        cmd.ExecuteNonQuery();
      }
  }

In MS Access 2003 database for Photo properties I have Required 'NO'.

Update 1

if (pictureBox1.Image != null)
                 {
                     ms = new MemoryStream();
                     pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                     byte[] photo_aray = new byte[ms.Length];
                     ms.Position = 0;
                     ms.Read(photo_aray, 0, photo_aray.Length);
                     cmd.Parameters.AddWithValue("@Photo", photo_aray);
                 }
                 else
                 {
                     cmd.Parameters.AddWithValue("@Photo", DBNull.Value); //Gord's code
                 }

Update 2

I have to include the following code when searching for record to show right image. So now it works as it should be. It was only afterwards when I opened this thread that I realised I should be searching for "How to pass null value in to the database" on Goggle. Doesn't matter now..

 if (dreader["Photo"] != System.DBNull.Value)
                 {
                     photo_aray = (byte[])dreader["Photo"];
                     MemoryStream ms = new MemoryStream(photo_aray);
                     pictureBox1.Image = Image.FromStream(ms);
                 }
                  else 
                     {
                         pictureBox1.Image = null;
                     }

THANKS GORD

Upvotes: 1

Views: 248

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123409

If you want to supply a null value for the third parameter then you can use

cmd.Parameters.AddWithValue("@Photo", DBNull.Value);

Upvotes: 2

Related Questions