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.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad Virtual Campus, CIIT COMSATS Institute of Information Technology T2-Lecture-12 ASP.NET Part - III For Lecture Material/Slides Thanks to: www.w3schools.com ASP.NET Part III WebPages (Continued) Introduction to SQL Thank You Objectives WebPages Databases WebPages Helpers WebPages WebGrid WebPages Charts WebPages Email WebPages PHP WebPages Publish Introduction to SQL T2-Lecture-12 Ahmed Mumtaz Mustehsan www.w3schools.com 1-4 WebPages Databases Displaying 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 T2-Lecture-12 Ahmed Mumtaz Mustehsan www.w3schools.com 1-6 Displaying Data from Database In the "DemoWebPages" folder, create a new CSHTML file named "Products.cshtml" Replace the code in the file with the code from the example below: @{ var db = Database.Open("SmallBakery"); var query = "SELECT * FROM Product"; } Small Bakery Products Id Product Description Price T2-Lecture-12 Ahmed Mumtaz Mustehsan @foreach(var row in db.Query(query)) { @row.Id @row.Name @row.Description @row.Price } www.w3schools.com 1-7 Output of the above Example Id Product Description Price Bread Baked fresh every day 2.99 Strawberry Cake Made with organic strawberries 9.99 Apple Pie Second only to your mom's pie 12.99 Pecan Pie If you like pecans, this is for you 10.99 Lemon Pie Made with the best lemons in the world 11.99 Cupcakes Your kids will love these T2-Lecture-12 Ahmed Mumtaz Mustehsan www.w3schools.com 9.99 1-8 Example 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 name of an SQL database, with full user name and password) This two-step search makes it possible to test the application with a local database, and run the application on a web host using a connection string T2-Lecture-12 Ahmed Mumtaz Mustehsan www.w3schools.com 1-9 WebPages Helpers Operators in The WHERE Clause The following operators can be used in the WHERE clause: Operator Description = Equal Not equal Note: In some versions of SQL this operator may be written as != > Greater than < Less than >= Greater than or equal