Share via


How to build your first Web Application with SQL Server and ASP.NET (RTM Update)

Version: 2.2.9

Description

In this Hands On Lab you will learn how to build a functional ASP.NET web application with Microsoft Visual Studio that uses Microsoft SQL Server to manage product data and images.

Overview

If you want to learn the basics of how to build web applications with the Microsoft Web Platform, this hands-on lab is the perfect starting point because it is full of practical information and valuable best practices. If you complete all of the exercises, you will learn how to build a functional ASP.NET web application with Microsoft Visual Studio that uses Microsoft SQL Server to manage product data and images. If you are new to the Microsoft Web Platform, this overview section provides some helpful introductory information to get you started.

More than just a powerful set of tools, servers and technologies, the Microsoft Web Platform offers a complete ecosystem for building and hosting web sites, services and applications. Whether you are looking to build your company’s web presence or launch the next MySpace, we have got the products you need to be successful. The Microsoft Web platform is trusted by the Fortune 500, used by millions of individuals and built to work for you. This hands-on lab will introduce you to a few important components of the Microsoft Web Platform, including Microsoft SQL Server, the Microsoft.NET Framework, ASP.NET and Visual Studio. Check out the Web Platform Installer which makes it easy to find and install everything you need to start building applications, including free editions of SQL Server and Visual Studio. If you want to learn more about the Microsoft Web Platform or the Web Platform Installer, check out the official website at https://www.microsoft.com/web.

Microsoft SQL Server is a family of database products, tools and services used to manage and analyze data of all shapes and sizes. Developers use SQL Server to create everything from small desktop databases, to data-driven web applications, to large enterprise-grade databases. SQL Server supports an ANSI standard Structured Query Language syntax for working with data known as Transact-SQL. The four basic Transact-SQL commands for manipulating data are SELECT for retrieving data, INSERT to add new data, UPDATE to change exiting data and DELETE to remove data. You can send these commands directly from an application, or you can create SQL Server stored procedures that perform database operations and execute them from your application. If you are new to SQL Server, check out the official SQL Server website at https://www.microsoft.com/sql.

The Microsoft .NET Framework is both a flexible runtime environment and a collection of class libraries and services for building applications. .NET supports a variety of popular programming languages including C# and Visual Basic. In this hands-on lab, you will use the C# language to program a web application. If you are new to the .NET Framework, check out the .NET Framework Developer Center on MSDN at https://msdn.microsoft.com/netframework.

The .NET Framework includes a set of services known as ASP.NET to create everything from small, personal websites to large enterprise-grade web applications. ASP.NET provides useful web controls for binding data to the user interface of a web application, including the ListView and DetailsView controls which you will use in this lab. If you are new to ASP.NET, check out the official ASP.NET website at https://www.asp.net.

Most .NET applications need to access data stored in a database. The .NET Framework includes System.Data, also known as ADO.NET, for data programmability.

ADO.NET provides a comprehensive set of class libraries, services and providers for working with data in an application. Some important class libraries for working directly with SQL Server data include SqlConnection, SqlCommand and SqlDataReader in System.Data.SqlClient, which is the ADO.NET data provider for SQL Server. Applications use SqlConnection to establish a connection with SQL Server, then SqlCommand to send Transact-SQL commands using the connection. If the command executes successfully, results are returned to the application and can be read directly with SqlDataReader. If you are new to ADO.NET, check out the Data Platform Developer Center on MSDN at https://msdn.microsoft.com/data.

While the .NET Framework includes its own SDK and compiler, most .NET developers prefer to use the Microsoft Visual Studio family of products to develop their .NET and SQL Server applications. Visual Studio makes developers more productive thanks to breakthrough features like IntelliSense and refactoring. If you are new to Microsoft Visual Studio, check out the Visual Studio developer center on MSDN at https://msdn.microsoft.com/vstudio.

Objectives

In this Hands-on Lab, you will learn how to:

  • Create your first web application using ASP.NET and SQL Server.
  • Design a class in C# that makes it easy to work with SQL Server data using a business object.
  • Bind data from SQL Server to ASP.NET web server controls using a business object.
  • Display images on web pages that are stored in SQL Server.
  • Build a SQL Server connection string and use it to connect to SQL Server using the ADO.NET SqlConnection class.
  • Construct parameterized Transact-SQL statements in an application and execute them using the ADO.NET SqlCommand class.
  • Read query results returned by SQL Server using the ADO.NET SqlDataReader class.
  • Handle database null values from SQL Server in an ASP.NET application.
  • Automatically generate unique primary keys using SQL Server IDENTITY values.
  • Use transactions to ensure that data modifications are performed in a consistent fashion.
  • Use optimistic concurrency to avoid lost updates.

System Requirements

You must have the following items to complete this lab:

Setup

The setup and configuration for this lab involves running a Setup script included with the training kit. To run all setup tasks for this lab:

Note:
This process may require administrative privileges.
  1. Open a Windows Explorer window and browse to the lab’s Source\Setup folder.
  2. Double-click the Setup.cmd file in this folder to launch the setup script and run all setup tasks for this lab.
  3. If the User Account Control dialog is shown, confirm the action to proceed.

Exercises

The following exercises make up this Hands-on Lab:

  1. Connecting to a SQL Server database
  2. Reading data from a SQL Server database
  3. Inserting data
  4. Updating data
  5. Deleting data
Note:
Each exercise is accompanied by a starting or “begin” solution. These solutions are missing some code sections that are completed through each exercise and therefore will not work if running them directly.

Inside each exercise, you will also find an end folder where you find the completed solution. You can use this solution as a guide if you need additional help working through the exercises.

Estimated time to complete this lab: 90 minutes.

Feedback

Your feedback is welcome! Please, post it on our UserVoice forums.