Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2008, 09:19 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default Creating a 'File Upload' in DetailsView

Hi! I am working on another project but I hope you can help me out.

I am trying to create a file upload input in DetailsView. I am trying to implement the VB.NET code on page 461 of the Beginning ASP.NET 3.5 book.

Above the code 'Dim FileUpload1 As FileUpload = CType(ListView1.InsertItem.FindControl("FileUpload 1"), FileUpload) there is another statement 'myPicture.PhotoAlbumId = Convert.ToInt32...' and above that not stated in the book but in the file ManagePhotoAlbum.aspx.vb there is this declaration 'Dim myPicture As Picture = CType(e.NewObject, Picture)'.

I am working on another project and therefore the 'Picture' class is not available and VWD gives me a warning which says 'Type Picture is not defined'.

How shall I create the 'Picture' Class? I have looked it up in Site and it is defined in 'Site\App_Code\PlanetWrox.designer.vb'. Unfortunately, I do not understand it in designer.vb.

Can anyone give me a pointer on how to create a file upload input in DetailsView or how to create the 'Picture' class easily?

Thanks for helping me out.

 
Old June 15th, 2008, 01:39 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

The Designer file you are talking about is created by the LINQ designer and is the "code behind" file of the .dbml file. LINQ is discussed in detail in chapter 13, so you may want to take a look there.

In the code, the Picture class is used to hook up an uploaded picture to a Picture record in the database. Since I don't know the design of your application, I can't tell if you need something similar in your case.

If all you need to do is store the uploaded file, just use the SaveAs method of the FileUpload control.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 16th, 2008, 01:40 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Imar! Thanks for your reply.

I need to store the file in a database, so I can't use the SaveAs method of the FileUpload control. I am implementing it on a ASP.NET 2.0 system, so I cannot use LINQ.

I have given some thought about how store the file, which is an image file. There are 2 possibilities:

1) To store the file directly into the SQL database, which is limited to 2^31 - 1 bytes. That means I declare the datatype of the column in MSSQL as 'image' and save the binary image file into the database. Question is: how do I implement it in DetailsView?

2) The second possibility is to use the method similar that described in your book. Upload the image file to disk and assign it a picture ID in the database that points to the file on disk. Question is: Can it be done without using LINQ? Can I use normal MS-SQL? This brings back the question of the 'Picture' class as described above.

What is your opinion?

Thanks.



 
Old June 16th, 2008, 01:44 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, it can certainly be done without LINQ. The Picture class is convenient, but by no means a requirement.

Simply save the file to disk, rename it using Guid.NewGuid() so it gets a unique name and then save a record in the database using standard SQL syntax with SqlConnection and SqlCommand objects.

For a more detailed description and a comparison between storing images in the database and on disk, take a look here: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=414

Hope this helps. If not, please provide more information about your current code and setup, and whether you know how to use direct SQL in your code.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 16th, 2008, 03:21 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Imar! Thanks for your reply.

I am taking things one step at a time. Firstly, I am testing out if the insert function of the DetailsView works with the browse button of the FileUpload control. I implemented the following:

Code:
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) 
Handles DetailsView1.ItemInserting
        Dim FileUpload1 As FileUpload = CType(DetailsView1.InsertItem.FindControl("FileUpload1"), FileUpload)
        Dim virtualFolder As String = "~/images/"
        Dim physicalFolder As String = Server.MapPath(virtualFolder)
        Dim fileName As String = Guid.NewGuid().ToString()
        Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)

        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))

    End Sub


Firstly, I get an error message for DetailsView1.InsertItem.FindControl

Error 1 Argument not specified for parameter 'causesValidation' of 'Public Overridable Sub InsertItem(causesValidation As Boolean)'.


I am sure I have a FileUpload control in my DetailsView1. I wonder why this line is underlined by VWD. Maybe it is not finding a FileUpload control?

 
Old June 16th, 2008, 03:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're mixing up controls and their abilities.

The code you are using comes from my example where I use a ListView. A ListView has an InsertItem property that refers to an item in the list to allow you to insert items.

The DetailsView on the other hand has an InsertItem method that is used to insert a record in the underlying datasource. The two are not interchangeable, so you won't be able to use FindControl on InsertItem for a DetailsView. (hint: the fact that IntelliSense doesn't kick in on DetailsView1.InsertItem. is often a good clue you're going in the wrong direction.)

I think this:

DetailsView1.FindControl(....)

should give you the right control.

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 16th, 2008, 03:44 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks Imar!

I changed it and ran the application. I clicked on 'Browse' and I selected a file and I clicked on 'Update'. Problem is the file does not appear in the ~/images directory as I have specified in the above code. What could be wrong? I thought the SaveAs will save the file to that location but it did not. (I have not implemented the database section of the code yet. I just want to see if the file is move to the ~/images location.

Please advise. Thanks.

 
Old June 16th, 2008, 06:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Are you sure the code gets hit? What happens when you set up a break point on the line with FileUpload1.SaveAs and debug the code?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 16th, 2008, 09:37 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Imar. You are right!!

I should have put the code in both DetailsView1_ItemInserting and DetailsView1_ItemUpdating. That's the problem. The file is now saved into the images directory.

Now the next step: I have declared the photopath as varchar(200) which keeps the photo URL and the filename. Problem is when I click on 'update', the database is for the photopath still null. It is the same for other data fields, like name or address in the DetailsView which I am trying to change. The data is not updated.

I have another GridView which displays the all the data in a table. I think it is that DataBind() function. Where should I place the DataBind() function? Should I enter it as GridView1.DataBind() or DetailsView1.DataBind()?

Thanks

 
Old June 16th, 2008, 04:01 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hard to tell without seeing your code, and without understanding exactly what you are asking.

Can you elaborate?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12:43 PM
JSP file upload and delete file pandjie JSP Basics 0 January 29th, 2005 10:49 PM
Upload file hbcontract XML 2 November 6th, 2003 07:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.