Question File upload path not saving to database

ndraycott

New member
Joined
Nov 26, 2011
Messages
2
Programming Experience
Beginner
Hi
I am attempting to write the file upload path to a sql server database. The file uploads fine but nothing is entered into the database?

Any help would be greatly appreciated.

Thanks

    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/")
            Dim fileOK As Boolean = False
            If FileUploadVideo.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                GetExtension(FileUploadVideo.FileName).ToLower()
                Dim allowedExtensions As String() = {".mov", ".wmv", ".avi", ".vob", ".mp4"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
                Next
                If fileOK Then
                    Using Conn As New SqlConnection(ConfigurationManager.ConnectionStrin  gs("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."
                        End Try
                    End Using
                Else
                    lblError.Text = "Cannot accept files of this type."
                End If
            End If
        End If
    End Sub
 
try this way...


cmd.Parameters.Add("@VideoName", SqlDbType.Text, 20, "VideoName").Value = txtVideoName.Text.Trim()

cmd.ExecuteNonQuery()

Or just try what you have with
cmd.ExecuteNonQuery() after your last parameter
 
Last edited:
Back
Top