Question

public EmployeeProfile(int EmpID)
{
      if (byteArrayToImage(Emp.Photo.ToArray()) != null)
            {
                pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
                pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
            }
}
public Image byteArrayToImage(Byte[] byteArrayIn)
{
       MemoryStream ms = new MemoryStream(byteArrayIn);
       Image returnImage = Image.FromStream(ms);
       return returnImage;
}

I have this code when an employee doesn't have a picture a message will appear:enter image description here

Was it helpful?

Solution

It looks like Emp.Photo is null and calling ToArray() is giving you the null reference exception. Try this:

if (Emp.Photo != null)
{
    pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
    pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
}

OTHER TIPS

Seems that your Emp originally doesn't have a Photo, which means the Emp.Photo is null, that's why you're having the NullReferenceException whose message is "Object reference not set to an instance of an object", you need first to check if your Emp object has a Photo or not :

public EmployeeProfile(int EmpID)
        {
              if (Emp.Photo != null)
                    {
                        pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
                        pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
                    }
        }

Make sure the Emp and Photo are not null:

public EmployeeProfile(int EmpID)
{
      if (Emp != null && Emp.Photo != null)
      {
           pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
           pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
      }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top