Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 10th, 2014, 04:02 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
Default Can I upload a PDF file on AddEditReview.aspx page?

Hi Imar,

I don't really want to bother you, but I hope this is my last question and this is very important for me!

When you insert a new review in the AddEditReview.aspx page, can I upload together with the review a PDF file?

And then, for example when a user clicks a review, inside it display the pdf file together with the review and be able to download that pdf file?

I'm working on a project and the logic is the same as you did with the two tables [Genre] and [Review].

Thanks.
Reply With Quote
  #2 (permalink)  
Old April 11th, 2014, 03:08 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You should find all the information you need in the sections that build the Photo Album feature. It talks about uploading files, storing them on disk, linking them to a database record and displaying them at run time.

The only difference is ts that you cannot display a PDF inline as you can with an image. You could place a link to the PDF file in an iframe, or provide a download link to the file instead.

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!
Reply With Quote
  #3 (permalink)  
Old April 13th, 2014, 10:42 AM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Please if you can look here is my Data Model: http://postimg.org/image/svxi0x4xb/

Now, on AddEditReview.aspx you used a <asp:DetailsView to Bind() Title, Summary, Body.

I'm doing it in the same way using <asp:TemplateField />, but now for the PDFUrl I'm trying like this:

Code:
<asp:TemplateField HeaderText="PDF" SortExpression="PDFUrl">
                
                <InsertItemTemplate>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <br />
                    <asp:CustomValidator ID="cusValPDF" runat="server" ErrorMessage="Select .PDF file." CssClass="ErrorMessage" />
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="PDFLabel" runat="server" Text='<%# Item.PDFUrl %>' />
                </ItemTemplate>
            </asp:TemplateField>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=... InsertCommand="INSERT INTO [Review].... ([PDFUrl])..../>

<InsertParameters>
...
     <asp:Parameter Name="PDFUrl" Type="String" />
</InsertParameters>

<SelectParameters>
            <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
</SelectParameters>

<UpdateParameters>
...
            <asp:Parameter Name="PDFUrl" Type="String" />
</UpdateParameters>
On Code Behind:

Code:
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values["UpdateDateTime"] = DateTime.Now;
        
        FileUpload FileUpload1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
        if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".pdf"))
        {
            CustomValidator cusValImage = (CustomValidator)DetailsView1.FindControl("cusValPDF");
            cusValImage.IsValid = false;
            e.Cancel = true;
        }
    }

protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        int genreId = Convert.ToInt32(Request.QueryString.Get("GenreId"));
        
        Review filePDF = (Review)e.Entity;  // here I cannot access the e.Entity
        filePDF.GenreId= genreId;              // because I'm using SqlDataSource instead EntityDataSource
                                            

        FileUpload FileUpload1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
        string virtualFolder = "~/PDF/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
        filePDF.PDFUrl= virtualFolder + fileName + extension;
    }
When I insert a new review, I need to upload the PDF file together with the review on the management section. Then, in the client side, if a user opens a review, example: /Reviews/ViewDetails.aspx?ReviewId=8, the PDF file should be present as a hyperlink where he/she can download or view it on browser.

Thanks for help Imar.
Reply With Quote
  #4 (permalink)  
Old April 13th, 2014, 10:53 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

And which part do you need help with? Did you take a look at the PhotoAlbum section? It covers this exactly, except it's dealing with images.

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!
Reply With Quote
  #5 (permalink)  
Old April 13th, 2014, 12:01 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I'm using a <asp:DetailsView /> to get the pdf file from the Uploads folder like this and it works:

Code:
<asp:DetailsView ID="DetailsView1" runat="server">
                <Fields>
                    <asp:TemplateField HeaderText="PDF" SortExpression="PDFUrl">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# String.Format("../Uploads/{0}.pdf", Eval("Id").ToString()) %>' Text="View in Browser" Target="_blank"></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView>
The only problem now I have is that on SqlDataSource1_Inserting I cannot access the e.Entity:

Code:
Review filePDF = (Review)e.Entity;
          filePDF.GenreId= genreId;
Thanks.
Reply With Quote
  #6 (permalink)  
Old April 13th, 2014, 02:25 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

The SqlDataSource doesn't work with entities. However, the e argument of the Inserting event has a NewValues collection that contains the fields you are looking for.

Hope this helps,

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!
Reply With Quote
  #7 (permalink)  
Old April 22nd, 2014, 04:00 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for help Imar,

I'm trying as you said:

Code:
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values["UpdateDateTime"] = DateTime.Now;
        
        // Insert book
        FileUpload FileUpload1 = (FileUpload)DetailsView1.FindControl("FileUpload1");
        string virtualFolder = "~/PDF/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
        
        // Upload file:
        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));

        //Assign url to db
        e.Values["PDFUrl"] = virtualFolder + fileName + extension;

        // Validate book's type
        if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".pdf"))
        {
            CustomValidator CustomValidator1 = (CustomValidator)DetailsView1.FindControl("CustomValidator1");
            CustomValidator1.IsValid = false;
            e.Cancel = true;
        }
    }
Now the book is inserted in the PDF folder, but then I'm getting an error:
String or binary data would be truncated. The statement has been terminated.

I added in the Review table a new column called PDFUrl with nvarchar(200) not null, then I increased the size to nvarchar(max) not null, but again it's not working.

Thanks.
Reply With Quote
  #8 (permalink)  
Old April 22nd, 2014, 04:19 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Did you update / refresh the EF model after you changed the size of the column? With that change, I see no reason why this doesn't work. Are you sure it crashed on this new column?

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!
Reply With Quote
  #9 (permalink)  
Old April 22nd, 2014, 04:35 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Yes I updated the EF model. Without the PDFUrl column I can insert reviews normally without errors.

Here is the full error:

String or binary data would be truncated.
The statement has been terminated.

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.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1789294
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340642
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +275
System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +208
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163
System.Web.UI.WebControls.SqlDataSourceView.Execut eDbCommand(DbCommand command, DataSourceOperation operation) +378
System.Web.UI.WebControls.SqlDataSourceView.Execut eInsert(IDictionary values) +399
System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +81
System.Web.UI.WebControls.DetailsView.HandleInsert (String commandArg, Boolean causesValidation) +365
System.Web.UI.WebControls.DetailsView.HandleEvent( EventArgs e, Boolean causesValidation, String validationGroup) +584
System.Web.UI.WebControls.DetailsView.OnBubbleEven t(Object source, EventArgs e) +89
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DetailsViewRow.OnBubbleE vent(Object source, EventArgs e) +80
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.Button.OnCommand(Command EventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +159
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Reply With Quote
  #10 (permalink)  
Old April 23rd, 2014, 07:12 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Oh, I see now you're not using Entity Framework for this.

Are you absolutely sure you updated the correct table and column, in the correct database? I see not reason why this shouldn't work.

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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pages 489-490 - AddEditReview.aspx Antonius Block BOOK: Beginning ASP.NET 4.5 : in C# and VB 3 January 13th, 2014 05:50 PM
Help needed for Modification to AddEditReview.aspx blueberret BOOK: Beginning ASP.NET 4 : in C# and VB 1 December 30th, 2011 06:49 AM
Chapter 12 AddEditReview.aspx dgjim BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 23rd, 2010 12:01 PM
open pdf file within aspx page shaktis General .NET 3 November 2nd, 2007 12:58 PM
upload doc,pdf file from ORACLE 9i on JSP page ashwinmittal Pro JSP 1 November 30th, 2004 01:18 PM



All times are GMT -4. The time now is 01:27 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.