Solved

If Else block not working correctly

Posted on 2007-03-30
14
237 Views
Last Modified: 2010-04-16
Hi
Can anyone tell me why this if else block fails to work correctly.
It supposed to work only if the image is jpg or gif, if i enter a png etc it fails to work and sends the attachments.

protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
            {

            if (!(Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" || Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" || Img1 == null || Img2 == null || Img3 == null))
                {
                Response.Write(Img1 + " " + Img2 + " " + Img3);
                }
            else
                {
                Response.Write("Sent");
                }
            }
        }
    }
0
Comment
Question by:LearningCSharpSQL
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18822210
Do you get an error message or anything?
0
 

Author Comment

by:LearningCSharpSQL
ID: 18822247
Hi angus
No it just sends the attachments
George
0
 
LVL 3

Accepted Solution

by:
Nico earned 250 total points
ID: 18822286
I suppose if any of the conditiions in the if statement are true, then the if statement itself would evaluate to false. if (not  (true or false or false or false)= true ) = false

I would rather change your if statement to check conditions for each group:

if( (Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img1.ToLower() != null) && (Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" || Img2.ToLower() != null) && (Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" || Img3.ToLower() != null) )

In this case Img1, Img2, AND Img3 have to be a jpg or gif and may not be null.

I recommend putting the checking of conditions in a seperate file btw, this is not a very elegant if statement -> consider something like: if(ValidFile(Img1) && ValidFile(Img2) && ValidFile(Img3) ) etc.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Expert Comment

by:Nico
ID: 18822319
Whipped up a little example:

if(ValidFile(Img1) && ValidFile(Img1) && ValidFile(Img1))  {
    // send stuff
}
else {
    // don't send stuff
}
                        
private bool ValidFile(string extension) {
// GetExtension will return empty string when file doesn't have an extension and will return null when the input path is also null      
      // so if the extension is not empty or null AND has jpg or gif as extension this method will return true, in any other case will return false
      if((extension != "" || extension != null) && (extension.ToLower == ".jpg" || extension.ToLower == ".gif") {
            return true;
      }
      return false;
}
0
 
LVL 3

Expert Comment

by:Nico
ID: 18822320
(sorry for the bad formatting, I accidently sent the page before I finished editing 8-) )
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18822368
write code more structured:

protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
        {
           bool do_send = true;
           ArrayList ex = new ArrayList ();
           ex.Add(".jpg");
           ex.Add(".gif");


           if ( Img1 == null || Img2 == null || Img3 == null )
           {  do_send = false; }

           if (do_send)
           {
               do_send = (ex.FindByValue(Img1.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.FindByValue(Img2.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.FindByValue(Img3.ToLower()) == null)
           }

            if (do_send)
            {
                Response.Write("Sent");
            }
            else
            {
               Response.Write(Img1 + " " + Img2 + " " + Img3);
            }
        }
    }
0
 

Author Comment

by:LearningCSharpSQL
ID: 18822901
Hi AngellIII
I get the following error:
Error      2      'System.Collections.ArrayList' does not contain a definition for 'FindByValue'      \test.aspx.cs      118

Hi derkesthai:
I'm unable to get your code to work, still sends png attachments
George      
0
 
LVL 3

Expert Comment

by:Nico
ID: 18823110
This code is doing exactly what I think your code should do....

public Form1()
{
    InitializeComponent();

    string Img1 = ".jpg";
    string Img2 = ".gif";
    string Img3 = ".png";
    if (ValidFile(Img1) && ValidFile(Img2) && ValidFile(Img3))
    {
        MessageBox.Show("Files valid..");      
    }
    else
    {
        MessageBox.Show("One of the files has an invalid extension!");
    }
}

private bool ValidFile(string extension)
{
    if ((extension != "" || extension != null) && (extension.ToLower() == ".jpg" || extension.ToLower() == ".gif"))
    {
        return true;
    }
    return false;
}
0
 

Author Comment

by:LearningCSharpSQL
ID: 18823213
Hi derkesthai:
Being working with your example and changed it slightly to below and seems to work.
if ((Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" && Img1.ToLower() != null) && (Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" && Img2.ToLower() != null) && (Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" && Img1.ToLower() != null))

Not sure why changing the || to && when checking for null value made a differance
if ((Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img1.ToLower() != null) && (Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" || Img2.ToLower() != null) && (Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" || Img3.ToLower() != null))
Regards
George
0
 
LVL 3

Expert Comment

by:Nico
ID: 18823340
for instance: test.jpg

Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" && Img1.ToLower() != null
true OR false AND NOT null   -> only evaluates to true when img isn't null and is a jpg or gif

Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img1.ToLower() == null
true OR false OR null   -> evaluates to true when its a gif, or jpg and also when it's null

And actually, the GetExtension method only returns null when you put null in the GetExtension method. When a file doesn't have an extension then GetExtension returns an empty string.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18823357
sorry, use contains instead of FindByValue ...


protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
        {
           bool do_send = true;
           ArrayList ex = new ArrayList ();
           ex.Add(".jpg");
           ex.Add(".gif");


           if ( Img1 == null || Img2 == null || Img3 == null )
           {  do_send = false; }

           if (do_send)
           {
               do_send = (ex.Contains(Img1.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.Contains(Img2.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.Contains(Img3.ToLower()) == null)
           }

            if (do_send)
            {
                Response.Write("Sent");
            }
            else
            {
               Response.Write(Img1 + " " + Img2 + " " + Img3);
            }
        }
    }


now, just as an explanation as why yours does not work:

you need to FIRST check against the NULL, and then only against a non-null value.
0
 
LVL 3

Expert Comment

by:Nico
ID: 18823475
angelll: ArrayList.Contains() returns a boolean, not the contained object...
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18823510
thanks derkesthai for the tip:

protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
        {
           bool do_send = true;
           ArrayList ex = new ArrayList ();
           ex.Add(".jpg");
           ex.Add(".gif");


           if ( Img1 == null || Img2 == null || Img3 == null )
           {  do_send = false; }

           if (do_send)
           {
               do_send = !ex.Contains(Img1.ToLower())
           }
           if (do_send)
           {
               do_send = !ex.Contains(Img2.ToLower())
           }
           if (do_send)
           {
               do_send = !ex.Contains(Img3.ToLower())
           }

            if (do_send)
            {
                Response.Write("Sent");
            }
            else
            {
               Response.Write(Img1 + " " + Img2 + " " + Img3);
            }
        }
    }

0
 

Author Comment

by:LearningCSharpSQL
ID: 18823559
Hi
Thanks for the help, as both of you helped with the solution and both work i've split the points.
Thanks
George
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question