Lecture E-Commerce - Chapter 25: ASP.NET MVC. In this chapter students will be able to: Introduction, MVC application, MVC folders, MVC layout, MVC controllers, MVC views, MVC database, MVC models.
Trang 1CSC 330 E-Commerce
Teacher
Ahmed Mumtaz Mustehsan
GM-IT CIIT Islamabad
Virtual Campus, CIIT
COMSATS Institute of Information Technology
T2-Lecture-12
Trang 2ASP.NET Part - III
For Lecture Material/Slides Thanks to: www.w3schools.com
Trang 3ASP.NET Part III WebPages (Continued)
Introduction to SQL
Thank You
Trang 5WebPages Databases
Trang 6Displaying Data from Database
With Web Pages, data can easily be displayed from a database
Connect to an existing database, or create a new
database from scratch
The following example will demonstrate to connect to
an existing SQL Server Compact database
Trang 7Displaying Data from Database
<h1>Small Bakery Products</h1>
<table border="1" width="100%">
Trang 8Output of the above Example
2 Strawberry Cake Made with organic
5 Lemon Pie Made with the best
lemons in the world 11.99
Small Bakery Products
Trang 9Example Explained
The Database.Open(name) method will connect to a
database in two steps:
◦First, it searches the application's App_Data folder
for a database that matches the name (Small bakery) parameter without the file-name extension
◦If no file is found, it looks for a "connection string" in the application's Web.config file
(A connection string contains information about how to connect to a database? It can include a file path, or the
password)
This two-step search makes it possible to test the
Trang 10WebPages Helpers
Trang 11ASP.NET Web Pages - Helpers
Web Helpers greatly simplifies web development and common programming tasks
ASP.NET Helpers
ASP.NET helpers are components that can be
accessed by single lines of Razor code
You can build your own helpers using Razor syntax
stored as cshtml files, or use built-in ASP.NET helpers
Trang 12Razor helpers: The WebGrid Helper
Automatically sets up an HTML table to display data
Supports different options for formatting
Supports paging (First, next, previous, last) through
data
Supports sorting by clicking on column headings
Trang 13The Chart Helper
The "Chart Helper" can display chart images of
different types with many formatting options and labels
Trang 14The WebMail Helper
email messages using SMTP (Simple Mail Transfer
Protocol)
Trang 15The WebImage Helper
and manipulate images in a web page
Trang 16WebPages WebGrid
Trang 17ASP.NET Web Pages - The WebGrid Helper
WebGrid - One of many useful ASP.NET Web Helpers
We have already learnt to display database data by
using razor code, and using the HTML markup
Trang 18ASP.NET Web Pages
( Displaying Data from Database previous Method)
Database Example
@{
var db =
Database.Open("SmallBakery");
var selectQueryString = "SELECT *
FROM Product ORDER BY Name";
Trang 19Output of the above Example
2 Strawberry Cake Made with organic
5 Lemon Pie Made with the best
lemons in the world 11.99
Small Bakery Products
Trang 20Using The WebGrid Helper
Using the WebGrid helper is an easier way to display data from Database
The WebGrid helper:
◦Automatically sets up an HTML table to display data
◦Supports different options for formatting
◦Supports paging through data
◦Supports Sorting by clicking on column headings
Trang 22WebGrid Example - Output
Small Bakery Products
Id Name Description Price
1 Bread Baked fresh every day 2,99
3 Pecan Pie If you like pecans, this is
Trang 23The Chart Helper
The "Chart Helper" can display chart images of
different types with many formatting options and labels
It can create standard charts like area charts, bar
charts, column charts, line charts, and pie charts, along with more specialized charts like stock charts
Trang 24The Chart Helper – Chart from an Array
Trang 25The Chart Helper
new Chart creates a new chart object and sets its width and height
Trang 26WebPages Email
Trang 27ASP.NET Web Pages
The WebMail Helper
The WebMail Helper makes it easy to send an email from a web application using SMTP (Simple Mail
transfer Protocol)
To demonstrate the use of email, we will create an
input page for support, let the user submit the page to another page, and send an email about the support
problem
Trang 28First: Edit Your AppStart Page
Add in the page called _AppStart.cshtml (already
learnt) contain the following contents:
Trang 29First: Edit Your _AppStart Page
Add the following entries:
SmtpServer: The name the SMTP server that will be used to
send the emails.
SmtpPort: The port the server will use to send SMTP
transactions (emails).
EnableSsl: True, if the server should use SSL (Secure
Socket Layer) encryption False otherwise
UserName: The name of the SMTP email account used to
send the email.
Password: The password of the SMTP email account.
same as UserName).
Trang 30First: Edit Your AppStart Page
How to add the WebMail properties to AppStart page:
Trang 31Second: Create an Email Input Page
Then create an input page, and name it Email_Input:
Email_Input.cshtml
<!DOCTYPE html>
<html>
<body>
<h1>Request for Assistance</h1>
<form method="post" action="EmailSend.cshtml">
<label>Username:</label>
<input type="text name="customerEmail" />
<label>Details about the problem:</label>
<textarea name="customerRequest" cols="45" rows="4"></textarea>
<p><input type="submit" value="Submit" /></p>
</form>
</body>
</html>
Trang 32Third: Create An Email Send Page
Then create the page that will be used to send the email, and name
it Email_Send:
Email_Send.cshtml
@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:" support@vcomsats.edu.pk ", subject: "Help
request from - " + customerEmail, body: customerRequest );
Trang 33WebPages Publish
Trang 34ASP.NET Web Pages - Publishing the Website
Publish Your Application Without Using WebMatrix
An ASP.NET Web Pages application can be published
to a remote server by using the Publish commands in
WebMatrix (or Visual Studio)
This function copies all your application files, cshtml
pages, images, and all the required DLL files for Web
Pages, for Razor, for Helpers, and for SQL Server
Compact (if a database is used)
Trang 35Publish Your Application Without Using
WebMatrix
To perform a web copy, you need to know how to
include the right files, what DDL files to copy, and
where store them
Follow these steps:
1 Use the Latest Version of ASP.NET
Before you continue, make sure your hosting computer runs the latest version of ASP.NET (4.0 or 4.5)
2 Copy the Web Folders
Copy your website (all folders and content) from your development computer to an application folder on your remote hosting computer (server)
Trang 36Publish Your Application Without Using
Trang 37Publish Your Application Without Using
WebMatrix
4 Copy Your Data
If your application contains data or a database For
instance an SQL Server Compact database (a sdf file in App_Data folder), consider the following:
Do you want to publish your test data to the remote
server?
Usually not (We do testing locally before publishing)
If you have test data on your development computer, it may overwrite production data on your remote hosting
computer
If you have to copy an SQL database (.sdf file), you
may delete everything in the database, and then copy the
Trang 38The End ASP.NET
Trang 39Introduction to SQL
Trang 40What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards
Institute) standard
Trang 41What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
Trang 42Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
An RDBMS database program (i.e MS Access, SQL Server, MySQL)
To use a server-side scripting language, like PHP or ASP
To use SQL to get the data you want
To use HTML / CSS
Trang 43RDBMS stands for Relational Database Management System
RDBMS is the basis for SQL, and for all modern
database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access
The data in RDBMS is stored in database objects
called tables
A table is a collection of related data entries and it
consists of columns and rows
Trang 44Database Table
A database most often contains one or more tables
Each table is identified by a name (e.g "Customers" or
"Orders") Tables contain records (rows) with data
In this Lecture we will use the sample database picked
up from W3schools Tutorial
Below is a selection from the "Customers" table:
Emparedados y helados
Constitución 2222
Taquería
Trang 45SELECT * FROM Customers;
Keep in Mind That
SQL is NOT case sensitive: select is the same as
SELECT
Trang 46The Most Important SQL Commands
Trang 47The SQL SELECT Statement
The SELECT statement is used to select data from a database
The result is stored in a result table, called the set
result-SQL SELECT Syntax
SELECT column_name,column_name FROM table_name;
SELECT * FROM table_name;
Trang 48Semicolon after SQL Statements?
Some database systems require a semicolon at the
end of each SQL statement
Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server
Use semicolon at the end of each SQL statement
Trang 49Demo Database
sample database
Emparedados y helados
Constitución 2222
Taquería
snabbköp
Trang 50SELECT Column Example
The following SQL statement selects the
"CustomerName" and "City" columns from the
Trang 51The SQL SELECT DISTINCT Statement
In a table, a column may contain many duplicate
values; and sometimes you only want to list the
different (distinct) values
distinct (different) values
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name
FROM table_name;
Or
SELECT DISTINCT City FROM Customers;
The above SQL statement selects only the distinct
values from the "City" columns from the "Customers"
Trang 52The SQL WHERE Clause
The WHERE clause is used to extract only those
records that fulfill a specified criterion
SQL WHERE Syntax
SELECT column_name,column_name FROM table_name
WHERE column_name operator value;
Example
SELECT * FROM Customers WHERE Country='Mexico';
The above SQL statement selects all the customers from the country "Mexico", in the "Customers" table:
Trang 53Text Fields vs Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double quotes)
However, numeric fields should not be enclosed in
quotes:
Example
SELECT * FROM Customers WHERE CustomerID=1;
Trang 54Operators in The WHERE Clause
The following operators can be used in the WHERE
Trang 55The SQL AND & OR Operators
The AND operator displays a record if both the first
condition AND the second condition are true
The OR operator displays a record if either the first
condition OR the second condition is true
AND Operator Example
SELECT * FROM Customers WHERE
Country='Germany‘ AND City='Berlin';
The above SQL statement selects all customers from the country "Germany" AND the city "Berlin", in the
"Customers" table:
Trang 57Combining AND & OR
You can also combine AND and OR (use parenthesis
to form complex expressions)
Example
SELECT * FROM Customers WHERE Country='Germany'
AND (City='Berlin' OR City='München');
The above SQL statement selects all customers from the country "Germany" AND the city must be equal to
"Berlin" OR "München", in the "Customers" table:
Trang 58The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set
by one or more columns
The ORDER BY keyword sorts the records in
ascending order by default To sort the records in a
descending order, you can use the DESC keyword
SQL ORDER BY Syntax
SELECT column_name,column_name FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example:
SELECT * FROM Customers ORDER BY Country;
The above SQL statement selects all customers from the "Customers" table, sorted by the "Country" column:
Trang 59ORDER BY DESC Example
Example
SELECT * FROM Customers ORDER BY Country DESC;
The above SQL statement selects all customers from the
"Customers" table, sorted DESCENDING by the
"Country" column:
ORDER BY Several Columns Example
Example
SELECT * FROM Customers ORDER BY Country,CustomerName;
The above SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the
"CustomerName" column:
Trang 60The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new
records in a table
It is possible to write the INSERT INTO statement in two forms
The first form does not specify the column names
where the data will be inserted, only their values:
Trang 61INSERT INTO Example
Assume we wish to insert a new row in the
"Customers" table
We can use the following SQL statement:
Example
INSERT INTO Customers (CustomerName, ContactName,
Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B Erichsen','Skagen
21','Stavanger','4006','Norway');
Trang 62Insert Data Only in Specified Columns
It is also possible to only insert data in specific
columns
The following SQL statement will insert a new row, but only insert data in the "CustomerName", "City", and
"Country" columns (and the CustomerID field will of
course also be updated automatically):
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
Trang 63The SQL UPDATE Statement
The UPDATE statement is used to update existing
Assume we wish to update the customer "Alfreds
Futterkiste" with a new contact person and city
We use the following SQL statement:
UPDATE Customers SET ContactName='Alfred Schmidt',
City='Hamburg‘ WHERE CustomerName='Alfreds Futterkiste';
Trang 65The SQL DELETE Statement
The DELETE statement is used to delete rows in a
table
SQL DELETE Syntax
DELETE FROM table_name WHERE some_column=some_value;
SQL DELETE Example
Assume we wish to delete the customer "Alfreds
Futterkiste" from the "Customers" table
We use the following SQL statement:
DELETE FROM Customers WHERE CustomerName='Alfreds
Futterkiste' AND ContactName='Maria Anders';
Trang 66Delete All Data
It is possible to delete all rows in a table without
deleting the table This means that the table structure, attributes, and indexes will be intact:
DELETE FROM table_name;
or
DELETE * FROM table_name;
Note: Be very careful when deleting records You cannot
undo this statement!
Trang 67End ASP.NET Part III Introduction to SQL
Thank You
Trang 68Visual Studio Express 2012/2010
Visual Studio Express is a free version of Microsoft
Visual Studio
Visual Studio Express is a development tool tailor
made for Web Forms (and MVC)
Visual Studio Express contains:
◦MVC and Web Forms
◦Drag-and-drop web controls and web components
◦A web server language (Razor using VB or C#)
◦A web server (IIS Express)
◦A database server (SQL Server Compact)
◦A full web development framework (ASP.NET)
◦Vista or XP)