- ASP.NET WP - Publish
- ASP.NET WP - Security
- ASP.NET WP - Caching
- Add Social Networking to the Website
- ASP.NET WP - Add Search
- ASP.NET WP - Add Email
- ASP.NET WP - Working with Videos
- ASP.NET WP - Working with Images
- ASP.NET WP - Working with Files
- ASP.NET WP - Charts
- ASP.NET WP - WebGrid
- ASP.NET WP - Delete Database Data
- ASP.NET WP - Edit Database Data
- ASP.NET WP - Add Data to Database
- ASP.NET WP - Database
- ASP.NET WP - Page Object Model
- ASP.NET WP - Working with Forms
- ASP.NET WP - Layouts
- ASP.NET WP - Programming Concepts
- ASP.NET WP - Global Pages
- Project Folder Structure
- ASP.NET WP - View Engines
- ASP.NET WP - Getting Started
- ASP.NET WP - Environment Setup
- ASP.NET WP - Overview
- ASP.NET WP - Home
ASP.NET WP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ASP.NET WP - Database
In this chapter, we will be covering how to create a database in WebMatrix using ASP.NET Web Pages (Razor) and how to display database data in a page.
A database contains one or more tables that contain information, pke table for Customers information or table for Students.
In any given table you have several pieces of information, for example in Customers table there will be their first name, last name, and address, etc.
In most database tables, there is a column that contains a unique identifier which is also known as primary key, pke a CustomerID, or StudentID, etc.
The primary key identifies each row in the table.
Create a Database
WebMatrix provides tools in which you can easily create a database and then can add tables in that database. The structure of a database is referred to as the database s schema. Now let’s open the WebMatrix and create a new empty site.
![Database](/asp.net_wp/images/database.jpg)
Enter WebPagesCustomers in the Site Name field and cpck Next.
In the left pane, cpck Database as highpghted in the following screenshot.
![WebPages Customers](/asp.net_wp/images/webpages_customers.jpg)
Now you will see that it opens the database related options in the ribbon.
![New Database](/asp.net_wp/images/new_database.jpg)
Cpck on the New Database option.
![Page Customers](/asp.net_wp/images/page_customers.jpg)
You will see that WebMatrix creates a SQL Server database which is a *.sdf file that has the same name as your site WebPagesCustomers.sdf and you can also rename this file as well.
Create Table
You can easily create a table in the database either by right cpcking on Tables in the left pane and then selecting a New Table or you can cpck on the New Table option in the ribbon.
![New Table](/asp.net_wp/images/new_table.jpg)
Now you can see that WebMatrix has opened the table designer.
![Table Designer](/asp.net_wp/images/table_designer.jpg)
Enter the Table Name and then some columns and press Ctrl+S to save as shown in the following screenshot.
![Table Name](/asp.net_wp/images/table_name.jpg)
For ID row set, the Is Primary Key? and Is Identify? options to be changed to Yes (if they are not).
Now let’s enter some raw data to work with by cpcking on the Data option and then entering some data as shown in the following screenshot.
![Primary Key](/asp.net_wp/images/primary_key.jpg)
Display Database Data
As we have a database and a Customers table and we also have some data in the database. Now we need to display that on the web page from the database. Let’s create a new CSHTML file.
![Display Database](/asp.net_wp/images/display_database.jpg)
Enter ListCustomers.cshtml in the Name field and cpck OK. Now to retrieve all the customers from the database let’s replace all the code in ListCustomers.cshtml file as shown in the following program.
@{ var db = Database.Open("WebPagesCustomers"); var selectQueryString = "SELECT * FROM Customers ORDER BY FirstName"; } <!DOCTYPE html> <html> <head> <title>Customers List</title> <style> table, th, td { border: sopd 1px #bbbbbb; border-collapse: collapse; padding: 2px; } </style> </head> <body> <h1>Customers List</h1> <table> <thead> <tr> <th>Id</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> </tr> </thead> <tbody> @foreach(var row in db.Query(selectQueryString)){ <tr> <td>@row.ID</td> <td>@row.FirstName</td> <td>@row.LastName</td> <td>@row.Address</td> </tr> } </tbody> </table> </body> </html>
Now let’s run the apppcation and specify the following url − http://localhost:36905/ListCustomers and you will see the pst of customers on the web page as shown in the following screenshot.
![List of Customers](/asp.net_wp/images/pst_of_customers.jpg)