Hi

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. _
                    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.ConnectionStrings("ConnectionString").ConnectionString)
                        Try
                            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 & _
                                 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

Recommended Answers

All 9 Replies

Before Conn.Close, you need to do a

cmd.ExecuteNonQuery

Hope this helps

Hi

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.

cmd.ExecuteNonQuery()
Conn.Close()

And what is the ex.Message value?

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

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

Conn.Open()

before executing the command.

Hope this helps

Then try

Conn.Open()

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:

Well done. This is a good point to close this thread and start a new one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.