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

September 2nd, 2012, 04:46 PM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Chapter 13: TryItOut-Customizing Detail View --- not inserting new data?
Chapter 13 is killing me. I took a couple of days off, but have come back and can't find the problem, even when I take the source code of addeditreview.aspx and paste it into my project. I have also checked the cs code, but will go back and check my syntax. The problem: It is not inserting new record reviews. I believe there is a problem with setting up a genreID --- I say this because I get all the way through the steps, and I get a data entry page much like you see in step 17 EXCEPT for the fact that there is no "ID" field to assign an ID. My table / data entry field starts off with the "Title" field and doesn't allow me to fill in an ID. Here is the code:
Code:
<%@ Page Title="Planet Wrox - Management - Insert and Update Reviews" Language="C#" MasterPageFile="~/MasterPages/Management.master" AutoEventWireup="true" CodeFile="AddEditReview.aspx.cs" Inherits="Management_AddEditReview" %>
<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="SqlDataSource1" DefaultMode="Insert"
Height="50px" onpageindexchanging="DetailsView1_PageIndexChanging"
Width="125px" oniteminserted="DetailsView1_ItemInserted"
oniteminserting="DetailsView1_ItemInserting"
onitemupdated="DetailsView1_ItemUpdated"
onitemupdating="DetailsView1_ItemUpdating">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
ReadOnly="True" SortExpression="Id" />
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Enter a Title" ControlToValidate="TextBox1"
runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ErrorMessage="Enter a Title" ControlToValidate="TextBox1"
runat="server" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary" SortExpression="Summary">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ErrorMessage="Enter a Summary" ControlToValidate="TextBox2"
runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" ErrorMessage="Enter a Summary" ControlToValidate="TextBox2"
runat="server" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Body" SortExpression="Body">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Body") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GenreId" SortExpression="GenreId">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="GenresDataSource" DataTextField="Name" DataValueField="Id"
SelectedValue='<%# Bind("GenreId") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="GenresDataSource" DataTextField="Name" DataValueField="Id"
SelectedValue='<%# Bind("GenreId") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("GenreId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="Authorized" HeaderText="Authorized"
SortExpression="Authorized" />
<asp:BoundField DataField="UpdateDateTime" HeaderText="UpdateDateTime"
SortExpression="UpdateDateTime" Visible="False" />
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>"
DeleteCommand="DELETE FROM [Review] WHERE [Id] = @Id"
InsertCommand="INSERT INTO [Review] ([Title], [Summary], [Body], [GenreId], [Authorized], [UpdateDateTime]) VALUES (@Title, @Summary, @Body, @GenreId, @Authorized, @UpdateDateTime)"
SelectCommand="SELECT [Id], [Title], [Summary], [Body], [GenreId], [Authorized], [UpdateDateTime] FROM [Review] WHERE ([Id] = @Id)"
UpdateCommand="UPDATE [Review] SET [Title] = @Title, [Summary] = @Summary, [Body] = @Body, [GenreId] = @GenreId, [Authorized] = @Authorized, [UpdateDateTime] = @UpdateDateTime WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
<asp:Parameter Name="Body" Type="String" />
<asp:Parameter Name="GenreId" Type="Int32" />
<asp:Parameter Name="Authorized" Type="Boolean" />
<asp:Parameter Name="UpdateDateTime" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
<asp:Parameter Name="Body" Type="String" />
<asp:Parameter Name="GenreId" Type="Int32" />
<asp:Parameter Name="Authorized" Type="Boolean" />
<asp:Parameter Name="UpdateDateTime" Type="String" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="GenresDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>"
SelectCommand="SELECT [Id], [Name] FROM [Genre] ORDER BY [SortOrder]">
</asp:SqlDataSource>
</asp:Content>
|
|

September 2nd, 2012, 04:57 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
My table / data entry field starts off with the "Title" field and doesn't allow me to fill in an ID. Here is the code:
|
I am not sure what you mean with this. There's no id text box, as that's what the drop down list is for.
Can you post the code behind? Also, is your database set up correctly?
Imar
|
|

September 2nd, 2012, 05:29 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
BTW: I just copied your markup into my project, and it runs fine and inserts the review.
So the problem is likely in the Code Behind or in your database serup.
Cheers,
Imar
|
|

September 3rd, 2012, 12:50 PM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Thanks as always for your prompt response. See CS script below:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Management_AddEditReview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.Get("Id") !=null)
{
DetailsView1.DefaultMode = DetailsViewMode.Edit;
}
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
EndEditing();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["UpdateDateTime"] = DateTime.Now;
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
EndEditing();
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
e.NewValues["UpdateDateTime"] = DateTime.Now;
}
private void EndEditing()
{
Response.Redirect("Reviews.aspx");
}
}
|
|

September 3rd, 2012, 04:16 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
That looks fine to me.
Did you check the database? Did you create the proper Id, identity and primary key columns and set up the correct relationships?
Also, try commenting out the call to Response.Redirect. It may reveal the real error...
Code:
private void EndEditing()
{
// Response.Redirect("Reviews.aspx");
}
Imar
Last edited by Imar; September 3rd, 2012 at 04:22 PM..
Reason: Forgot to comment out the code
|
|

September 3rd, 2012, 09:36 PM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Regarding Chapter 13
I checked the database and it appeared to be okay. But I will check again. I am finding more often than not there's some small error in code. I will let you know in a couple of days --- I may not be getting back to this point in the chapter due to work conflicts. But I have started reading on chapter 14.
w!
|
|

September 4th, 2012, 09:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
I am finding more often than not there's some small error in code.
|
In your code, or in mine? To my knowledge, the code in the book works as expected. That's not say an error is impossible of course.
Usually, this problem is caused by incorrect keys or relationships in the database. Commenting out the redirect should reveal the database error, if any. Also, check your database and see if any records are inserted at all; they may end up linked to the wrong genre.
Cheers,
Imar
|
|

September 4th, 2012, 08:26 PM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Again, sorry for any confusion. I was referring to MY code!
I look forward to returning to this project soon. One of the reasons for buying your text is that I have been saddled with the sad responsibility of content management for complex .net 4.0 site built for us. Your text is helping me understand the fundamentals of the back-end code.
|
|
 |
|