Hello sir,
The almost last thing I am struggling with is that I am trying to delete the contents from physical folder. When a "delete" query is fired from any of the data bound controls, it deletes the record from database but leaves out the stuff in physical folder as trash which will never be used again.
I have idea about deleting/renaming files/folders, but I am having problem in getting the exact Id of the file while deleting. I am using FormView. How I get the exact Id of item.
My page is
AddDeleteSong.aspx which contains the FormView. How the id of item can be acquired?
I am using following code snippet.
Code:
Private con As New SqlConnection("server=.\SqlExpress;AttachDbFileName=|DataDirectory|Gallery.mdf;Integrated Security=True;User Instance=True")
Private fileNamePath As String
Then files are being uploaded properly(except files are duplicated in the physical folder)
Code:
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Try
Dim theFileUpload As FileUpload = CType( _
FormView1.FindControl("FileUpload1"), FileUpload)
If theFileUpload.HasFile Then
fileNamePath = "~/Music/" & Guid.NewGuid.ToString()
Dim theExtension As String = Path.GetExtension(theFileUpload.FileName)
fileNamePath &= theExtension
theFileUpload.SaveAs(Server.MapPath(fileNamePath))
e.Values("SongPath") = fileNamePath
e.Values("SongExtn") = theExtension
End If
Catch ex As Exception
Throw
End Try
End Sub
deleting file is as follows.
Code:
Protected Sub FormView1_ItemDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewDeleteEventArgs) Handles FormView1.ItemDeleting
con.Open()
Dim musicId As Integer = Convert.ToInt32(Request.QueryString.Get("Id"))
Dim filePath As String = "select SongPath from Music where Id = " & musicId
Dim cmd As New SqlCommand(filePath, con)
cmd.ExecuteNonQuery()
File.Delete(filePath)
con.Close()
End Sub
musicId can be acquired by query string but where I use this query string and how.
Is there any thing wrong with the deleting code. Can you please guide me regarding this, where I should modify the
FormView1_ItemDeleting