S
shapper
Hello,
I am using a ListView with a LinqDataSource to insert a record in a
SQL table with the file info and upload the file.
I need to do the following:
1. Upload the file to a temporary directory
Cancel everything (e.Cancel) if there was an error on file
uploading.
2. Insert record in database table.
The record ID is a Guid generated in the table with NewID.
And there is another field named Path where I save the record path
(including name and extension)
3. Move the file to the final folder and rename the filename to the
FileId in table (Guid)
4. Update file path in table.
Am I complicating things to much?
The problem is after I insert the record the updated path does not
show in ListView. It only shows when I refresh it.
Here is my code:
Protected Sub LinqDataSource1_Inserting(ByVal sender As Object,
ByVal e As LinqDataSourceInsertEventArgs) Handles
LinqDataSource1.Inserting
' Create file
Dim file As File = CType(e.NewObject, File)
' Create FileUploadInsert
Dim FileUploadInsert As FileUpload =
CType(FindRecursiveControl(ListView1, "FileUploadInsert"), FileUpload)
' Define path
Dim path As String = "App_Temporary\" & FileUploadInsert.FileName
' Define file path
If FileUploadInsert.HasFile Then
Try
FileUploadInsert.SaveAs(Server.MapPath(path))
file.Filename = path
Catch ex As Exception
e.Cancel = True
End Try
Else
e.Cancel = True
End If
End Sub
Protected Sub LinqDataSource1_Inserted(ByVal sender As Object, ByVal
e As LinqDataSourceStatusEventArgs) Handles LinqDataSource1.Inserted
' Create file
Dim file As File = CType(e.Result, File)
' Create FileUploadEdit
Dim FileUploadEdit As FileUpload =
CType(FindRecursiveControl(ListView1, "FileUploadEdit"), FileUpload)
' Define file
Dim ifile As New System.IO.FileInfo(Server.MapPath(file.Filename))
' Define new path
Dim path As String = "App_Assets\Documents\" &
file.FileID.ToString & ifile.Extension
' Delete file if exists
If System.IO.File.Exists(Server.MapPath(path)) Then
System.IO.File.Delete(Server.MapPath(path))
' Move file
ifile.MoveTo(Server.MapPath(path))
' Get context
Dim database As New CodeDataContext
' Update record
Dim nFile = database.Files.Single(Function(t) t.FileID =
file.FileID)
nFile.Filename = path
database.SubmitChanges()
End Sub
Thanks,
Miguel
I am using a ListView with a LinqDataSource to insert a record in a
SQL table with the file info and upload the file.
I need to do the following:
1. Upload the file to a temporary directory
Cancel everything (e.Cancel) if there was an error on file
uploading.
2. Insert record in database table.
The record ID is a Guid generated in the table with NewID.
And there is another field named Path where I save the record path
(including name and extension)
3. Move the file to the final folder and rename the filename to the
FileId in table (Guid)
4. Update file path in table.
Am I complicating things to much?
The problem is after I insert the record the updated path does not
show in ListView. It only shows when I refresh it.
Here is my code:
Protected Sub LinqDataSource1_Inserting(ByVal sender As Object,
ByVal e As LinqDataSourceInsertEventArgs) Handles
LinqDataSource1.Inserting
' Create file
Dim file As File = CType(e.NewObject, File)
' Create FileUploadInsert
Dim FileUploadInsert As FileUpload =
CType(FindRecursiveControl(ListView1, "FileUploadInsert"), FileUpload)
' Define path
Dim path As String = "App_Temporary\" & FileUploadInsert.FileName
' Define file path
If FileUploadInsert.HasFile Then
Try
FileUploadInsert.SaveAs(Server.MapPath(path))
file.Filename = path
Catch ex As Exception
e.Cancel = True
End Try
Else
e.Cancel = True
End If
End Sub
Protected Sub LinqDataSource1_Inserted(ByVal sender As Object, ByVal
e As LinqDataSourceStatusEventArgs) Handles LinqDataSource1.Inserted
' Create file
Dim file As File = CType(e.Result, File)
' Create FileUploadEdit
Dim FileUploadEdit As FileUpload =
CType(FindRecursiveControl(ListView1, "FileUploadEdit"), FileUpload)
' Define file
Dim ifile As New System.IO.FileInfo(Server.MapPath(file.Filename))
' Define new path
Dim path As String = "App_Assets\Documents\" &
file.FileID.ToString & ifile.Extension
' Delete file if exists
If System.IO.File.Exists(Server.MapPath(path)) Then
System.IO.File.Delete(Server.MapPath(path))
' Move file
ifile.MoveTo(Server.MapPath(path))
' Get context
Dim database As New CodeDataContext
' Update record
Dim nFile = database.Files.Single(Function(t) t.FileID =
file.FileID)
nFile.Filename = path
database.SubmitChanges()
End Sub
Thanks,
Miguel