 |
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 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
|
|
|
|
|

April 10th, 2014, 04:02 PM
|
|
Authorized User
|
|
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 11th, 2014, 03:08 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 13th, 2014, 10:42 AM
|
|
Authorized User
|
|
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 13th, 2014, 10:53 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 13th, 2014, 12:01 PM
|
|
Authorized User
|
|
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 13th, 2014, 02:25 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 22nd, 2014, 04:00 PM
|
|
Authorized User
|
|
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 22nd, 2014, 04:19 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 22nd, 2014, 04:35 PM
|
|
Authorized User
|
|
Join Date: Mar 2014
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 23rd, 2014, 07:12 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
 |
|