Link to home
Start Free TrialLog in
Avatar of mlg101
mlg101Flag for United States of America

asked on

Multiple ASP File upload boxes won't work when not all used

I have 5 asp fileupload controls on my page. If a user fills all 5 of them, when submited it works great. If they less than 5, then I get an error that says, "cannot find path". Can anyone look at my code and let me know how I can make it work whether 1, 2, 3, 4 or 5 files are uploaded? Thanks.
' Specify the path on the server to
            ' save the uploaded file to.
            Dim savePath As String = Server.MapPath("~/ServeeDocs/")
            Dim savePath2 As String = Server.MapPath("~/ServeeDocs/")
            Dim savePath3 As String = Server.MapPath("~/ServeeDocs/")
            Dim savePath4 As String = Server.MapPath("~/ServeeDocs/")
            Dim savePath5 As String = Server.MapPath("~/ServeeDocs/")
 
            
            ' Before attempting to perform operations
            ' on the file, verify that the FileUpload 
            ' control contains a file.
        
            ' Get the name of the file to upload.
            Dim fileName As String = FileUpload1.FileName
            Dim fileName2 As String = FileUpload2.FileName
            Dim fileName3 As String = FileUpload3.FileName
            Dim fileName4 As String = FileUpload4.FileName
            Dim fileName5 As String = FileUpload5.FileName
                      
            ' Append the name of the file to upload to the path.
            savePath += fileName
            savePath2 += fileName2
            savePath3 += fileName3
            savePath4 += fileName4
            savePath5 += fileName5
                
            ' Call the SaveAs method to save the 
            ' uploaded file to the specified path.
            ' This example does not perform all
            ' the necessary error checking.               
            ' If a file with the same name
            ' already exists in the specified path,  
            ' the uploaded file overwrites it.
            FileUpload1.SaveAs(savePath)
            FileUpload2.SaveAs(savePath2)
            FileUpload3.SaveAs(savePath3)
            FileUpload4.SaveAs(savePath4)
            FileUpload5.SaveAs(savePath5)
                
            ' Notify the user of the name the file
            ' was saved under.
            UploadStatusLabel2.Text = "Your file was saved as " & fileName
            Label535.Text = fileName
            Label536.Text = fileName2
            Label537.Text = fileName3
            Label538.Text = fileName4
            Label539.Text = fileName5
            
            
            Dim strConn3 As String = "Data Source=sql2005-5.reinventinc.com;Integrated Security=false;Initial Catalog=phcampbell;User ID=re100679;Password=church101"
            'Dim strConn3 As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Client.mdf;Integrated Security=True;User Instance=True"
            Dim MySQL3 As String = "INSERT INTO ScannedDocs (Docs, OrdersID, OwnerID) VALUES (@Docs, @OrdersID, @OwnerID); INSERT INTO ScannedDocs (Docs, OrdersID, OwnerID) VALUES (@Docs2, @OrdersID, @OwnerID); INSERT INTO ScannedDocs (Docs, OrdersID, OwnerID) VALUES (@Docs3, @OrdersID, @OwnerID); INSERT INTO ScannedDocs (Docs, OrdersID, OwnerID) VALUES (@Docs4, @OrdersID, @OwnerID); INSERT INTO ScannedDocs (Docs, OrdersID, OwnerID) VALUES (@Docs5, @OrdersID, @OwnerID)"
            Dim Conn3 As New SqlConnection(strConn3)
            Dim Cmd3 As New SqlCommand(MySQL3, Conn3)
            Cmd3.Parameters.Add(New SqlParameter("@Docs", Label535.Text))
            Cmd3.Parameters.Add(New SqlParameter("@Docs2", Label536.Text))
            Cmd3.Parameters.Add(New SqlParameter("@Docs3", Label537.Text))
            Cmd3.Parameters.Add(New SqlParameter("@Docs4", Label538.Text))
            Cmd3.Parameters.Add(New SqlParameter("@Docs5", Label539.Text))
            Cmd3.Parameters.Add(New SqlParameter("@OrdersID", NewJobNumber.Value.ToString()))
            Cmd3.Parameters.Add(New SqlParameter("@OwnerID", Variable.ToString()))
        
            Conn3.Open()
            Cmd3.ExecuteNonQuery()
            
            Response.Redirect("sentjobsuccess.aspx")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of chinu1310
chinu1310
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mlg101

ASKER

I tried that, but my sql insert query is trying to insert all 5, when there are less than 5 sometimes.
Avatar of mlg101

ASKER

If i do as you said, it only inserts the 1st fileupload control. the others do not get saved in database if they are present.
As a sample I placed if condition for one fileupload control.
You need check for all the fileupload controls , if they have files or not and then proceed.

Is that helping ?