Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding an ELSE clause somewhere

    Hi everyone,

    Quick query if I may. Whilst I got to the bottom of a problem last week, thanks to this forum, another one has reared its ugly head; and it is as follows.

    I have an online form which allows users to upload a photo, however, if a user attempts to submit the form without uploading a photo, ASP.NET throws an exception. Question, how can I modify the following code so as to avoid an exception. I have a feeling I need to incorporate an ELSE clause somehere.

    Protected Sub frmPhoto_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
    ' Get the FileUpload control
    Dim upPhoto As FileUpload = CType(frmInsert.FindControl("upPhoto"), FileUpload)
    srcEmployee.InsertParameters("FileName").DefaultValue = upPhoto.FileName

    ' Save contents to file system
    Dim savePath As String = MapPath("~/Photos/" + upPhoto.FileName)
    upPhoto.SaveAs(savePath)
    End Sub

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Just use a validator. They're good for that.

    Code:
            <asp:RequiredFieldValidator runat="server" id="fileUploadControlRequired"
            ErrorMessage="You didn't choose a file to upload. Please choose a file that is on your computer, then try again."
            ControlToValidate="fileUploadControl" 
            Display="Dynamic" />

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I've used vaildators qute extensively for the important textboxes, however I shouldn't expect users to have images on hand; that's why it cannot be enforced by a validation property. Do you not think an 'if else' will do?

  • #4
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [ASP.Net 2.0]

    Use the HasFile property to make sure the user had selected a file to upload:

    Code:
    If upPhoto.HasFile Then
    	'code here
    	upPhoto.SaveAs(savePath)
    End If

  • #5
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Quote Originally Posted by Max01 View Post
    Thanks. I've used vaildators qute extensively for the important textboxes, however I shouldn't expect users to have images on hand; that's why it cannot be enforced by a validation property. Do you not think an 'if else' will do?
    Sure, if it isn't actually required.
    See otaku149's solution.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #6
    New Coder
    Join Date
    Nov 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I very much appreciate both of your comments; thanks. The code works perfectly and was inserted as follows:

    Protected Sub frmPhoto_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)

    ' Get the FileUpload control
    Dim upPhoto As FileUpload = CType(frmInsert.FindControl("upPhoto"), FileUpload)
    If upPhoto.HasFile Then
    srcEmployee.InsertParameters("FileName").DefaultValue = upPhoto.FileName

    ' Save contents to file system
    Dim savePath As String = MapPath("~/Photos/" + upPhoto.FileName)
    upPhoto.SaveAs(savePath)

    End If
    End Sub


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •