Click here to Skip to main content
15,921,989 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
I am trying write the upload path of a file to sql server database. The file uploads fine, but nothing is being entered into the database?

Any help would be most welcome.

Thanks

VB
 Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

        If IsPostBack Then
            Dim path As String = Server.MapPath("~/UploadedVideos/")
            If FileUploadVideo.HasFile Then
                If isValidExtension(System.IO.Path.GetExtension(FileUploadVideo.FileName).ToLower()) Then
                    Using Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
                        Try
                            Dim FilePath = path & FileUploadVideo.FileName + fileExtension
                            Dim SQL As String = "INSERT INTO [Video] ([VideoName], [CourseNo], [ModuleNo], [VideoUrl]) VALUES (@VideoName, @CourseNo, @ModuleNo, @VideoUrl)"
                            Dim cmd As New SqlCommand(SQL, Conn)
                            cmd.Parameters.AddWithValue("@VideoName", txtVideoName.Text.Trim())
                            cmd.Parameters.AddWithValue("@CourseNo", cboCourse.SelectedValue())
                            cmd.Parameters.AddWithValue("@ModuleNo", cboModule.SelectedValue())
                            cmd.Parameters.AddWithValue("@VideoUrl", FilePath)

                            FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)

                            lblError.Text = "File uploaded!"
                            Conn.Close()
                        Catch ex As Exception
                            lblError.Text = "File could not be uploaded; Error: " + ex.message
                        End Try
                    End Using
                Else
                    lblError.Text = "Cannot accept files of this type."
                End If
            End If
        End If
    End Sub

Private Function isValidExtension(fileExt As String) As Boolean

    Dim allowedList As String() = {".mov", ".wmv", ".avi", ".vob", ".mp4"}

    Return allowedList.Contains(fileExt.ToLower)

End Function
Posted
Updated 26-Nov-11 20:18pm
v3
Comments
[no name] 27-Nov-11 0:30am    
What do you mean "nothing is being entered into database"? Is the SQL command not being run? Have you debugged?
DaveAuld 27-Nov-11 2:08am    
You should look at refactoring your code, as iterating through a collection to check a valid extension is not very elegant. I will edit your question with a suggested change.
DaveAuld 27-Nov-11 2:17am    
When you catch the exception you are not doing anything with the error message which may be telling you what is wrong, see edit above and then let us know if you are getting any additional feedback from the EX
PorkyGimp 27-Nov-11 6:16am    
Thanks Dave. I've managed to sort it out and it's working fine. Now I just have to figure out how to retrieve the files and display them! :)

1 solution

Try debugging the following line:

cmd.Parameters.AddWithValue("@VideoUrl", FilePath)


see what's the value of FilePath.

Regards,
Eduard
 
Share this answer
 

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