ASP Application Development Session 3. Topics Covered Using SQL Statements for: –Inserting a tuple –Deleting a tuple –Updating a tuple Using the RecordSet.

Slides:



Advertisements
Similar presentations
ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access.
Advertisements

Web Database Programming Connecting Database to Web.
CIS 375—Web App Dev II ADO I. 2 Introduction ADO (________ Data Objects) is a Microsoft technology for accessing data in a database. ADO is automatically.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Session 6 Server-side programming - ASP. An ASP page is an HTML page interspersed with server-side code. The.ASP extension instead of.HTM denotes server-side.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.
Database Connectivity Session 2. Topics Covered ADO Object Model Database Connection Retrieving Records Creating HTML Documents on-the-fly.
Python and Web Programming
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Master Detail Page Set Enabling Users to See More Detailed Data.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”
Figure 1. Hit analysis in 2002 of database-driven web applications Hits by Category in 2002 N = 73,873 Results Reporting 27% GME 26% Research 20% Bed Availability.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
ADO Recordsets. Recordset Objects Similar to Tables and Queries: data Using VBA/VBScript you… –Open a recordset, –Locate a record –Update or add a record.
Interacting With Data Databases.
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan
Database 20/2/12 Connection. 
Server Side Programming ASP1 Server Side Programming Database Integration (cont.) Internet Systems Design.
CIS 375—Web App Dev II ASP II. 2 ASP Session: Introduction The Session _______ is used to store information about, or change settings for a user session.
Creating a Web Site to Gather Data and Conduct Research.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Stored Procedures, Transactions, and Error-Handling
Form and Graphical User Interfaces. Lesson plan More about queries More about entering data into a table Form.
UNIT 9.2: Learning Objectives Agile Development – Bruce Feiler on Agile Programming Database access from code – Database Cycle Review – SQL Command Types.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Lecture Note 10: Simple Database Techniques. Introduction –Database System –Access, SQL Server and others. –Microsoft Access - Interacting with this databases.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through.
ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Rev.04/2015© 2015 PLEASE NOTE: The Application Review Module (ARM) is a system that is designed as a shared service and is maintained by the Grants Centers.
Paging Through a Data Table Creating a Navigation Bar Using the Recordset Navigation Bar Server Behavior.
Lecture Note 8: ASP Including Files and The Global.asa file.
Lecture Note 7: Sessions and Applications Object.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Database Connectivity What is ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a Microsoft Active-X component.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
© 2007 by Prentice Hall2-1 Introduction to Oracle 10g Chapter 2 Overview of SQL and SQL*Plus James Perry and Gerald Post.
Maintaining State in ASP. Problem - How do I maintain state information about the user  Several Methods –Cookies –Session variables –Hidden fields 
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.
Using a Database Access97 Please use speaker notes for additional information!
Labtest.ASP Notes. INSERT STATUS INSERT STATUS
ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson.
January 27, 2001Database Tutorial1 Using a Database in ASP.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
1 ADO Activex Data Objects. 2 ADO ADO allows users to access data easily from many existing databases (such as Access or Paradox) From ODBC compliant.
Using databases ActiveX Data Objects (ADO) Connecting to a database Reading data from a database Inserting, updating and deleting records Using databases.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Emdeon Office Batch Management Services This document provides detailed information on Batch Import Services and other Batch features.
ASP.NET Programming with C# and SQL Server First Edition
ADO VBA Programming in Access
A Guide to SQL, Seventh Edition
INT213 Updating the Database.
Chapter 6 Displaying Your Products
Chapter 10 ASP and Data Store Access
Cookies A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer.
Presentation transcript:

ASP Application Development Session 3

Topics Covered Using SQL Statements for: –Inserting a tuple –Deleting a tuple –Updating a tuple Using the RecordSet Object for: –Inserting a tuple –Deleting a tuple –Updating a tuple

ASP Session Object The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application <% Session("username")=“JoeShmoe" Session("age")=23 %> Welcome <% Session.Timeout=5 %> The example below sets a timeout interval of 5 minutes:

ASP Application Object A group of ASP files that work together to perform some purpose is called an application. The Application object in ASP is used to store variables and access variables from any page, just like the Session object. The difference is that all users share ONE Application object, while with Sessions there is one Session object for each user. <% Sub Application_OnStart application("vartime")="" application("users")=1 End Sub %> There are <% Response.Write(Application("users")) %> active connections. In the example we have created two Application variables: "vartime" and "users". You can access the value of an Application variable like this.

Controlling Application Behavior You can create Application variables in "Global.asa" ASP The Global.asa file: –The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application ASP Including Files : –The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages Hello……

RecordSet Object Recordset objects can support two types of updating: Recordset objects can support two types of updating: –Immediate updating all changes are written immediately to the database once you call the Update method –Batch updating the provider will cache multiple changes and then send them to the database with the UpdateBatch method

