Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

If Else block not working correctly

Posted on 2007-03-30
14
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

604 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