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 June 16th, 2011, 11:18 AM
Registered User
 
Join Date: Jun 2011
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Help Again please

Hi Imar,

Im sorry about writing again, Im having a really bad week! But I have now got stuck on page 414 - I have tried the 'Try It Out' twice now but it wont add any new records. If I update a record it works, but I cant seem to add any records in the AddEditReview.aspx page. I have included the markup and code behind for the page

I would be very grateful for any help or advice.

Thanks!

Code:
<%@ Page Title="Planet Wrox - Management - Insert and Update Reviews" Language="C#" MasterPageFile="~/MasterPages/ManagementMaster.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="ContentPlaceHolder1" 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:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Id" />
            <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="ReqVal1" ControlToValidate="TextBox2" runat="server" ErrorMessage="Enter a summary"></asp:RequiredFieldValidator></EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2"  TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal2" ControlToValidate="TextBox2" runat="server" ErrorMessage="Enter a summary"></asp:RequiredFieldValidator></InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Title" SortExpression="Title">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal3" ControlToValidate="TextBox1" runat="server" ErrorMessage="Enter a title"></asp:RequiredFieldValidator></EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal4" ControlToValidate="TextBox1" runat="server" ErrorMessage="Enter a title"></asp:RequiredFieldValidator></InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Body" SortExpression="Body">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal5" ControlToValidate="TextBox3" runat="server" ErrorMessage="Enter body text"></asp:RequiredFieldValidator></EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal6" ControlToValidate="TextBox3" runat="server" ErrorMessage="Enter body text"></asp:RequiredFieldValidator></InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Body") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GenreId" SortExpression="GenreId">
                <EditItemTemplate>
                    <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsGenres" 
                        DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("GenreId") %>'>
                </asp:DropDownList>
                </InsertItemTemplate></EditItemTemplate>
                <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsGenres" 
                        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="UpdateDateTime" HeaderText="UpdateDateTime" 
                SortExpression="UpdateDateTime" Visible="False" />
            <asp:CheckBoxField DataField="Authorized" HeaderText="Authorized" 
                SortExpression="Authorized" />
            <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] ([Summary], [Title], [Body], [GenreId], [UpdateDateTime], [Authorized]) VALUES (@Summary, @Title, @Body, @GenreId, @UpdateDateTime, @Authorized)" 
        SelectCommand="SELECT [Id], [Summary], [Title], [Body], [GenreId], [UpdateDateTime], [Authorized] FROM [Review] WHERE ([Id] = @Id)" 
        UpdateCommand="UPDATE [Review] SET [Summary] = @Summary, [Title] = @Title, [Body] = @Body, [GenreId] = @GenreId, [UpdateDateTime] = @UpdateDateTime, [Authorized] = @Authorized WHERE [Id] = @Id">
        <SelectParameters>
            <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Summary" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Body" Type="String" />
            <asp:Parameter Name="GenreId" Type="Int32" />
            <asp:Parameter Name="UpdateDateTime" Type="DateTime" />
            <asp:Parameter Name="Authorized" Type="Boolean" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Summary" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Body" Type="String" />
            <asp:Parameter Name="GenreId" Type="Int32" />
            <asp:Parameter Name="UpdateDateTime" Type="DateTime" />
            <asp:Parameter Name="Authorized" Type="Boolean" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsGenres" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" 
        SelectCommand="SELECT [Id], [Name] FROM [Genre] ORDER BY [SortOrder]">
    </asp:SqlDataSource>
    <p>
    </p>
</asp:Content>
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_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        EndEditing();
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values["UpdateTimeNow"] = 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");
    
    }
}
 
Old June 16th, 2011, 11:55 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Do you get an error when you run this code? And if so, which one?

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!
 
Old June 16th, 2011, 12:30 PM
Registered User
 
Join Date: Jun 2011
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Im sorry, I did not check the error list because the page was compiling fine.. but I just had a look at the error message and it says:

---
Element 'DropDownList' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.
---

I do have a web.config file on my project so it shouldnt be missing.

Thanks!
 
Old June 16th, 2011, 06:14 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at this:

Code:
 
<EditItemTemplate>
  <InsertItemTemplate>
  <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsGenres" 
      DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("GenreId ") %>'>
  </asp:DropDownList>
  </InsertItemTemplate>
</EditItemTemplate>
You nested the InsertItemTemplate inside the EditItemTemplate which is not allowed and won't work.

The error message is a bit confusing as it points you to the DDL,but it's really the templates that are causing the problem.

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!
 
Old June 17th, 2011, 10:59 AM
Registered User
 
Join Date: Jun 2011
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for your help in the previous post. I no longer get any error messages when I compile the page...although it still does not add a new record. I cant understand it, I have looked at the code so many times and I just cant see why it wont work. I thought at first it might be to do with the SQL server, but it does UPDATE records so it cant be anything to do with that. Any other suggestions? Thanks for all your help!


