Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Insert Record and File sequence

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 16 Feb 2008 04:06 GMT
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
Michael Nemtsev [MVP] - 16 Feb 2008 08:16 GMT
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>
shapper - 16 Feb 2008 16:04 GMT
> Hello shapper,
>
[quoted text clipped - 98 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.