I have the following code in C#:
if (FileUpload_Certificate.HasFile == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: Please upload your digital certificate";
}
else
{
if (val.isCertificate(FileUpload_Certificate) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The file uploaded is not a certificate!";
}
else
{
if(val.hasValidCA(FileUpload_Certificate) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The certificate uploaded does not come from a trusted certification authority!";
}
else
{
if (val.hasPublicKey(FileUpload_Certificate) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The certificate uploaded is not in the correct format!";
}
else
{
if (val.hasValidDate(FileUpload_Certificate) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The certificate uploaded has expired!";
}
else
{
if (val.EmptyString(ip_address) == false)
{
if (val.isIP(ip_address) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The IP address field is not in the correct format!";
}
}
if (val.EmptyString(port_number) == false)
{
if (val.IsNumeric(port_number) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The port number field must be composed of digits only!";
}
else
{
int port = 0;
try
{
port = Convert.ToInt32(port_number);
}
catch (Exception)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: There was a problem converting the port number to integer!";
}
if (port > 65535)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The port number field must be less than 65536!";
}
}
}
}
}
}
}
}
As you can see, I am first checking whether the certificate uploaded by the user is valid. These checks must be performed always.
Afterwards, I am checking if the ip address and port number entered by the user are valid. The difference, however, is that these checks should be performed in case the user entered some text in the ip address and port number fields. If the user entered nothing in both fields, the checks should be skipped.
Moreover, if any one of the fields has text in it, an error message should advise the user to enter the text in both fields.
As the code currently is, no error message is displayed in case the user enters text in either the ip address or port number fields but not both. How can I solve this please?
The following is not an answer as such, but rather some general advice on how you might get yourself into a better position to answer your own question;
The above is a really difficult-to-follow coding style. Way too many nested code blocks to follow the logic easily.
A "better" way to write this sort of code would be to involve the liberal use of return statements, like so;
if (FileUpload_Certificate.HasFile == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: Please upload your digital certificate";
return;
}
if (val.isCertificate(FileUpload_Certificate) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The file uploaded is not a certificate!";
return;
}
When the nesting is simplified like this, it will probably be much easier to solve your actual problem.
IT will also make it easier to isolate the actual area which is problematic. In this case, all the code prior to this line;
if (val.EmptyString(ip_address) == false)
is completely irrelevant and just clouds the real issue.