Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The problem with this code is it validate but doesn't stop page to continue with updating

What I have tried:

dataFile = Server.MapPath("~/Documents/ids/" & memberno & ".pdf")

          If File.Exists(dataFile) Then
          Else

              txtfileuploaderror2.Text = "ID document is a required field"
              txtfileuploaderror2.Focus()
          End If



          dataFile2 = Server.MapPath("~/Documents/MeansTests/" & memberno & ".pdf")
          If File.Exists(dataFile2) Then
          Else
              txtfileuploaderror4.Text = "Means Test is a required field"

              txtfileuploaderror4.Focus()

          End If



          dataFile3 = Server.MapPath("~/Documents/Contracts/" & memberno & ".pdf")
          If File.Exists(dataFile3) Then
          Else
              txtfileuploaderror3.Text = "Copy of Contract is a required field"

              txtfileuploaderror3.Focus()
              'Exit Sub
          End If
Posted
Updated 22-May-17 3:39am
Comments
[no name] 22-May-17 9:20am    
Was there a question somewhere you forgot to ask?
CHill60 22-May-17 9:29am    
Because you have commented out the only Exit Sub.
F-ES Sitecore 22-May-17 10:31am    
Having server-only validation when you have file upload controls will annoy the end user as the browser can't remember the state of them like it can text boxes etc so the user will have to re-select all of their files each time. In addition to your server validation you should impldement client validation of the fields too. If you google how to validate a file upload conrtol with javascript you'll find examples of the kind of things you can do. Checking a file is selected and checking the file extension before the form is submitted are both possible.

1 solution

You are not doing anything with the information that there is missing data. YOu could do it like this.
VB
dataFile = Server.MapPath("~/Documents/ids/" & memberno & ".pdf")
dataFile2 = Server.MapPath("~/Documents/MeansTests/" & memberno & ".pdf")
dataFile3 = Server.MapPath("~/Documents/Contracts/" & memberno & ".pdf")

If Not File.Exists(dataFile) Then
    txtfileuploaderror2.Text = "ID document is a required field"
    txtfileuploaderror2.Focus()
    Exit Sub
End If

If Not File.Exists(dataFile2) Then
    txtfileuploaderror4.Text = "Means Test is a required field"
    txtfileuploaderror4.Focus()
    Exit Sub
End If

If Not File.Exists(dataFile3) Then
    txtfileuploaderror3.Text = "Copy of Contract is a required field"
    txtfileuploaderror3.Focus()
    Exit Sub
End If

Rest of update code


But if there is more than one problem you should really let the user know about all of them as soon as possible e.g
VB
dataFile = Server.MapPath("~/Documents/ids/" & memberno & ".pdf")
dataFile2 = Server.MapPath("~/Documents/MeansTests/" & memberno & ".pdf")
dataFile3 = Server.MapPath("~/Documents/Contracts/" & memberno & ".pdf")

Dim ok As Boolean = True

If Not File.Exists(dataFile) Then
    txtfileuploaderror2.Text = "ID document is a required field"
    txtfileuploaderror2.Focus()
    ok = False
End If

If Not File.Exists(dataFile2) Then
    txtfileuploaderror4.Text = "Means Test is a required field"
    txtfileuploaderror4.Focus()
    ok = False
End If

If Not File.Exists(dataFile3) Then
    txtfileuploaderror3.Text = "Copy of Contract is a required field"
    txtfileuploaderror3.Focus()
    ok = False
End If

If Not ok Then
    Exit Sub
End If

'Rest of update code
 
Share this answer
 
v2
Comments
Samkelo Siyabonga Ngubo 22-May-17 9:47am    
Thank you this helps a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900