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

January 30th, 2011, 02:43 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 17 problem
Hi Imar,
I am trying to follow along with the Try it out exercises but have hit a problem when trying to let users manage their own photo albums.
After creating a new photo album users on my site are forwarded back to the homepage instead of the managing page.
After a little digging around it seems that the problem is that the 'User.Identity.Name' and 'photoAlbumOwner' dont match when I print the contents of these to a label on screen they show as the same username however the photoAlbumOwner has a trailing space which is making them not compare.
Have you any idea's why this would be happening? I have triple checked the code in the examples and they are the same so can only assume something I put in the previous examples in the book is causing a conflict..?
Thanks in advance
Paul
|
|

January 30th, 2011, 03:07 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Paul,
Do new records get a space as well? If so, there must be an issue still with the code. If only old records have the space, you can remove them from the database, or use Trim to strip them away.
Cheers,
Imar
|
|

January 31st, 2011, 01:59 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Yes it also happens with new signups, looking at the data in the database table aspnet_Users none of the usernames are saved with the trailing space.
Below is the code for my NewPhotoAlbum.aspx page and code behind (C#)
Quote:
<%@ Page Title="Create New Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="_NewPhotoAlbum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Id" DataSourceID="EntityDataSource1" DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
SortExpression="Id" InsertVisible="false"></asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name">
</asp:BoundField>
<asp:CommandField ShowInsertButton="True"></asp:CommandField>
</Fields>
</asp:DetailsView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=PlanetWroxEntities"
DefaultContainerName="PlanetWroxEntities" EnableFlattening="False"
EnableInsert="True" EntitySetName="PhotoAlbums"
oninserted="EntityDataSource1_Inserted"
oninserting="EntityDataSource1_Inserting">
</asp:EntityDataSource>
</asp:Content>
|
Quote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;
public partial class _NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()));
}
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
myPhotoAlbum.UserName = User.Identity.Name;
}
}
|
and my ManagePhotoAlbum.aspx page and code behind
Quote:
<%@ 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"
oniteminserted="ListView1_ItemInserted">
<InsertItemTemplate>
<li style="">
Description:
<asp:RequiredFieldValidator id="reqDesc" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Enter a Description" />
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' TextMode="MultiLine" />
<br />
ToolTip:
<asp:RequiredFieldValidator id="reqTooltip" ControlToValidate="ToolTipTextBox" runat="server" ErrorMessage="Enter a Tooltip" />
<asp:TextBox ID="ToolTipTextBox" runat="server" Text='<%# Bind("ToolTip") %>' />
<asp:FileUpload id="FileUpload1" runat="server"></asp:FileUpload>
<asp:CustomValidator id="cusValImage" runat="server" ErrorMessage="Select a valid .jpg file."></asp:CustomValidator>
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" CausesValidation="false" />
</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 runat="server" ImageUrl='<%# Eval("ImageUrl") %>' ID="ImageUrl"></asp:Image>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" CausesValidation="false" />
</li>
</ItemTemplate>
<LayoutTemplate>
<ul class="ItemContainer">
<li runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>
</asp:ListView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=PlanetWroxEntities"
DefaultContainerName="PlanetWroxEntities" 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>
|
Quote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;
using System.Web.Security;
public partial class _ManagePhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
string photoAlbumOwner = (from p in myEntities.PhotoAlbums
where p.Id == photoAlbumId
select p.UserName).Single();
if (User.Identity.Name != photoAlbumOwner && !User.IsInRole("Managers"))
{
Response.Redirect("~/");
}
}
}
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;
}
}
protected void ListView1_ItemInserted(object sender, ListViewInsertedEventArgs e)
{
}
}
|
Can you see where I might have gone wrong? Thanks for your help.
Paul
|
|

January 31st, 2011, 02:14 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I quickly looked at that and can't see anything wrong. Can you check your database tables and make sure you're using nvarchar datatypes and not nchar types? The latter are filled with trailing spaces
Quote:
|
Yes it also happens with new signups, looking at the data in the database table aspnet_Users none of the usernames are saved with the trailing space.
|
Not exactly sure what you mean here. Do you mean signups or new albums and pictures? Don't want to nitpick; just trying to find out where the error maybe.
BTW: when posting code, please use the Code, not Quote button as it preseves your code better.
Cheers,
Iamr
|
|

January 31st, 2011, 02:32 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
It was the datatype in the PhotoAlbum table that was wrong as you suggested I had set it up with the nchar type! Thank you for your help!
I will make sure I use the code tags to place code next time I run into a problem.
Again thanks for the prompt reply, and the book.
Thanks
Paul
|
|

January 31st, 2011, 02:33 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're welcome and have fun with the remainder of the book.
Cheers,
Imar
|
|
 |
|