ASP.NET Part 3 Instructor: Charles Moen CSCI/CINF 4230.

Slides:



Advertisements
Similar presentations
CIS 451: ASP Sessions and Applications Dr. Ralph D. Westfall January, 2009.
Advertisements

Introduction to Database Processing with ADO.NET.
Chapter 1: An Introduction To ASP.NET Web Programming
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
ASP.NET Programming with C# and SQL Server First Edition
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom.
C# programming with database Basic guideline. First step Install SQL Server 2008/2010 (Professional edition if possible) Install Visual Studio 2008/2010.
1 Web Database Processing. Web Database Applications Static Report Publishing a report is prepared from a database application and exported to HTML DB.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
ASP.NET Part 4 Instructor: Charles Moen CSCI/CINF 4230.
1 Introducing Database Access from Web Applications by Sally Kyvernitis.
Advanced Web Forms with Databases Programming Right from the Start with Visual Basic.NET 1/e 13.
Chapter 15: Using LINQ to Access Data in C# Programs.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Tutorial 1: Browser Basics.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
A Simple Introduction. What is ADO.net? First the word ADO stands for ActiveX Data Objects And it is an integral part of.Net Framework of Microsoft hence.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
MySQL Connection using ADO.Net Connecting to MySQL from.NET Languages.
11 Using ADO.NET II Textbook Chapter Getting Started Last class we started a simple example of using ADO.NET operations to access the Addresses.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net Basics Ruwan Wijesinghe Trainer.
Caching Chapter 12. Caching For high-performance apps Caching: storing frequently-used items in memory –Accessed more quickly Cached Web Form bypasses:
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
ASP.NET Rina Zviel-Girshin Lecture 5
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
Session and Cookie Management in.Net Sandeep Kiran Shiva UIN:
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Database Handling, Sessions, and AJAX. Post Back ASP.NET Functionality The IsPostBack method in ASP.NET is similar to the BlackBerry.refresh method –IsPostBack.
1.Net programmingADO.NETNOEA / PQC ADO.NET Architecture Connection factory Config file DataReader DataSet DataGrid Autogeneration of SQL.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
CIS 375—Web App Dev II ASP.NET 9 Database. 2 ADO.NET What is ADO.NET? ADO.NET is a part of the.NET Framework ADO.NET consists of a set of classes used.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
HNDIT Rapid Application Development
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
CSCI 4230 Homework #3 Group Three Samer Al Jefri * Kevin Odom * David Hood * JD Wells * Philippe Gambling.
1 Project 4 Address Lookup. Project 4 Write an ASP.NET app that permits users to retrieve addresses from a potentially large list of addresses. There.
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
Querying Information in a Database. CONTENTS Relational Database Systems Creating Database with SQL Server Reading Data with LINQ Requirements to Get.
.NET Data Access and Manipulation
ASP.NET Part 2 Instructor: Charles Moen CSCI/CINF 4230.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Database Processing with ADO.NET
Computing with C# and the .NET Framework
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
E-commerce Applications Development
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

ASP.NET Part 3 Instructor: Charles Moen CSCI/CINF 4230

State Management (Continued from last week)

3 State Management  Storing the data that your web application needs  The problems Thousands of users can be using the same web application at the same time, so we need to be concerned about having sufficient memory and keeping track of each user’s data All users of your web application communicate with it over a stateless HTTP connection  Five choices View state Query string Cookies Session state Application state ASP.NET (MacDonald)‏

4 Session Tracking in ASP.NET  Storing the data in a session object Stored in memory on the server Tracked by the server by using a unique identifier  Scope Global for your entire application for the current user Can be stored between visits by setting the Expires property  Lifetime Times out due to inactivity after a preset amount of time, usually 20 minutes Lost if the user closes and restarts the browser May be lost if the user opens the page in another browser window Session.Abandon() can end the session  Typical use Storing items in a shopping cart ASP.NET (MacDonald)‏

