Wrox Programmer Forums
|
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
 
Old March 18th, 2015, 03:58 PM
Registered User
 
Join Date: Mar 2015
Posts: 5
Thanks: 0
Thanked 2 Times in 2 Posts
Default Chapter 13: Detailview doesn't Update.

When I insert a review into the database, it works, but when I try to update, it doesn't. There is no error message, it just returns to the review page.

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Management.master" AutoEventWireup="true" CodeFile="AddEditReviewold.aspx.cs" Inherits="Management_AddEditReview" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainSection" Runat="Server">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted" OnItemInserting="DetailsView1_ItemInserting" OnItemUpdated="DetailsView1_ItemUpdated" OnItemUpdating="DetailsView1_ItemUpdating">
<Fields>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter a Title." ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter a Title." ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</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="TextBox3" runat="server" Text='<%# Bind("Summary") %>' TextMode="MultiLine" Width="500px" Height="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Enter a Summary." ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Summary") %>' TextMode="MultiLine" Width="500px" Height="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Enter a Summary." ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Body" SortExpression="Body">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Body") %>' TextMode="MultiLine" Width="500px" Height="100px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Body") %>' TextMode="MultiLine" Width="500px" Height="100px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Body") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="Authorized" HeaderText="Authorized" SortExpression="Authorized" />
<asp:TemplateField HeaderText="GenreID" SortExpression="GenreID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="GenreDataSource" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("GenreID") %>'></asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="GenreDataSource" 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:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" InsertVisible="False" ReadOnly="True" />
<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" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" DeleteCommand="DELETE FROM [Review] WHERE [Id] = @original_Id AND [Authorized] = @original_Authorized AND [GenreID] = @original_GenreID AND (([Body] = @original_Body) OR ([Body] IS NULL AND @original_Body IS NULL)) AND [Summary] = @original_Summary AND [Title] = @original_Title AND [UpdateDateTime] = @original_UpdateDateTime" InsertCommand="INSERT INTO [Review] ([Authorized], [GenreID], [Body], [Summary], [Title], [UpdateDateTime]) VALUES (@Authorized, @GenreID, @Body, @Summary, @Title, @UpdateDateTime)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Authorized], [GenreID], [Body], [Summary], [Title], [Id], [UpdateDateTime] FROM [Review] WHERE ([Id] = @Id)" UpdateCommand="UPDATE [Review] SET [Authorized] = @Authorized, [GenreID] = @GenreID, [Body] = @Body, [Summary] = @Summary, [Title] = @Title, [UpdateDateTime] = @UpdateDateTime WHERE [Id] = @original_Id AND [Authorized] = @original_Authorized AND [GenreID] = @original_GenreID AND (([Body] = @original_Body) OR ([Body] IS NULL AND @original_Body IS NULL)) AND [Summary] = @original_Summary AND [Title] = @original_Title AND [UpdateDateTime] = @original_UpdateDateTime">
<DeleteParameters>
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_Authorized" Type="Boolean" />
<asp:Parameter Name="original_GenreID" Type="Int32" />
<asp:Parameter Name="original_Body" Type="String" />
<asp:Parameter Name="original_Summary" Type="String" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_UpdateDateTime" Type="DateTime" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Authorized" Type="Boolean" />
<asp:Parameter Name="GenreID" Type="Int32" />
<asp:Parameter Name="Body" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="UpdateDateTime" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Authorized" Type="Boolean" />
<asp:Parameter Name="GenreID" Type="Int32" />
<asp:Parameter Name="Body" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="UpdateDateTime" Type="DateTime" />
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_Authorized" Type="Boolean" />
<asp:Parameter Name="original_GenreID" Type="Int32" />
<asp:Parameter Name="original_Body" Type="String" />
<asp:Parameter Name="original_Summary" Type="String" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_UpdateDateTime" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="GenreDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" SelectCommand="SELECT [Id], [Name] FROM [Genre] ORDER BY [SortOrder]"></asp:SqlDataSource>
</asp:Content>









CODE BEHIND FILE
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;
}
}
private void EndEditing()
{
Response.Redirect("Review.aspx");
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
if (e.Exception == null)
EndEditing();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["UpdateDateTime"] = DateTime.Now;
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
if (e.Exception == null)
{
EndEditing();
}
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
e.NewValues["UpdateDateTime"] = DateTime.Now;
}
}
 
Old March 18th, 2015, 04:23 PM
Registered User
 
Join Date: Mar 2015
Posts: 5
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I am able to update the information in the server explorer manually.
 
Old March 20th, 2015, 11:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

When you debug the code, do you see the various events being fired?

If that doesn't help, can you repost your code and wrap it in code tags (using the Code button with the # symbol on the text editor's toolbar)? Then I can compare it with the code from the book.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
DetailView Update not working. scott100 ASP.NET 3.5 Basics 6 March 30th, 2013 09:47 AM
How to update the gridview through a detailview fh84 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 December 18th, 2008 08:14 PM
Rolling headers (13 months) how to update records Axxess Access VBA 0 May 22nd, 2008 06:24 AM
Detailview insert tombert ASP.NET 2.0 Basics 1 April 26th, 2007 05:00 AM
DetailView Control Showkkath ASP.NET 2.0 Professional 1 November 23rd, 2005 02:48 PM





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