Welcome <% Session.Timeout=5 %> The example below sets a timeout interval of 5 minutes:"> Welcome <% Session.Timeout=5 %> The example below sets a timeout interval of 5 minutes:">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ASP Application Development Session 3. Topics Covered Using SQL Statements for: –Inserting a tuple –Deleting a tuple –Updating a tuple Using the RecordSet."— Presentation transcript:

1 ASP Application Development Session 3

2 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

3 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:

4 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.

5 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……

6 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

7 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.

8 Cursor Types

9 Lock Types

10 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)

11 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

12 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

13 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

14 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"

15 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

16 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.

17 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"

18 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

19 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.

20 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

21 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

22 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


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

Similar presentations


Ads by Google