0

I have the following code to store image in database

private void btnSave_Click(object sender, EventArgs e)
        {   
            MemoryStream ms = new MemoryStream();
            pboxImage.Image.Save(ms, pboxImage.Image.RawFormat);
            byte[] byteimage = ms.ToArray();  

            AddClient.ADD_CLIENT(txtName.Text, byteimage);   
            MessageBox.Show("add successfuly);
        }

AddClient

public void ADD_CLIENT(string de_fullName, byte[] de_photo)
        {

            DAL.open();

            SqlParameter[] param = new SqlParameter[2];

            param[0] = new SqlParameter("@de_fullName", SqlDbType.NVarChar, 50);
            param[0].Value = de_fullName;


            if ( de_photo == null)
            {
                param[1] = new SqlParameter("@de_photo", SqlDbType.Binary);
                param[1].Value = DBNull.Value;
            }
            else
            {
                param[1] = new SqlParameter("@de_photo", SqlDbType.Binary);
                param[1].Value = de_photo;
            }


            DAL.ExecuteCommand("ADD_CLIENT", param);
            DAL.close();

        }

it works if pboxImage has image but when it has no photo it gives me an error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

John Deck
  • 787
  • 1
  • 12
  • 27
  • 1
    Well, if `pboxImage.Image` is null (i.e. "when [`pboxImage`] has no photo"), what would you expect to happen when you try accessing `pboxImage.Image.RawFormat` (or later (in execution sequence) `pboxImage.Image.Save` for that matter)? – Corak Sep 15 '17 at 10:24
  • so how to edit my code to pass a null value in that case ? – John Deck Sep 15 '17 at 10:44
  • *You* tell *me*. You know now what the problem is, you know how "branching execution paths" (if/else) work, so what would they look like, if put together? (Hint: *somewhere* there should probably be something like `if (pboxImage.Image == null) { [...] }`) – Corak Sep 15 '17 at 12:35

0 Answers0