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

March 19th, 2011, 01:57 PM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 13,page 462
HI
I'm currently experiencing this problem.The application executes well but it throws me back in the debugger as it shows me this message:Object reference not set to an instance of an object.Any way around this?
|

March 19th, 2011, 02:16 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Can you post the code for the pages NewPhotoAlbum.aspx and ManagePhotoAlbum.aspx and the code behind of these pages? Please paste the code in Notepad first, then copy and paste it here and use the Code formatting button (#) on the toolbar. Otherwise, this forum messes up your code ;-(
Cheers,
Imar
|

March 19th, 2011, 02:48 PM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 13,page 462
Here they are:
ManagePhotoAlbum.aspx
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
DataSourceID="LinqDataSource1" InsertItemPosition="LastItem"
oniteminserting="ListView1_ItemInserting">
<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="ImageUrl" runat="server" ImageUrl='<%# Eval ("ImageUrl") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" CausesValidation="false" />
</li>
</ItemTemplate>
<InsertItemTemplate>
<li style="">
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine"
Text='<%# Bind("Description") %>' />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Please enter a description"></asp:RequiredFieldValidator>
Tooltip:
<asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="ToolTipTextBox" runat="server" ErrorMessage="Please enter a tooltip"></asp:RequiredFieldValidator>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please 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"
Text="Clear" CausesValidation="false" />
</li>
</InsertItemTemplate>
<LayoutTemplate>
<ul class="itemContainer">
<li ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Cell_WareDataContext" EnableDelete="True" EnableInsert="True"
TableName="Pictures" Where="PhotoAlbumId == @PhotoAlbumId"
oninserting="LinqDataSource1_Inserting">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="-1" Name="PhotoAlbumId"
QueryStringField="PhotoAlbumId" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
Code-Behind of ManagePhotoAlbum.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ManagePhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
string virtualFolder = "~/Cell-Pics/";
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("Find Upload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator CustomValidator1 =
(CustomValidator)ListView1.InsertItem.FindControl( "CustomValidator1");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
}
NewPhotoAlbum.aspx
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Id" DataSourceID="LinqDataSource1" DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Cell_WareDataContext" EnableInsert="True"
oninserted="LinqDataSource1_Inserted" TableName="PhotoAlbums">
</asp:LinqDataSource>
CodeBehind of NewPhotoAlbum.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()));
}
}
|

March 19th, 2011, 03:19 PM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 13,page 462
Here they are:
ManagePhotoAlbum.aspx
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
DataSourceID="LinqDataSource1" InsertItemPosition="LastItem"
oniteminserting="ListView1_ItemInserting">
<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="ImageUrl" runat="server" ImageUrl='<%# Eval ("ImageUrl") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" CausesValidation="false" />
</li>
</ItemTemplate>
<InsertItemTemplate>
<li style="">
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine"
Text='<%# Bind("Description") %>' />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Please enter a description"></asp:RequiredFieldValidator>
Tooltip:
<asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="ToolTipTextBox" runat="server" ErrorMessage="Please enter a tooltip"></asp:RequiredFieldValidator>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please 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"
Text="Clear" CausesValidation="false" />
</li>
</InsertItemTemplate>
<LayoutTemplate>
<ul class="itemContainer">
<li ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Cell_WareDataContext" EnableDelete="True" EnableInsert="True"
TableName="Pictures" Where="PhotoAlbumId == @PhotoAlbumId"
oninserting="LinqDataSource1_Inserting">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="-1" Name="PhotoAlbumId"
QueryStringField="PhotoAlbumId" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
Code-Behind of ManagePhotoAlbum.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ManagePhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
string virtualFolder = "~/Cell-Pics/";
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("Find Upload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator CustomValidator1 =
(CustomValidator)ListView1.InsertItem.FindControl( "CustomValidator1");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
}
NewPhotoAlbum.aspx
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Id" DataSourceID="LinqDataSource1" DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Cell_WareDataContext" EnableInsert="True"
oninserted="LinqDataSource1_Inserted" TableName="PhotoAlbums">
</asp:LinqDataSource>
CodeBehind of NewPhotoAlbum.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()));
}
}
|

March 20th, 2011, 08:08 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
When you post code in the future, can you please follow my advise and wrap the code in code tags use the Code button (#) on the toolbar? Your current code is hard to read, and contains a number of issues / errors due to a formatting bug in this fourm (extra spaces get added for example).
Anyway, I think the problem is in this code:
Code:
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FindUpload1");
The control is called FileUpload1, not FindUpload1.
Cheers,
Imar
|

March 21st, 2011, 08:42 AM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code:
Yes thanks for that information.Anyway regarding the project this line of code:FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, filename + extension));
generates this exception:Could not find a part of the path 'C:\Users\Masego\Documents\Visual Studio 2008\WebSites\Cell-Ware\Cell-Pics\fa6466b0-b14d-4100-aa3d-2c3e77abfc35.jpg'.When there are already pics in the folder,what could be the problem?
|

March 21st, 2011, 08:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It could be a permissions issue. Does the site run under IIS? If so, it doesn't have access to your User folder. That's the reason the site is placed in a folder such as C:\BegASPNET. Obviously, other locations like C:\Inetpub should work as well, as long as the IIS account has access.
Cheers,
Imar
|

March 21st, 2011, 09:40 AM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code:
Ok.How do go about checking if it does run on an IIS?
|

March 21st, 2011, 09:43 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What do you see in the browser's Address bar? And how did you set up and start the site?
Either way, it's probably a good idea to move the site out of your Documents folder, and configure security settings as explained in the book.
Cheers,
Imar
|

March 22nd, 2011, 02:34 PM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well this is what I see in the address bar: http://localhost:56805/Cell-Ware/Man...toAlbum.aspx.I set up the site according to the book chapter 2 page 37 unless I did something otherwise.
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Chapter 13,page 440. |
Masego.Moikanyang |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 |
6 |
March 17th, 2011 10:45 AM |
Chapter 13, Page 465 Try it Out |
dstreeter |
BOOK: Beginning ASP.NET 4 : in C# and VB |
3 |
November 7th, 2010 10:41 AM |
page 456, chapter 13. |
leemark2k3 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 |
8 |
September 21st, 2010 08:21 PM |
|
 |