Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 6th, 2011, 11:24 AM
Registered User
 
Join Date: Jun 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default Upload Image and Insert New Record

Okay, in ListView's Insert Item Template I have managed to get it to insert only the information that I need - now I want to incorporate a file upload in the same Insert Item Template - basically want to upload a file and add that files information plus the new record information to the database in one click - getting this error...Object reference not set to an instance of an object.

I am tinkering around with the code but I can upload what I have if that would help.

Any assistance is greatly appreciated. This is so confusing.
 
Old June 6th, 2011, 04:52 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,

That error is really common and can be caused by many different factors. So, it's impossible to say what's going on without seeing you code.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
ken71683 (June 7th, 2011)
 
Old June 7th, 2011, 07:42 AM
Registered User
 
Join Date: Jun 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default Here is my code...

Code:
        <InsertItemTemplate>

            Click Browse to Upload File.<br />

            <asp:FileUpload ID="FileField" runat="server" size="60" /><br />
            <asp:Label runat="server" ID="FileWrite" />

            Title:
            <asp:TextBox ID="FileNameTextBox" runat="server" Text='<%# Bind("FileName") %>' /><br />
            
            Description:
            <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />


            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
            
        </InsertItemTemplate>



Code:
Protected Sub ListView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles ListView1.ItemInserting


            Dim FileField As FileUpload = CType(ListView1.FindControl("FileField"), FileUpload)
            Dim virtualFolder As String = "~/Documents/"
            Dim physicalFolder As String = Server.MapPath(virtualFolder)
            Dim fileName As String = Guid.NewGuid().ToString()
            Dim extension As String = System.IO.Path.GetExtension(FileField.FileName)

            'Upload file:
            FileField.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
            'Assign url to db:
            e.Values("DocumentURL") = virtualFolder + fileName + extension

            e.Values("UploadDate") = DateTime.Now
            e.Values("UserName") = Membership.GetUser().UserName
            e.Values("DownloadCount") = 0
            e.Values("FileType") = "DOC"


  End Sub
 
Old June 7th, 2011, 02:17 PM
Registered User
 
Join Date: Jun 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default Fileupload lost on postback

I've been reading some forums and it is saying that the fileUpload field is getting dumped in the insert item template because of postback? I'm not entirely clear of what that means - I can upload any file by clicking a standard button control but when I add the fileupload option to the item insert template it fails.

I want the upload file/browse box followed by a Title, Description textbox then "Insert" to be clicked - when I do that it fails and the Upload File box is dumped.

I'm thinking I'm really close to knocking this out - I'm just missing something somewhere.
 
Old June 7th, 2011, 03:28 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Did you try FindControl on the InsertItem? E.g. ListView1.InsertItem.FindControl?

And do you what line causes the null reference exception?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 7th, 2011, 03:37 PM
Registered User
 
Join Date: Jun 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default Here is code again....

I think I'm putting everything in correctly I've read a few other forums that say postback is making it lose the fileUpload capability...

Code:
Protected Sub ListView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles ListView1.ItemInserting

Dim FileField As FileUpload = ListView1.FindControl("FileField")


            Dim savePath As String = "C:\Inetpub\wwwroot\DotNet_Projects\MyClarkTireResources\Management\Documents\"

            If (FileField.HasFile) Then


                Dim fileName As String = FileField.FileName

                savePath += fileName

                FileField.SaveAs(savePath)

                Label1.Text = "Your File Was uploaded as " + fileName

                Dim virtualFolder As String = "~/Documents/"
                Dim physicalFolder As String = Server.MapPath(virtualFolder)
                Dim fileNameExt As String = Guid.NewGuid().ToString()
                Dim extension As String = System.IO.Path.GetExtension(FileField.FileName)

                'Upload(file)
                FileField.SaveAs(System.IO.Path.Combine(physicalFolder, fileNameExt + extension))

                'Assign url to db:
                e.Values("DocumentURL") = virtualFolder + fileNameExt + extension

                e.Values("DocumentSize") = 100
                e.Values("UploadDate") = DateTime.Now
                e.Values("UserName") = Membership.GetUser().UserName
                e.Values("DownloadCount") = 0
                e.Values("FileType") = "DOC"


            Else

                Label1.Text = "Trash"

           End If
  End Sub
Code:
        <InsertItemTemplate>

            <div id="InfoContainer">
            Click Browse to Upload File.<br />
                <asp:FileUpload ID="FileField" runat="server" size="60" /><br />
            </div>

            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

            Title:
            <asp:TextBox ID="FileNameTextBox" runat="server" Text='<%# Bind("Title") %>' /><br />
            
            Description:
            <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />


            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
  
        </InsertItemTemplate>




Error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 68: 'Dim UploadDetails As Label
Line 69:
Line 70: FileName.InnerHtml = FileField.PostedFile.FileName <==HERE IS WHERE IT IS RED ################
Line 71: FileContent.InnerHtml = FileField.PostedFile.ContentType
Line 72: FileSize.InnerHtml = FileField.PostedFile.ContentLength
 
Old June 7th, 2011, 04:26 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can't upload a file like this without a postback so it should work. Maybe you are referring to issues with an UpdatePanel?

Did you see my previous post? And what happens when you debug the app? Is the control null, or the PostedFile property?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload image-create & save thumbnail-display image angshujit ASP.NET 2.0 Professional 6 July 11th, 2013 10:34 PM
How to: Upload image to server and save image name in db with other form info Clint PHP How-To 1 October 26th, 2009 05:50 AM
how to upload image? vipin k varghese BOOK: XSLT Programmer's Reference, 2nd Edition 3 June 3rd, 2008 02:53 AM
Upload image phuc2583 Classic ASP Basics 1 January 17th, 2008 08:48 AM
Image upload cutesneakers PHP How-To 2 January 5th, 2005 08:00 AM





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