Code:
<%@ Page Title="Planet Wrox - Management - Insert & Update Reviews" Language="C#" MasterPageFile="~/MasterPages/ManagementMaster.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="ContentPlaceHolder1" 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:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Id" />
            <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="ReqVal1" ControlToValidate="TextBox2" runat="server" ErrorMessage="Enter a summary"></asp:RequiredFieldValidator></EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2"  TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal2" ControlToValidate="TextBox2" runat="server" ErrorMessage="Enter a summary"></asp:RequiredFieldValidator></InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Title" SortExpression="Title">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal3" ControlToValidate="TextBox1" runat="server" ErrorMessage="Enter a title"></asp:RequiredFieldValidator></EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal4" ControlToValidate="TextBox1" runat="server" ErrorMessage="Enter a title"></asp:RequiredFieldValidator></InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Body" SortExpression="Body">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal5" ControlToValidate="TextBox3" runat="server" ErrorMessage="Enter body text"></asp:RequiredFieldValidator></EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal6" ControlToValidate="TextBox3" runat="server" ErrorMessage="Enter body text"></asp:RequiredFieldValidator></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="sdsGenres" 
                        DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("GenreId") %>'>
                </asp:DropDownList>
                </EditItemTemplate>
               
                <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsGenres" 
                        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="UpdateDateTime" HeaderText="UpdateDateTime" 
                SortExpression="UpdateDateTime" Visible="False" />
            <asp:CheckBoxField DataField="Authorized" HeaderText="Authorized" 
                SortExpression="Authorized" />
            <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] ([Summary], [Title], [Body], [GenreId], [UpdateDateTime], [Authorized]) VALUES (@Summary, @Title, @Body, @GenreId, @UpdateDateTime, @Authorized)" 
        SelectCommand="SELECT [Id], [Summary], [Title], [Body], [GenreId], [UpdateDateTime], [Authorized] FROM [Review] WHERE ([Id] = @Id)" 
        UpdateCommand="UPDATE [Review] SET [Summary] = @Summary, [Title] = @Title, [Body] = @Body, [GenreId] = @GenreId, [UpdateDateTime] = @UpdateDateTime, [Authorized] = @Authorized WHERE [Id] = @Id">
        <SelectParameters>
            <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Summary" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Body" Type="String" />
            <asp:Parameter Name="GenreId" Type="Int32" />
            <asp:Parameter Name="UpdateDateTime" Type="DateTime" />
            <asp:Parameter Name="Authorized" Type="Boolean" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Summary" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Body" Type="String" />
            <asp:Parameter Name="GenreId" Type="Int32" />
            <asp:Parameter Name="UpdateDateTime" Type="DateTime" />
            <asp:Parameter Name="Authorized" Type="Boolean" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsGenres" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" 
        SelectCommand="SELECT [Id], [Name] FROM [Genre] ORDER BY [SortOrder]">
    </asp:SqlDataSource>
    <p>
    </p>
</asp:Content>
 
Old June 17th, 2011, 11:02 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 define "won't work"? Do you get an error? How is the page behaving? What steps do you follow to test out the Insert behavior and so on...

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!
 
Old June 17th, 2011, 12:02 PM
Registered User
 
Join Date: Jun 2011
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Imar,

The page compiles ok when I press F5 (no error messages).. >> and I fill in all the required fields on the AddEditReview.aspx page >> when I press 'Insert' the page will go to Reviews.aspx >> I use the drop-down menu to look for the genre I inserted my record as i.e. 'rock music', but nothing.. just the couple of records I already had there from previous exercises. Its strange because if I click on one of the exisiting records and change (i.e. the summary) it works, but I just cant add a new record. There is no error message on the reviews.aspx page - nor any other page. argghhh!

Thanks for your help!
 
Old June 17th, 2011, 12:03 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Did you also check the database to see of the record is there?

I'll try your code in my sample site later this weekend and see if I can find anything....
__________________
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!
 
Old June 17th, 2011, 03:36 PM
Registered User
 
Join Date: Jun 2011
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I did check the database, but no, any records that I try to insert dont save in the database either... any data that I fill in at the AdddEditReview.aspx page dissapears as soon as I click 'insert'.

Thank you again for all the help you are giving me!
 
Old June 17th, 2011, 06:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Found it. Take a look at this:

Code:
 
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
  e.Values["UpdateTimeNow"] = DateTime.Now;
}
UpdateTimeNow is not a valid column name or select parameter. It shoukd be UpdateDateTime.

The Response.Redirect is hiding the real error from you. Uncomment the code in EndEditing and you'll see the exception message. Changing the column name then fixes the problem.

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:
Soccerguy (June 20th, 2011)









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