Insert Record and File sequence

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
 
M

Michael Nemtsev [MVP]

Hello shapper,

Just rebind your ListView and data will be extracted again
or, instead of transferring data afain just add the new record to listview
manually

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


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

shapper

Hello shapper,

Just rebind your ListView and data will be extracted again
or, instead of transferring data afain just add the new record to listview
manually

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

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

I have done just that on the Inserted event of the LinqDataSource:

LinqDataSource1.DataBind()
ListView1.DataBind()

But it does not work!
Any idea?

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top