Cursor Types In ADO there are 4 different cursor types defined: –Forward-only cursor Allows you to only scroll forward through the Recordset. Additions, changes, or deletions by other users will not be visible. Allows you to only scroll forward through the Recordset. Additions, changes, or deletions by other users will not be visible. –Keyset cursor Like a dynamic cursor, except that you cannot see additions by other users, and it prevents access to records that other users have deleted. Data changes by other users will still be visible. –Dynamic cursor Allows you to see additions, changes, and deletions by other users. –Static cursor Provides a static copy of a recordset for you to use to find data or generate reports. Additions, changes, or deletions by other users will not be visible. This is the only type of cursor allowed when you open a client-side Recordset object.

Cursor Types

Lock Types

Inserting a Tuple using SQL Statement User inputs new data in a form Gather that data and create an SQL statement Open connection to the database Execute the SQL statement Syntax for SQL statement: –insert into tablename (attribute list) Values (actual values) –Example insert into tblStudent (StudentNo, FirstName, LastName, Year, Major) values (45, ‘Joe’, ‘Shmoe’, 2000, MIS) insert into tblStudent values (45, ‘Joe’, ‘Shmoe’, 2000, MIS)

Inserting a Tuple – Form to input data Student Information Form Please enter the following student information and click on the Add button. Student Number First Name Last Name Year Major Call the ASP page Text Boxes for attributes Submit button to send the values Demo the Example

Inserting a Tuple – ASP Page Get Product from Form <% 'get the user entered data sn=request.form("sno") fn=request.form("fname") ln=request.form("lname") yr=request.form("year") mj=request.form("major") 'Establish the connection with the admin database set my_conn= Server.CreateObject("ADODB.Connection") 'Construct the connection string using relative path for the database file myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & _ server.mappath("db/admin.mdb") 'Open the connection to the data source my_conn.Open myvar 'create sql query using the user entered data StrSql= "insert into tblStudent (StudentNo, FirstName, LastName, Year, Major) values (" & _ sn & ",'" & fn & "','" & ln & "'," & yr & ",'" & mj & "');" 'continued on the next slide Get the user input and store in variables DSN-less connection string Construct the DML statement using the variables that contain the user values String values are delimeted using single quotes Need to add comma to separate each value

Inserting a Tuple – ASP Page (continued) 'print out the SQL query to see if it is constructed properly response.write " SQL Statement Created: &nbsp&nbsp&nbsp" & StrSql 'execute the query which will insert the tuple into the table tblStudent my_conn.Execute (StrSql) 'display message saying the typle is inserted response.write " Inserted the tuple for " & fn & " " & ln & " " my_conn.Close %> Click on this link to see if the student has been added. For debugging purposes, it is a good idea to print out the sql statement that is constructed Execute the SQL statement Just a confirmation message Call another ASP page that lists all the student names in the table to make sure the tuple was actually inserted. Don’t have to do this in your application

Deleting a Tuple User identifies the tuple to delete Construct the delete SQL statement Execute the SQL statement Syntax of delete statement –DELETE From Tablename WHERE primarykey = value –Example "DELETE FROM tblStudent WHERE StudentNo = 87"

Deleting a Tuple Example – Get User Input Get Product from Form <% 'Establish the connection with the nowrthwind database set my_conn= Server.CreateObject("ADODB.Connection") 'Construct the connection string using relative path for the database file myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & server.mappath("db/admin.mdb") 'Open the connection to the data source my_conn.Open myvar 'Get the student information from the tblStudent table StrSql= "Select * from tblStudent" set rs = my_conn.Execute (StrSql) %> Deleting a Student from tblStudent Table Please select a student and click on the Delete Student button to remove the student. Select a student Name: &nbsp "> &nbsp <% rs.movenext loop my_Conn.Close set my_conn = nothing %> Create the drop-down list called “student” For each option, the value is set as the StudentNo, which will be sent to the next ASP page Demo the Example

Deleting a Tuple Example – ASP page Delete a Tuple <% 'get the student number that was selected selected_id = request.form("student") 'Establish the connection with the admin database set my_conn= Server.CreateObject("ADODB.Connection") 'Construct the connection string using relative path for the database file myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & _ server.mappath("db/admin.mdb") 'Open the connection to the data source my_conn.Open myvar 'create the sql query using the user entered data for student number StrSql= "DELETE FROM tblStudent WHERE StudentNo = " & selected_id 'print out the SQL query to see if it is constructed properly response.write " SQL Statement Created: &nbsp&nbsp&nbsp" & StrSql 'execute the query which will insert the tuple my_conn.Execute (StrSql) my_conn.Close %> Click on this link to see if the student has been deleted.

