-3
bool gGender = false;

if (radioBtnMale.Checked == true)
   gGender = true;

if (!string.IsNullOrEmpty(txtboxName.Text))
{
    if (imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)) == null)
    {
        repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text,null);
    }

    repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text, imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)));

    Information WFNameInfo = new Information("Massege", "The Employee " + txtboxName.Text + " was Added successfully");
    WFNameInfo.ShowDialog();
}
else
{
    Error err = new Error("The Employee must have a name, please try again");
    err.ShowDialog();
}

In this example I want to add an employee, but when an employee don't have a photo I mean the OpenFileDialog don't have any value it returns an exception at add operation.

I need to be able to add an employee without a photo!!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anas Salem
  • 177
  • 1
  • 2
  • 13

2 Answers2

1

I think your problem is the lack of an else statement :

if (imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)) == null)
{
    repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text,null);
}
 repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text, imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)));

if (imageToByteArray() == null) you are still executing the second statement. Try:

if (imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)) == null)
{
    repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text,null);
}
else
{
    repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text, imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)));
}
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
0

Problem : You are not calling openFileDialogPhoto.ShowDialog() and trying to access the openFileDialogPhoto.FileName which is ofcourse null.

Solution: You should call the openFileDialogPhoto.ShowDialog() and also should only proceed further if the user selects the File.you can check this by DialogResult return value ,before accessing the its FileName property.

Try This:

if(openFileDialogPhoto.ShowDialog()==DialogResult.OK)
{
   if (imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)) == null)
   {
     repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text,null);
   }
   repository.WorkflowsRepository.AddEmployee(txtboxName.Text, dateTimePickerBirthDate.Value, dateTimePickerHireDate.Value, gGender, txtboxMobile.Text, txtboxAddress.Text, txtboxEmail.Text, imageToByteArray(Image.FromFile(openFileDialogPhoto.FileName)));
   Information WFNameInfo = new Information("Massege", "The Employee " + txtboxName.Text + " was Added successfully");
   WFNameInfo.ShowDialog();
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67