5 Session Example  Two pages that use a session for the user’s favorite color ASP.NET (MacDonald)‏ <asp:Button ID="submitButton" runat="server" onclick="submitButton_Click" Text="Submit" /> SecondPage.aspx

6 Session Example  Two pages that use a session for the user’s favorite color ASP.NET (MacDonald)‏ Default.aspx

7 Session Example ASP.NET (MacDonald)‏ public partial class _Default : System.Web.UI.Page { protected void submitButton_Click(object sender, EventArgs e) { string colorName = favoriteColorTextBox.Text; if (colorName != "") { Color favColor = new Color(colorName); Session["FavoriteColor"] = favColor; } public partial class SecondPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Color favColor = (Color)Session["FavoriteColor"]; if (favColor != null) { favoriteColorLabel.Text = "Favorite color = " + favColor.Name; } else { favoriteColorLabel.Text = "No favorite color"; } public class Color { private string name; public string Name { get { return name; } set { name = value; } } public Color(string name) { Name = name; }

8 C# Properties ASP.NET (MacDonald, Mayo)‏ public class Color { private string name; public string Name { get { return name; } set { name = value; } } public Color(string name) { Name = name; }  Properties are similar to member variables in a class  Easier to use and easier to code public class Color { private string name; public string getName() { return name; } public void setName(string value) { name = value; } public Color(string name) { this.name = name; } Color favColor = new Color("red"); Console.WriteLine(favColor.getName()); Traditional encapsulation with a member variableEncapsulating state with a property Color favColor = new Color("red"); Console.WriteLine(favColor.Name); Getters and setters are shorter

ADO.NET

10 ADO.NET  The.NET technology for interacting with a database A set of classes that are part of the.NET Framework  Two models of data access Connected – direct data access Disconnected – fetch the data and store it in memory  Not the same as “data binding” in ASP.NET, which is binding a control to a data source ASP.NET (MacDonald, Walther)‏

11 Three-Tier Architecture  A type of client-server architecture ASP.NET (MacDonald, Gittleman)‏ Presentation Tier Logic Tier Data Tier ADO.NET

12 Role of the Database  Relational database The most common way to store data for a web application Organizes data into tables, and each row in a table is called a record Each record must have a field called the primary key, which uniquely identifies the record A relationship between two tables can be created by storing the primary key of one table as a foreign key in another table Database Management System (DBMS) is the program that organizes, stores, and manages the data, e.g. MS Access, MS SQL Server, Oracle, MySQL  Most important web sites use data stored in a database Amazon.com – stores products, customers, orders Continental Airlines – stores flight schedules, reservations, frequent flyer miles Google – stores web page URLs, links, and keywords ASP.NET (MacDonald)‏

13 Microsoft Access  Relational DBMS Microsoft product Bundled in some versions of MS Office Can be used for small web applications Available at UHCL  The databases that we use in this class are created by MS Access All the tables are stored in a single file named db.mdb You will need to use MS Access to create the tables for your project The db.mdb file can be downloaded or uploaded by FTP in binary mode For the homework project, consider putting your db.mdb file in a directory named “App_Data” stored in your app directory inside the “pages” directory ASP.NET‏

14 Microsoft Access Example Categories table Two fields, id and category The primary key is “id” ASP.NET‏

15 Microsoft Access Example MenuItems table Five fields, id, item, description, price, and category The primary key is “id” The “category” field is a foreign key from Categories ASP.NET‏

16 SQL  SQL is used for all database interaction through ADO.NET  SQL (Structured Query Language) Standard language for interacting with relational databases Four commonly used commands SELECT – retrieves a record UPDATE – modifies an existing record INSERT – adds a new record DELETE – deletes an existing record ASP.NET (MacDonald)‏

17 SQL Examples ASP.NET (MacDonald)‏ select id, category from Categories; Select all the records from the Categories table select * from MenuItems where category = 2; Select only the salad records from the MenuItems table

18 ADO.NET Classes  Three types of classes are used in ADO.NET  Connection Classes that encapsulate a connection to a database  Command Classes that are used to execute a command against a database  DataReader Classes that encapsulate the data retrieved from a database ASP.NET (MacDonald, Walther)‏

19 Implementations of ADO.NET Classes  The Connection, Command, and DataReader classes for particular DBMSs are separated into different namespaces  System.Data.SqlClient MS SQL Server 7.0 or higher  System.Data.OleDb Using an OLEDB driver; OleDbConnection, OleDbCommand, OleDbDataReader  System.Data.Odbc Using an ODBC driver  System.Data.OracleClient Oracle 8i or higher ASP.NET (MacDonald, Walther)‏

20 Direct Data Access Steps to query the database with direct data access 1. Create Connection, Command, and DataReader objects 2. Retrieve information from the database with the DataReader object and add it to a page 3. Close the connection 4. Send the page to the user agent as HTML ASP.NET (MacDonald)‏ using System.Data; using System.Data.OleDb; In your C# code, import the correct ADO.NET namespaces

21 Deli Menu Demo ASP.NET (MacDonald)‏  Use ASP.NET and Visual Studio 2008 to build a web application to display the results of a menu search by category using a DropDownList

22 Demo web.config ASP.NET (MacDonald)‏  Define the connection string in the web.config file so that it is available throughout your application NOTE: This is a partial file. Points to the App_Data folder inside your web application directory The alias that you can use in your code

23 Demo Default.aspx.cs ASP.NET (MacDonald)‏ using System; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; using System.Web.Configuration; public partial class _Default : System.Web.UI.Page { private string connectionString = WebConfigurationManager.ConnectionStrings["Deli"].ConnectionString; Use the alias defined in web.config

24 Demo Page_Load ASP.NET (MacDonald)‏ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string selectSql = "SELECT id, category FROM Categories "; OleDbConnection conn = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(selectSql, conn); try { conn.Open(); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ListItem newItem = new ListItem(); newItem.Text = reader["category"].ToString(); newItem.Value = reader["id"].ToString(); categoriesDropDownList.Items.Add(newItem); } reader.Close(); } catch (Exception error) { resultsLabel.Text = "ERROR: " + error.Message; } finally { conn.Close(); } When the page loads, query the Categories table. For each record in the results, add a list item to the DropDownList

25 Demo categoriesDropDownList_SelectedIndexChanged ASP.NET (MacDonald)‏ protected void categoriesDropDownList_SelectedIndexChanged(object sender, EventArgs e) { if (IsPostBack) { string selectSql = "select * from MenuItems where category = " + categoriesDropDownList.SelectedValue; OleDbConnection conn = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(selectSql, conn); OleDbDataReader reader; using (conn) { conn.Open(); reader = cmd.ExecuteReader(); StringBuilder sb = new StringBuilder(); sb.Append(" "); while (reader.Read()) { sb.Append(" "); sb.Append(reader["item"].ToString()); sb.Append(" "); sb.Append(reader["description"].ToString()); sb.Append(" "); sb.Append(reader["price"].ToString()); sb.Append(" "); } sb.Append(" "); resultsLabel.Text = sb.ToString(); } When the DropDownList selection changes, query the MenuItems table For each record that has a category value equal to the value of the list selection, add the record to the table as a row

26 References Ding, Wei, “ASP.NET” UHCL lecture slides, Gittleman, Art, Computing with C# and the.NET Framework. Jones and Bartlett Publishers, Lu, Dennis and Doug Rothaus, Microsoft (2002). “Best Practices for Using ADO.NET”. [Online]. Available: MacDonald, Matthew, Beginning ASP.NET 3.5 in C# 2008: From Novice to Professional, Second Edition. Apress, Walther, Stephen. ASP.NET 3.5 Unleashed. SAMS, W3Schools Online Web Tutorials. “ASP.NET”. [Online]. Available: