title: Validate uploaded file extension
body: <p>Uploading files works fine but now I'm trying to validate file extensions and looks like there's some interference 
  between <code>FileUpload1</code> and <code>FileUpload2</code>.</p>
  
  <p><code>FileUpload1</code> is used for uploading .jpg or .png images, and <code>FileUpload2</code> for uploading .pdf files.</p>
  
  <p>Here's the code which is executed on <code>BtnInsert_Click</code> event:</p>
  
  <pre><code>protected void BtnInsert_Click(object sender, EventArgs e)
  {
      string[] validPhotoFile = { ".jpg", ".png" };
      string validPDFFile = ".pdf";
  
      string photoExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
      string pdfExt = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
  
      bool isValidPhotoFile = false;
      bool isValidPDFFile = false;
  
      for (int i = 0; i &lt; validPhotoFile.Length; i++)
      {
          if (photoExt == "." + validPhotoFile[i])
          {
              isValidPhotoFile = true;
              break;
          }
      }
  
      for (int i = 0; i &lt; validPDFFile.Length; i++)
      {
          if (pdfExt == "." + validPDFFile[i])
          {
              isValidPDFFile = true;
              break;
          }
      }
  
      if (!isValidPhotoFile)
      {
          PhotoErrorMessage.Text = "Upload .jpg or .png image!";
      }
  
      if (!isValidPDFFile)
      {
          PDFErrorMessage.Text = "Upload .pdf file!";
      }
  
      else
      {
          string photoFilPath = Path.GetFileName(FileUpload1.PostedFile.FileName.ToString());
          string pdfFilPath = Path.GetFileName(FileUpload2.PostedFile.FileName.ToString());
  
          string photoPath = Server.MapPath(@"~/PDFCover/" + fotoFilPath);
          string pdfPath = Server.MapPath(@"~/PDF/" + pdfFilPath);
  
          FileUpload1.PostedFile.SaveAs(photoPath);
          FileUpload2.PostedFile.SaveAs(pdfPath);
  
          SqlCommand cmd = new SqlCommand("INSERT INTO Book(Title,Content...) VALUES ('" + TextBox1.Text
              + "','" + TextBox2.Text + ... + "','" + "~/PDFCover/" + photoFilPath
              + "','" + "~/PDF/" + pdfFilPath + "')", con);
  
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();
      }
  }
  </code></pre>
  
  <p>Now even if I choose to upload valid files it's showing label error messages to upload valid files.</p>