Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
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 November 21st, 2011, 01:15 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default Ch13 Manage Data with the DetailsView Control

Hi all,

I am stuck at the above Try it Out. I have gone through the session and when I click a link on the management page it take me to the DetailsView page but the fields are all empty and the record I clicked on is not displayed...

What could I be doing wrong?

Regards
Lee
 
Old November 21st, 2011, 01:22 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,

Hard to tell without seeing your code. Can you post the markup and code behind of the relevant pages? Maybe the ID isn't passed on correctly?

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 November 21st, 2011, 01:25 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Hi,

This is the code for the DetailsView page:

Code:
<%@ Page Title="Names Update" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="AddEditNames.aspx.vb" Inherits="Management_AddEditNames" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        <br />
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataKeyNames="ID" DataSourceID="SqlDataSource1" DefaultMode="Insert" 
            Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Surname" HeaderText="Surname" 
                    SortExpression="Surname" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" 
                    SortExpression="DateOfBirth" />
                <asp:BoundField DataField="IssueID" HeaderText="IssueID" 
                    SortExpression="IssueID" />
                <asp:BoundField DataField="Address01" HeaderText="Address01" 
                    SortExpression="Address01" />
                <asp:BoundField DataField="Address02" HeaderText="Address02" 
                    SortExpression="Address02" />
                <asp:BoundField DataField="Address03" HeaderText="Address03" 
                    SortExpression="Address03" />
                <asp:BoundField DataField="PostCode" HeaderText="PostCode" 
                    SortExpression="PostCode" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="Mobile" HeaderText="Mobile" 
                    SortExpression="Mobile" />
                <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            DeleteCommand="DELETE FROM [names] WHERE [ID] = @ID" 
            InsertCommand="INSERT INTO [names] ([Name], [Surname], [Age], [DateOfBirth], [IssueID], [Address01], [Address02], [Address03], [PostCode], [Email], [Mobile]) VALUES (@Name, @Surname, @Age, @DateOfBirth, @IssueID, @Address01, @Address02, @Address03, @PostCode, @Email, @Mobile)" 
            SelectCommand="SELECT [ID], [Name], [Surname], [Age], [DateOfBirth], [IssueID], [Address01], [Address02], [Address03], [PostCode], [Email], [Mobile] FROM [names] WHERE ([Name] = @Name)" 
            UpdateCommand="UPDATE [names] SET [Name] = @Name, [Surname] = @Surname, [Age] = @Age, [DateOfBirth] = @DateOfBirth, [IssueID] = @IssueID, [Address01] = @Address01, [Address02] = @Address02, [Address03] = @Address03, [PostCode] = @PostCode, [Email] = @Email, [Mobile] = @Mobile WHERE [ID] = @ID">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Surname" Type="String" />
                <asp:Parameter Name="Age" Type="String" />
                <asp:Parameter DbType="Date" Name="DateOfBirth" />
                <asp:Parameter Name="IssueID" Type="Int32" />
                <asp:Parameter Name="Address01" Type="String" />
                <asp:Parameter Name="Address02" Type="String" />
                <asp:Parameter Name="Address03" Type="String" />
                <asp:Parameter Name="PostCode" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="Mobile" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="Name" QueryStringField="Name" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Surname" Type="String" />
                <asp:Parameter Name="Age" Type="String" />
                <asp:Parameter DbType="Date" Name="DateOfBirth" />
                <asp:Parameter Name="IssueID" Type="Int32" />
                <asp:Parameter Name="Address01" Type="String" />
                <asp:Parameter Name="Address02" Type="String" />
                <asp:Parameter Name="Address03" Type="String" />
                <asp:Parameter Name="PostCode" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="Mobile" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
    </p>
</asp:Content>
And this is the code for the page prior:


Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Management_Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        
        <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" 
            AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="IssueName" 
            DataValueField="IssueID">
            <asp:ListItem value = "">Please select a condition</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [Issues]"></asp:SqlDataSource>
        
        <br />
        
    </p>
    <p>
        
       
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" DataSourceID="SqlDataSource2">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:HyperLinkField DataNavigateUrlFields="ID" 
                    DataNavigateUrlFormatString="AddEditNames.aspx?{0}" DataTextField="Name" 
                    HeaderText="Name" />
                <asp:BoundField DataField="Surname" HeaderText="Surname" 
                    SortExpression="Surname" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="IssueID" HeaderText="IssueID" 
                    SortExpression="IssueID" />
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT [ID], [Name], [Surname], [Age], [IssueID] FROM [names] WHERE ([IssueID] = @IssueID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="IssueID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <a href="AddEditNames.aspx">Insert new client</a><br />
        
    </p>
</asp:Content>
 
Old November 21st, 2011, 01:27 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

This is the code behind:


Code:
Partial Class Management_AddEditNames
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Request.QueryString.Get("Id") IsNot Nothing Then
            DetailsView1.DefaultMode = DetailsViewMode.Edit
        End If
    End Sub
End Class
 
Old November 21st, 2011, 01:35 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I can see a few things that could be wrong:

AddEditNames.aspx?{0}

You're not creating a Query String parameter. Something like this:

AddEditNames.aspx?Name={0}

or this:

AddEditNames.aspx?Id={0}

should work.

Then your DetailsView looks at the Name query string, while your code behind looks at the ID. All of them should line up.....

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!
 
Old November 21st, 2011, 01:42 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Thanks. I did this... but it still doesn't work!

Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Management_Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        
        <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" 
            AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="IssueName" 
            DataValueField="IssueID">
            <asp:ListItem value = "">Please select a condition</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [Issues]"></asp:SqlDataSource>
        
        <br />
        
    </p>
    <p>
        
       
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" DataSourceID="SqlDataSource2">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:HyperLinkField DataNavigateUrlFields="ID" 
                    DataNavigateUrlFormatString="AddEditNames.aspx?ID={0}" DataTextField="Name" 
                    HeaderText="Name" />
                <asp:BoundField DataField="Surname" HeaderText="Surname" 
                    SortExpression="Surname" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="IssueID" HeaderText="IssueID" 
                    SortExpression="IssueID" />
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT [ID], [Name], [Surname], [Age], [IssueID] FROM [names] WHERE ([IssueID] = @IssueID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="IssueID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <a href="AddEditNames.aspx">Insert new client</a><br />
        
    </p>
</asp:Content>
 
Old November 21st, 2011, 01:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And how did you fix the page with the DetailsView?

What do you see in the browser's address bar?

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 November 21st, 2011, 01:51 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

I didn't change anything in the details view... What should I have changed?

My browser shows: http://localhost:49346/WebSite1/Mana...mes.aspx?ID=20
 
Old November 21st, 2011, 01:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Then your DetailsView looks at the Name query string, while your code behind looks at the ID. All of them should line up.....
You're mixing Name and Id at various locations.You should one or the other, but not mix them....

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 November 21st, 2011, 01:57 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

I have changed the code behind to be ID so that the DetailsView is also ID, but when I now run the page, the new page appears but with no table at all, empty or filled...!





Similar Threads
Thread Thread Starter Forum Replies Last Post
DetailsView Data Control behaviour mat41 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 November 1st, 2010 03:18 AM
DetailsView control stormage BOOK: Beginning ASP.NET 4 : in C# and VB 1 November 1st, 2010 03:17 AM
Is it possible to manage data entry and updates th Bjay Pro VB Databases 0 July 22nd, 2007 04:49 PM
DetailsView Control Showkkath ASP.NET 1.x and 2.0 Application Design 2 August 21st, 2006 07:49 AM





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