?
Solved

If Else block not working correctly

Posted on 2007-03-30
14
Medium Priority
?
240 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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