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 May 4th, 2012, 11:09 AM
Authorized User
 
Join Date: Apr 2012
Posts: 12
Thanks: 11
Thanked 0 Times in 0 Posts
Unhappy ch: 14, Try it out pg: 515

Completed the try it out, image isnt going to gigpics folder and i am getting following error in chrome:

Could not find a part of the path 'D:\project\WebSite1\virtualFolder\85296d4e-3f86-41d5-b05f-665304372668.jpg'.

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.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\project\WebSite1\virtualFolder\85296d4e-3f86-41d5-b05f-665304372668.jpg'.

Source Error:


Line 24: string fileName = Guid.NewGuid().ToString();
Line 25: string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
Line 26: FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder,fileName+extension));
Line 27: myPicture.ImageUrl = virtualFolder + fileName + extension;
Line 28: }
 
Old May 4th, 2012, 11:14 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you post the rest of the code for the Save action and explain how you set up your site? E.g. where you stored the web site, which folder for the Images you created and so on?

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:
binaryspirit (May 4th, 2012)
 
Old May 4th, 2012, 11:29 AM
Authorized User
 
Join Date: Apr 2012
Posts: 12
Thanks: 11
Thanked 0 Times in 0 Posts
Default

Site Structure image link:

http://img651.imageshack.us/img651/3033/captureqv.png

running website on filesystem

ManagePhotoAlbum.aspx.cs code:

public partial class ManagePhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;

FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
string virtualFolder = "~/GigPics/";
string physicalFolder = Server.MapPath("virtualFolder");
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder,fileName+extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator cusValImage = (CustomValidator) ListView1.InsertItem.FindControl("cusValImage");
cusValImage.IsValid=false;
e.Cancel=true;
}
}
}


ManagePhotoAlbum.aspx :

<%@ Page Title="Manage Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="ManagePhotoAlbum.aspx.cs" Inherits="ManagePhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
DataSourceID="EntityDataSource1" InsertItemPosition="LastItem"
oniteminserting="ListView1_ItemInserting">

<InsertItemTemplate>
<li style="">
Description:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Enter a description"></asp:RequiredFieldValidator>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' TextMode="MultiLine" />
<br />ToolTip:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="ToolTipTextBox" runat="server" ErrorMessage="Enter a Tooltip"></asp:RequiredFieldValidator>
<asp:TextBox ID="ToolTipTextBox" runat="server" Text='<%# Bind("ToolTip") %>' />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="cusValImage" runat="server" ErrorMessage="Select a valid .jpg file"></asp:CustomValidator>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" CausesValidation="false"
Text="Clear" />
</li>
</InsertItemTemplate>

<ItemTemplate>
<li style="">
Description:

<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
ToolTip:

<asp:Label ID="ToolTipLabel" runat="server" Text='<%# Eval("ToolTip") %>' />
<br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" CausesValidation="false"
Text="Delete" />
</li>
</ItemTemplate>
<LayoutTemplate>
<ul class="ItemContainer">
<li runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>

</asp:ListView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=PlanetHarmonyEntities"
DefaultContainerName="PlanetHarmonyEntities" EnableDelete="True"
EnableFlattening="False" EnableInsert="True" EntitySetName="Pictures"
Where="it.PhotoAlbum.Id= @PhotoAlbumId"
oninserting="EntityDataSource1_Inserting">
<WhereParameters>
<asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId"
Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
</asp:Content>
 
Old May 4th, 2012, 11:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at this:

Code:
string physicalFolder = Server.MapPath("virtualFolder");
You're using the literal value "virtualFolder", not the value of that variable. Change it to this and it should work:

Code:
 
string physicalFolder = Server.MapPath(virtualFolder);
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:
binaryspirit (May 4th, 2012)
 
Old May 4th, 2012, 11:49 AM
Authorized User
 
Join Date: Apr 2012
Posts: 12
Thanks: 11
Thanked 0 Times in 0 Posts
Default

thanx a lot, that really a silly mistake.., now its working

how can i make it to accept other image formats, currently i am trying following but it isnt working

if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg") || !FileUpload1.FileName.ToLower().EndsWith(".jpeg"))
{
CustomValidator cusValImage = (CustomValidator) ListView1.InsertItem.FindControl("cusValImage");
cusValImage.IsValid=false;
e.Cancel=true;
}
 
Old May 4th, 2012, 12:32 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 need to "Or" and "And " your logic together. E.g. in pseudo code:

if (no file And (not jpg or not jpeg or not gif)

E.g.

if (!FileUpload1.HasFile && (!FileUpload1.FileName.ToLower().EndsWith(".jpg") || !FileUpload1.FileName.ToLower().EndsWith(".jpeg")) )

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!
The Following User Says Thank You to Imar For This Useful Post:
binaryspirit (May 4th, 2012)
 
Old May 4th, 2012, 12:49 PM
Authorized User
 
Join Date: Apr 2012
Posts: 12
Thanks: 11
Thanked 0 Times in 0 Posts
Default

ok, thats really nice of u. Enjoying your book a lot, its awesome n ur precious support too.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse Error on Chapter 14 pg 523 hozdaman BOOK: Beginning ASP.NET 4 : in C# and VB 3 August 10th, 2011 02:35 PM
Chapter 14 Exercise 3 Solution pg 747 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 2 April 21st, 2011 07:58 PM
chapter 14 Try It Out pg 505 step #4 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 21st, 2011 02:28 PM
Ch 14 Help digink BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 November 3rd, 2009 03:35 PM





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