Cover Page

BEGINNING ASP.NET 4.5.1 IN C# AND VB

 

Imar Spaanjaars

 

 

 

Wiley Logo

To Niek

ABOUT THE AUTHOR

IMAR SPAANJAARS graduated in Leisure Management at the Leisure Management School in the Netherlands, but he quickly changed his career path to the Internet world.

After working in the Internet business at various web agencies for over twelve years, he now runs his own company called De Vier Koeden (http://devierkoeden.com), a small Internet agency specializing in consultancy and development of Internet and intranet applications with Microsoft technologies such as ASP.NET 4.5.1. He’s also the CTO of Dynamicweb North America, the U.S. branch of the popular Danish content management, e-commerce, and online marketing platform.

Imar has written books on ASP.NET and Macromedia Dreamweaver, all published under the Wrox brand. He is also one of the top contributors to the Wrox Community Forum at p2p.wrox.com, where he shares his knowledge with fellow programmers.

Imar has received Microsoft’s Most Valuable Professional (MVP) award each year since 2008 for his contributions to the ASP.NET community. In early 2012, Imar joined the ASPInsiders, a small group of international professionals that provide feedback and direction on new features for future versions of ASP.NET.

Imar lives in Utrecht, the Netherlands, with his girlfriend Fleur and his son Niek. You can contact him through his personal website at http://imar.spaanjaars.com or by e-mail at imar@spaanjaars.com.

CREDITS

Acquisitions Editor

Mary James

Project Editor

Brian Herrmann

Technical Editor

Jason Gaylord

Production Editor

Daniel Scribner

Editorial Manager

Mary Beth Wakefield

Associate Director of Marketing

David Mayhew

Marketing Manager

Ashley Zurcher

Business Manager

Amy Knies

Vice President and Executive Group Publisher

Richard Swadley

Associate Publisher

Jim Minatel

Project Coordinator, Cover

Katie Crocker

Proofreaders

Josh Chase, Louise Watson, Sarah Kaikini, Word One

Indexer

Robert Swanson

Cover Designer

Ryan Sneed

Cover Image

© Nevin Giesbrecht / iStockPhoto

ACKNOWLEDGMENTS

ALTHOUGH THE JUMP in version number by only 0.1 seems to suggest that there’s not much new in ASP.NET 4.5.1 or Visual Studio 2013, you’d be surprised at the number of changes—small and large—that made their way into these products. I spent the past couple of months working on updating this book from the .NET 4.5 release to the new .NET 4.5.1 release. I discovered new features and functionality every day. Some of those changes are really small, but could mean a boost in productivity on a day-to-day basis. Others are much bigger and affect the way you build or deploy your websites. I tried to incorporate all the new features found in ASP.NET and Visual Studio that will make sense for you, someone with no or limited experience with ASP.NET.

I have also made a lot of changes to the book based on reader feedback. Just as with the previous versions of the book, I went over all the errata that have been submitted as well as over the hundreds of forum posts that were made, identifying areas in the book that readers had difficulties with, and finding ways to improve it. If you have the previous edition and posted a question in the Wrox forums: thanks for your valuable feedback; you’ve really helped to make this book better.

Besides my readers, I owe a lot to other people who helped me write this book.

First of all, a big thanks goes out to Brian Herrmann for his editorial work. Once again, it was a pleasure to work with you! I also want to thank Jason Gaylord for his many useful suggestions he provided as a technical editor. Both of you really helped shape this book. Many thanks also to the people from Wrox for their support and contributions to this book.

Another person I owe a lot to is my friend Anne Ward from Blue Violet, a UK-based web and graphic design company. Anne has done most of the designs used in this book and I highly appreciate her input. Thanks again, Anne! The concert pictures you see in this book come from Nigel D. Nudds, who kindly let me use pictures from his collection.

Finally, I would like to thank my lovely girlfriend Fleur for her support during this project. I couldn’t have done it without you!

FOREWORD

The adoption rate of emerging standards like HTML5 and CSS3 grows every day. Things that were only possible on thick client apps are becoming a reality on the web. With browsers getting faster and better each day, with more common tasks becoming available as reusable libraries, and with open sourcing of nearly all big web frameworks, our World Wide Web is a happening place. Penetration of mobile devices and the varied mobile app development technologies are making developers further consider the open and accessible web as their medium of expression.

During this time, client side libraries like jQuery and jQuery mobile, Angular.js, Knockout.js and server-side technologies like ASP.NET are making typically difficult and cumbersome tasks approachable. On top of all this, free tools like Visual Web Developer make web development more fun than ever before. It is indeed a joy to be a web developer these days, and it is nice to see this book come out and make becoming a web developer approachable for everyone.

Imar Spaanjaars, the author of this book, has been a Microsoft MVP in ASP.NET since 2008, and this time around we also had him join the ASP.NET Insiders group, in which we bounce feature ideas and pre-release products even before they ever get to public beta. Imar has been a constant source of feedback for the team during the development process and I am certain he will continue to be so even in the future.

In Beginning ASP.NET 4.5.1: in C# and VB he starts slow, goes deep, builds concepts, and covers the latest features of both ASP.NET 4.5.1 and Visual Studio 2013. Whether you are just starting web development or upgrading to ASP.NET 4.5.1, this book is certainly worth adding to your toolbox.

It is my pleasure to know Imar, and I want to thank him for his contribution to our community. His insights and thoughts were invaluable to the product team behind ASP.NET and Visual Studio. I hope his insights will help you too.

Vishal R. Joshi

Principal Program Manager Lead
Windows Azure Group, Microsoft Corporation
http://vishalrjoshi.com

INTRODUCTION

TO BUILD EFFECTIVE AND ATTRACTIVE DATABASE-DRIVEN WEBSITES, you need two things: a solid and fast framework to run your web pages on and a rich and extensive environment to create and program these web pages. With ASP.NET 4.5.1 and Visual Studio 2013 you get both. Together they form the platform to create dynamic and interactive websites.

ASP.NET 4.5.1 builds on top of its popular predecessors ASP.NET 2.0, 3.5, 4.0, and 4.5. While maintaining backward compatibility with sites built using these older versions, ASP.NET 4.5.1 and Visual Studio 2013 introduce new, exciting features.

You’ll also find many changes — small and large — in both the ASP.NET Framework and Visual Studio. Some of these changes are the inclusion of Entity Framework 6 (discussed in Chapter 14), the introduction of Browser Link discussed in Chapter 18 and the introduction of the Publish Wizard for Web Site Projects, discussed in Chapter 19.

If you haven’t used Visual Studio 2012 with ASP.NET 4.5 yet, you’ll also appreciate the improved CSS and JavaScript editors (discussed in Chapter 3 and Chapter 10, respectively), the inclusion of NuGet (Chapter 11), and the Page Inspector (Chapter 18), all of which were added in VS 2012 that VS 2013 builds on top of.

If you’re familiar with earlier versions of ASP.NET, you’ll be happy to find many small gems in the new version of the framework that will make your life as a developer easier. I mention and discuss these new features throughout this book where appropriate. For a complete list of all new features in ASP.NET, check out the following white paper at the official ASP.NET website:

www.asp.net/visual-studio/overview/2013/release-notes

If this link no longer works by the time you read this book, search www.asp.net for: “What’s new in ASP.NET 4.5.1.”

Probably the best thing about Visual Studio Express 2013 for Web is its price: it’s available for free. This makes Visual Studio and ASP.NET probably the most attractive and compelling web development technologies available today.

WHO THIS BOOK IS FOR

This book is for anyone who wants to learn how to build rich and interactive websites that run on the Microsoft platform. With the knowledge you gain from this book, you create a great foundation to build any type of website, ranging from simple hobby-related websites to sites you may be creating for commercial purposes.

Anyone new to web programming should be able to follow along because no prior background in web development is assumed, although it helps if you do have a basic understanding of HTML and the web in general. The book starts at the very beginning of web development by showing you how to obtain and install Visual Studio. The chapters that follow gradually introduce you to new technologies, building on top of the knowledge gained in the previous chapters.

Do you have a strong preference for Visual Basic over C# or the other way around? Or do you think both languages are equally cool? Or maybe you haven’t made up your mind yet and want to learn both languages? Either way, you’ll like this book because all code examples are presented in both languages!

Even if you have some experience with prior versions of ASP.NET, you may gain a lot from this book. Although many concepts from previous versions are brought forward into ASP.NET 4.5.1, you’ll discover there’s a lot of new stuff to be found in this book, including the strongly typed data controls, smarter code editors, new debugging facilities, and more.

WHAT THIS BOOK COVERS

This book teaches you how to create a feature-rich, data-driven, and interactive website called Planet Wrox. Although this is quite a mouthful, you’ll find that with Visual Studio 2013, developing such a website isn’t as hard as it seems. You’ll see the entire process of building a website, from installing Visual Studio in Chapter 1 all the way up to putting your website on a live server in Chapter 19. The book is divided into 19 chapters, each dealing with a specific subject:

HOW THIS BOOK IS STRUCTURED

This book takes the time to explain concepts step by step using working examples and detailed explanations. Using the famous Wrox Try It Out and How It Works sections, you are guided through a task step by step, detailing important things as you progress through the task. Each Try It Out task is followed by a detailed How It Works section that explains the steps you performed in the exercise.

At the end of each chapter, you find exercises that help you test the knowledge you gained in this chapter. You can find the answers to each question in Appendix A at the end of this book. Don’t worry if you don’t know all the answers to the questions. Later chapters do not assume you followed and carried out the tasks from the exercise sections of previous chapters.

Because this is a beginner’s book, I can’t go into great detail on a number of topics. For nearly every chapter in this book, you’ll find numerous other books that exclusively deal with the topic discussed. Where appropriate, I have included references to these books so you can easily decide where to go to next if you want to deepen your knowledge on a specific subject.

WHAT YOU NEED TO USE THIS BOOK

This book assumes you have a system that meets the following requirements:

Chapter 1 shows you how to obtain and install Visual Studio 2013, which in turn installs the Microsoft .NET Framework version 4.5.1 and SQL Server Express LocalDB edition; then all you need is a good operating system and the drive to read this book!

CONVENTIONS

To help you get the most from the text and keep track of what’s happening, a number of conventions are used throughout the book.

As for styles in the text:

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download from the book’s own page on the Wrox website at www.wrox.com/go/begaspnet451. If somehow this link no longer works, go to www.wrox.com and locate the book either by using the Search box or by using one of the title lists. Click the Download Code link on the book’s detail page to obtain all the source code for the book.

You can download the full source for this book as a single file for each programming language used in the book (C# or Visual Basic). You can decompress these files with your favorite decompression tool. If you extract the source, make sure you maintain the original folder structure that is part of the code download. The different decompression tools use different names for this feature, but look for a feature like Use Folder Names or Maintain Directory Structure. Once you have extracted the files from the code download, you should end up with a folder called Source and a folder called Resources. Then create a new folder in the root of your C drive, call it BegASPNET, and move the Source and Resources folders into this new folder so you end up with folders like C:\BegASPNET\Source and C:\BegASPNET\Resources. The Source folder contains the source for each of the 19 chapters of this book and the final version of the Planet Wrox website. The Resources folder contains files you need during some of the exercises in this book. If everything turned out correctly, you should end up with the structure shown in Figure I-1.

image

FIGURE I-1

Later chapters have you create folders called Site and Release inside the same C:\BegASPNET folder, giving you a folder structure similar to that in Figure I-2.

image

FIGURE I-2

The Site folder contains the site as you’ll build it throughout this book, and the Release folder will contain your final version at the end of this book. Whenever you’re stuck with some examples in this book, you can take a peek in the Source folder to see how things should have ended up.

If you want to run the site for a specific chapter to see how it works, be sure to open the chapter’s folder in Visual Studio as a website. So, you should open a folder such as C:\BegASPNET\Source\Chapter12 directly rather than opening its parent folder C:\BegASPNET\Source.

If you want to follow along in both programming languages, create a second folder called C:\BegASPNETVB or C:\BegASPNETCS to hold the files for the other version. This way, the two sites can coexist without any problems. If you create a folder specifically for the C# language, don’t include the hash symbol (#) because that’s an invalid character in the pathname for a website.

Sticking to this structure ensures a smooth execution of the Try It Out exercises in this book. Incorrectly mixing or nesting these folders makes it harder to carry out the exercises and may even lead to unexpected situations and errors. Whenever you run into an issue or error that is not explained in this book, ensure that your site structure is still closely related to the one presented here.

ERRATA

I have made every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in this book, such as a spelling mistake or a faulty piece of code, I’d be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping me provide even higher quality information.

To find the errata page for this book, go to www.wrox.com/go/begaspnet451 or go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the book’s Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. I’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. I am a frequent visitor of the Wrox forums, and I’ll do my best to help you with any questions you may have about this book.

At p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register Now link.
  2. Read the terms of use and click Agree.
  3. Complete the required information to join as well as any optional information you wish to provide and click Submit.
  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P but in order to post your own messages, you must join (which is free).

After you join, you can post new messages and respond to messages other users post. You’ll find this book’s own forum under the ASP.NET 4.5.1 category that is available from the homepage. You can read messages at any time on the web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.