Updating a Tuple User identifies the tuple to update Get the new values for attributes Create the appropriate SQL statement with new values Execute the SQL statement Syntax of update statement UPDATE Tablename SET UPDATE Tablename SET AttributeName = new value AttributeName = new value WHERE primarykey = value WHERE primarykey = value –Example UPDATE tblStudent SET LastName = “Shmoe” UPDATE tblStudent SET LastName = “Shmoe” WHERE StudentNo = 87" WHERE StudentNo = 87"

Updating a Tuple - Example <% set conn=Server.CreateObject("ADODB.Connection") myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & _ server.mappath("db/admin.mdb") conn.Open myvar strsql = "SELECT * FROM tblStudent" set rs = conn.execute(strsql) %> Student Information <% for each x in rs.Fields response.write(" " & ucase(x.name) & " ") next %> <% for each y in rs.Fields if y.name="StudentNo" then%> "> <%end if Next rs.MoveNext%> <% loop conn.close %> Conection string Get all the tuples from the tblStudent table and create the recordset object “rs” Create a table and output the attribute names as the first row of the table Subsequent rows contain values for each student. First field in each row of the table corresponds to StudentNo and contains a form with a submit button. When clicked, calls the next ASP page Output the values of other attributes in each row of the table. End of the do … until loop Demo the Example

Updating a Tuple Continued (ASP Page) Update Student Record <% set conn=Server.CreateObject("ADODB.Connection") myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & _ server.mappath("db/admin.mdb") conn.Open myvar sid=Request.Form("StudentNo") strsql = "SELECT * FROM tblStudent WHERE StudentNo=" & sid if Request.form("FirstName")="" then set rs = conn.execute(strsql) %> " value=" "> <% else sql="UPDATE tblStudent SET " sql=sql & "FirstName='" & Request.Form("FirstName") & "'," sql=sql & "LastName='" & Request.Form("LastName") & "'," sql=sql & "Year=" & Request.Form("Year") & "," sql=sql & "Major='" & Request.Form("Major") & "'" sql=sql & " WHERE StudentNo=" & sid on error resume next conn.Execute sql end if conn.close %> The first time this page is called, it will have value for only “StudentNo” If first time, execute sql and retrieve information for the selected sid. Create a form with text fields to edit Output the field name followed by a text box with the value. User can edit the values and click on submit. The same ASP page is called again The second time around, the text fields will have the new values. Construct an Update statement using those values and execute that query.

Adding A Record Through RecordSet Add a Record <% 'Establish the connection with the admin database set my_conn= Server.CreateObject("ADODB.Connection") 'Construct the connection string myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & _ server.mappath("db/admin.mdb") 'Open the connection to the data source my_conn.Open myvar Dim oRS Set oRS=Server.CreateObject("ADODB.Recordset") oRS.CursorType = 2 oRS.LockType = 3 strsql = "select * from tblStudent" oRS.Open strsql, my_conn oRS.AddNew oRS.Fields("StudentNo")=Request.Form("sno") oRS.Fields("FirstName")=Request.Form("fname") oRS.Fields("LastName")=Request.Form("lname") oRS.Fields("Year")=Request.Form("year") oRS.Fields("Major")=Request.Form("major") oRS.Update 'display message saying the typle is inserted response.write " Added the record for " & Request.Form("fname") & _ " " & Request.Form("lname") & " " oRS.Close my_conn.Close %> Click on this link to see if the student has been added. Create an instance of RecordSet object called oRS and set the appropriate CursorType and LockType Execute SQL statement and get the current records from the table Add a new record to the recordset object Provide values for each of the attributes Update the recordset object which then updates the base table Demo the Example

Deleting a Record using RecordSet Object Delete a Tuple <% 'get the student number that was selected selected_id = request.form("student") 'Establish the connection with the admin database set my_conn= Server.CreateObject("ADODB.Connection") 'Construct the connection string myvar = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & _ server.mappath("db/admin.mdb") 'Open the connection to the data source my_conn.Open myvar Dim oRS Set oRS=Server.CreateObject("ADODB.Recordset") oRS.CursorType = 2 oRS.LockType = 3 strsql = "select * from tblStudent" oRS.Open strsql, my_conn oRS.Find "StudentNo=" & selected_ID oRS.delete my_conn.Close %> Click on this link to see if the student has been deleted. Demo the Example Create the RecordSet object oRS and set The appropriate cursor type and lock type Get the selected StudentNo and store it in a variable Open the oRS recordset object by executing the SQL statement (strsql) through the connection object (my_conn) Find the record with the selected StudentNo The current record pointer points to that record Delete that record by calling the delete method

ASP Exercise 3 Create an ASP application to manage a table Use the Employee table in exercise3.mdb exercise3.mdb Initial page with choices for viewing the data, adding, deleting, or updating a record View option –Call an ASP page to display the records in the employee table Add option –Call an ASP page for entering employee data –Call another ASP page for adding the record to the employee table Delete option –Call an ASP page to indicate the record to be deleted –Call an ASP page to delete the record Update option –Call an ASP page to indicate the record to be updated –Call an ASP page to update the record