I am trying to store the filepath of uploaded files in a sql server database using vb.net. I have run the code at it uploads fine but nothing is being added to the database.
Any help will be greatly appreciated.

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/")
            Dim fileOK As Boolean = False
            If FileUploadVideo.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                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
                If fileOK Then
                    Using Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
                            Dim FilePath = path & FileUploadVideo.FileName
                            'Here you can fire your insert statement in filepath value u can pass directly that value
                            Const 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.Add("@VideoUrl", SqlDbType.NVarChar, 50, FilePath)

                            FileUploadVideo.PostedFile.SaveAs(path & _

                            lblError.Text = "File uploaded!"
                        Catch ex As Exception
                            lblError.Text = "File could not be uploaded."
                        End Try
                    End Using
                    lblError.Text = "Cannot accept files of this type."
                End If
            End If
        End If

    End Sub


Thanks for responding

I tried using that line just before but the file won't then upload. It just goes to
File could not be uploaded error message.


And what is the ex.Message value?

Sorry. I'm not sure what you mean.:$

Please, change the following

Catch ex As Exception
    lblError.Text = "File could not be uploaded. " & ex.Message
End Try

Edited 5 Years Ago by lolafuertes: n/a

Please, change the following

Catch ex As Exception
    lblError.Text = "File could not be uploaded. " & ex.Message
End Try

the message is as follows:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed

Then try


before executing the command.

Hope this helps

Yes that was what the problem was. Thank you very much. Now I just need to figure out how to retrieve the files and display them! :icon_smile:

This question has already been answered. Start a new discussion instead.