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 August 26th, 2012, 11:15 AM
Registered User
 
Join Date: Aug 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Default Ch. 13 - PlanetWroxConnectionString1

Hi;

I am working through the "Using the GridView and SqlDataSource Controls" section, and am having a problem with the auto-generated connectionstring. The book suggests that this connectionstring should default to "PlanetWroxConnectionString1" however this does not appear to be happening for me, and I get a default of "ConnectionString" - I'm obviously missing something, but I've been through the exercise twice and it has happened both times.

Any help will be gratefully received.

Code:
<%@ Page Title="Planet Wrox - Management - Genres" Language="C#" MasterPageFile="~/MasterPages/Management.master" AutoEventWireup="true" CodeFile="Genres.aspx.cs" Inherits="Management_Genres" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" 
    DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display.">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
            ShowSelectButton="True" />
        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" 
            SortExpression="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="SortOrder" HeaderText="SortOrder" 
            SortExpression="SortOrder" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    DeleteCommand="DELETE FROM [Genre] WHERE [Id] = @Id" 
    InsertCommand="INSERT INTO [Genre] ([Name], [SortOrder]) VALUES (@Name, @SortOrder)" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT [Id], [Name], [SortOrder] FROM [Genre]" 
    UpdateCommand="UPDATE [Genre] SET [Name] = @Name, [SortOrder] = @SortOrder WHERE [Id] = @Id">
    <DeleteParameters>
        <asp:Parameter Name="Id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="SortOrder" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="SortOrder" Type="Int32" />
        <asp:Parameter Name="Id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
 
Old August 26th, 2012, 12:46 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,

That's not a problem per se; as long as the connection string name here points to one with the same name in the web.config file.

Usually, VS creates the name after the database (and thus it should be prefixed with PlanetWrox). What's the name of your database? And did you drag the database table from the Database Explorer? Which version of VS are you using?

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 August 26th, 2012, 01:46 PM
Registered User
 
Join Date: Aug 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Default Ch. 13 - PlanetWroxConnectionString1

Hi Imar;

I am using Visual Web Developer 2010 Express, the name of the database is PlanetWrox.mdf, it is in the App_Data folder, and I dragged the Genre table from Database Explorer into the cpMainContent area of the Management/Genres.aspx file while it was in Design View.

Your questions raise another issue, the <connectionStrings> declaration ends up in the Default/web.config file rather than the Management/web.config file:

Code:
<connectionStrings>
      <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PlanetWrox.mdf;Integrated Security=True;User Instance=True"
         providerName="System.Data.SqlClient" />
   </connectionStrings>
As you say, it isn't a problem on its own, but when you get to the next stage where there are two SqlDataSource declarations on the same page it seems to be causing problems!

Thanks for looking at this, and on a Sunday too!

John
 
Old August 26th, 2012, 01:52 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:
Your questions raise another issue, the <connectionStrings> declaration ends up in the Default/web.config file rather than the Management/web.config file:
That's how it's supposed to be.


Quote:
but when you get to the next stage where there are two SqlDataSource declarations on the same page it seems to be causing problems!
Why? Both controls would simply point to the same connection string which is also how it's supposed to be. The name is just that; a name; it doesn't matter much what name it is....
Quote:
Thanks for looking at this, and on a Sunday too!
You're welcome.

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 August 26th, 2012, 02:03 PM
Registered User
 
Join Date: Aug 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
That's how it's supposed to be.
Cool, it was confusing me.

Quote:
Why? Both controls would simply point to the same connection string which is also how it's supposed to be. The name is just that; a name; it doesn't matter much what name it is....
It appeared to me that the "Genres" and "Reviews" tables were being pointed to indiscriminately, but perhaps I had made an error unrelated to the <connectionString> declaration. I will go over it again and see if I get a different result.

Thanks Imar.
 
Old August 26th, 2012, 02:30 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:
It appeared to me that the "Genres" and "Reviews" tables were being pointed to indiscriminately
The connection string name points to a named connection string in the web.config which in turn points to your database server and the database. This could / typically is the same for all code in your application.

The SqlDataSource and other controls / code then refer to individual tables in the databaase.

Hope this clariefies things.

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!
The Following User Says Thank You to Imar For This Useful Post:
hopper3011 (August 27th, 2012)
 
Old August 27th, 2012, 02:19 AM
Registered User
 
Join Date: Aug 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks

Hi Imar;

Thanks for all your help, it gave me confidence to know that what I was seeing was right, because it looked odd.

I still haven't been able to get the auto-generate to work correctly, but by inserting the missing pieces of code manually the DetailsView now works as it should.

Thanks again,

John





Similar Threads
Thread Thread Starter Forum Replies Last Post
another question in ch 13 nawar youssef BOOK: Beginning PHP 5.3 0 October 17th, 2011 12:40 AM
Ch 13 Use of @ in front of variable name comecme BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 7 December 2nd, 2010 07:23 PM
Ch 13-cms_articles_compose nuhbutt BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 1 June 1st, 2010 12:28 PM
Simple Pagination for Ch 13 djlfreak BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 June 1st, 2010 08:29 AM
Search ch 13, ch 16 sporik BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 October 27th, 2009 04:44 PM





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