Shawn D
Shawn D

Reputation: 13

C# IF statement not breaking correctly

Been banging my head against the wall and Google try to find the answer to my problem.

When the below IF statement executes is appears to be running completely through the IF and ELSE statements.

  if (IsPostBack)
         {
             Boolean bFileOK = false;

             if (fulReagentImg.HasFile)
             {
                 String sFileExtension = System.IO.Path.GetExtension(fulReagentImg.FileName).ToLower();
                 String sFileExtensionLabel = sFileExtension;
                 lblFileExtension.Text = sFileExtensionLabel;
                 String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                 for (int i = 0; i < allowedExtensions.Length; i++)
                 {
                     if (sFileExtension == allowedExtensions[i])
                     {
                         bFileOK = true;
                     }
                     else
                     {
                         lblException.Text = "Can only upload .gif, .png, .jpeg, .jpg";
                         lblException.CssClass = "red";
                     }

 }

Any ideas why it's not stopping with bFileOK = true?

Upvotes: 1

Views: 206

Answers (6)

pixelbadger
pixelbadger

Reputation: 1596

You need to add a break statement after setting bFileOk to true. As you are looping through every string in the array you're effectively only testing the last string I am the array.

I'd also investigate Array.Contains which will provide a cleaner implementation.

Upvotes: 0

Pranav
Pranav

Reputation: 8871

i think you want that when bFileOK =true you should break ; then use break statement

if (IsPostBack)
    {
        Boolean bFileOK = false;

        if (fulReagentImg.HasFile)
        {
            String sFileExtension = System.IO.Path.GetExtension(fulReagentImg.FileName).ToLower();
            String sFileExtensionLabel = sFileExtension;
            lblFileExtension.Text = sFileExtensionLabel;
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (sFileExtension == allowedExtensions[i])
                {
                    bFileOK = true;
                    break;
                }
                else
                {
                    lblException.Text = "Can only upload .gif, .png, .jpeg, .jpg";
                    lblException.CssClass = "red";
                }

}

Upvotes: 1

Jim Wooley
Jim Wooley

Reputation: 10408

Is it possible that the first item in the allowed extensions succeeds and the second one fails thus entering the first and second part of the criteria? Perhaps you meant this:

if (IsPostBack)
    {
        Boolean bFileOK = false;

        if (fulReagentImg.HasFile)
        {
            String sFileExtension = System.IO.Path.GetExtension(fulReagentImg.FileName).ToLower();
            String sFileExtensionLabel = sFileExtension;
            lblFileExtension.Text = sFileExtensionLabel;
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (sFileExtension == allowedExtensions[i])
                {
                    bFileOK = true;
                }
            }
            if (!bFileOK)
            {

                    lblException.Text = "Can only upload .gif, .png, .jpeg, .jpg";
                    lblException.CssClass = "red";

            }
     }
}

Upvotes: 0

Sconibulus
Sconibulus

Reputation: 731

You shouldn't have that else inside the for loop, or you'll post the fail message at least allowedExtensions.Length-1 times.

Move that else out of the for loop, and have it follow:

if(bFileOK)
{
//Do Stuff
}

Upvotes: 0

Seth Flowers
Seth Flowers

Reputation: 9180

Because your sFileExtension is a single extension, not all four of the allowed extensions. This means that even if the sFileExtension is ONE of the allowed extensions, it will still not be the other three, so no matter what, your else statement will get hit.

Upvotes: 1

SLaks
SLaks

Reputation: 887857

Your loop is completely wrong.
If the user's extension isn't equal to all of the extensions in your list, it will show the error.

You should call the Contains() method, preferably of a HashSet<String>.

Upvotes: 5

Related Questions