OpenFileDialog为空时返回异常

时间:2014-02-21 19:04:01

标签: c# winforms openfiledialog

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();
}

在此示例中,我想添加一名员工,但是当员工没有照片时我的意思是OpenFileDialog没有任何值,它会在添加操作时返回异常。< / p>

我需要能够在没有照片的情况下添加员工!

2 个答案:

答案 0 :(得分:1)

我认为你的问题是缺少一个else语句:

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)您仍在执行第二个语句。尝试:

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)));
}

答案 1 :(得分:0)

问题:您没有致电openFileDialogPhoto.ShowDialog()并尝试访问openFileDialogPhoto.FileName当前的null

解决方案:您应该调用openFileDialogPhoto.ShowDialog(),并且只应在用户选择文件时继续进行操作。您可以在访问DialogResult之前通过FileName返回值进行检查它的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(); } 属性。

试试这个:

{{1}}