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