Solved

If Else block not working correctly

Posted on 2007-03-30
14
234 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
Comment Utility
Do you get an error message or anything?
0
 

Author Comment

by:LearningCSharpSQL
Comment Utility
Hi angus
No it just sends the attachments
George
0
 
LVL 3

Accepted Solution

by:
Nico earned 250 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:Nico
Comment Utility
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
Comment Utility
(sorry for the bad formatting, I accidently sent the page before I finished editing 8-) )
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Expert Comment

by:Nico
Comment Utility
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
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
angelll: ArrayList.Contains() returns a boolean, not the contained object...
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now