web-based programming lanjut pertemuan 8 matakuliah: m0492 / web-based programming lanjut tahun:...

40
Web-based Programming Lanjut Pertemuan 8 Matakuliah : M0492 / Web-based Programming Lanjut Tahun : 2007

Post on 22-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Web-based Programming Lanjut Pertemuan 8

Matakuliah : M0492 / Web-based Programming Lanjut Tahun : 2007

Page 2: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

ASP and Data on the Client

• Disconnected Recordset• Remote Data Services (RDS)• Data Transfer between Server and Client• Recordset Paging

Page 3: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Disconnected Recordset

• A disconnected recordset is just a normal recordset, but its connection to the server removed.

• The recordset stands alone, can act like a normal recordset, allowing updates, additions, and deletions.

• But changes only happen in the recordset, are not reflected in the server.

• The connection to the server can be re-established and the server updated with any changes

• If data on the server has changed, you can detect these changes. This is called conflict resolution.

Page 4: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Remote Data Service (RDS)

• RDS is the name given to the set of services that allows us to handle data on the client.

• RDS is part of ADO• Only comes into use when you need to transfer and use

data at the client• Made up from several components

Page 5: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Remote Data ServerServer

DataFactory

Object

Custom

Component

Web

Server

Data store

OLE-DB

Provider

Client

DataSpace

Object

DataClientCache

DataSourceObject

DataBinding

Manager

Name:

Client:

Network

Page 6: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Remote Data Server

• RDS Server Components– Data store

– OLE DB Provider

– Two option how work with the data• The DataFactory

The default server-side component, provides access to data stores

• A Custom ComponentA normal COM component that has methods to perform data transfer.

– Web Server, acts as the interface between client and the server.

Page 7: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Remote Data Server• RDS Client Components

– DataSpace Object• goes hand-in-hand with the DataFactory or a Custom Component, provides

the client-side part of the equation.• DataSpace is a proxy object, responsible for the communication with the

server, providing the channel through which data is moved.• Created by using an HTML <OBJECT> tag.

– Data Source Object (DSO)• Responsible for storing data on the client, contains an ADO Recordset and

work together with the Client Data Cache to manage the data• Created by using an HTML <OBJECT> tag

– Client Data Cache• The client cursor service managing the data on the client

– Data Binding Manager• Create the link (binding) between the HTML controls and the DSO. Perform by

specifying the DATASRC and DATAFLD attributes of certain HTML controls.

Page 8: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Remote Data Server• Browser Support for RDS

• Only works in Microsoft browsers• Fully supported on Microsoft Internet Explorer 4.0 and

above• Be aware, clients accessing application that rely on

RDS might have different versions with the server. For example, IE4 shipped with RDS 1.5, while IE5, Office2000 and Visual Studio 6 with RDS 2.0, Windows 2000 with RDS 2.5

• Two ways to take care of compability problem :– Make sure all users upgrade to the latest version of

RDS.– Specify the Data Factory Mode when connecting to

data source. Specify which version to use.

Page 9: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Remote Data Server• Data Source Objects

Is a client-side object that stores and manages the data once it is on the client.

Several different DSO :– Tabular Data Control (TDC) is design to manipulate data held in a

tabular or delimited from text files.

– Remote Data Service Data Control is design to connect to OLEDB data stores, and can specify where to connect to as well as which data to return

– Java DataBase Connector is a Java applet that allows connection to data stores through the Java DataBase Control (JDBC).

– Microsoft HTML (MSHTML) DSO takes data marked up in HTML and uses this as the source of the data.

– XML DSO uses XML data, and can be used for either well-structured or arbitrarily structured XML.

Page 10: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Tabular Data Control• TDC is the simplest DSO, useful for small amounts of data that will be read-only.• Created by placing an <OBJECT> tag

<OBJECT CLASSID=“clsid:333C7BC4-460F-11D0BC04-0080C7055A83”

ID=“dsoEmployee” WIDTH=“0” HEIGHT=“0”>

<PARAM NAME=“DataURL” Value=“Employee.csv”>

</OBJECT>

example of data in Comma Separated Values (CSV):

PMA42628M,"Paolo","M","Accorti",13,35,"0877",1992-08-27 00:00:00

PSA89086M,"Pedro","S","Afonso",14,89,"1389",1990-12-24 00:00:00

VPA30890F,"Victoria","P","Ashworth",6,140,"0877",1990-09-13 00:00:00

H-B39728F,"Helen"," ","Bennett",12,35,"0877",1989-09-21 00:00:00

L-B31947F,"Lesley"," ","Brown",7,120,"0877",1991-02-13 00:00:00

F-C16315M,"Francisco"," ","Chang",4,227,"9952",1990-11-03 00:00:00

Page 11: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

RDS Data Control• RDS Data Control provides access to normal data stores, as opposed to flat

files.• Most often used to connect to SQL database to retrieve data from tables

queries or stored procedures• Allow data to be update• Created using an HTML <OBJECT> tag

<OBJECT CLASSID=“clsid:BD96C556-65A3-11D0-983A-00C04FC29E33”

ID=“dsoAuthors” WIDTH=“0” HEIGHT=“0”>

<PARAM NAME=“Connect” Value=“Connection String”>

<PARAM NAME=“Server” Value=“Server Name”>

<PARAM NAME=“SQL” Value=“Query Text”>

</OBJECT>

Page 12: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

RDS Data Control

• Example :

• ADO 2.5 has a new URL property that allows to use file as the source of data.

<OBJECT CLASSID=“clsid:BD96C556-65A3-11D0-983A-00C04FC29E33”

ID=“dsoAuthors” WIDTH=“0” HEIGHT=“0”>

<PARAM NAME=“Connect” Value=“DSN=pubs”>

<PARAM NAME=“Server” Value=“W2000”>

<PARAM NAME=“SQL” Value=“SELECT * FROM Authors”>

</OBJECT>

<OBJECT CLASSID=“clsid:BD96C556-65A3-11D0-983A-00C04FC29E33”

ID=“dsoAuthors” WIDTH=“0” HEIGHT=“0”>

<PARAM NAME=“URL” Value=“DataPage.asp”>

</OBJECT>

Page 13: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

RDS Data Control

• The DataPage.asp file could contain the following VBScript code :

<!-- #include file="dataconn.inc" -->

<%

Dim rsData

Set rsData = Server.CreateObject (“ADODB.Recordset”)

rsData.Open “SELECT * FROM Authors”, strConn

rsData.Save Response, 1 ‘ adPersistXML= 1

rsData.Close

Set rsData = Nothing

%>

Page 14: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

MSHTML Data Control

• MSHTML is rather unusual – is part of IE• Allows to have a data source based on HTML.• MSHTML is not a form that naturally use for data storage• MSHTML could be useful if you have a lot of HTML pages that do

contain some data.• MSHTML data control has a Recordset property, but has no

methods.

• Created by <OBJECT> tag

<OBJECT ID=“dsoAuthors” DATA=“Authors.html” HEIGHT=“0” WIDTH=“0”>

</OBJECT>

Page 15: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

MSHTML Data Control• To be able to use the control, your HTML tag must have an ID

attribute.

• The example above show that the HTML tag names are irrelevant – it’s the ID that is important.

• The above HTML, would generate 2 rows of data, that looked like this:

<DIV ID=“au_id”>172-32-1176</DIV>

<SPAN ID=“au_lname”>White</SPAN>

<H1 ID=“au_fname”>Bob</H1>

<PRE ID=“au_id”>213-46-8915</PRE>

<H2 ID=“au_lname”>Green</H2>

<H1 ID=“au_fname”>Cheryl</H1>

au_id au_lname au_fname

172-32-1176 White Bob

213-46-8915 Green Cheryl

Page 16: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

XML Data Control• The <XML> tag acts like a data control• Use the <XML> tag in one of two ways:

– The first way, use the SRC attribute to specify the location of the data

– Alternatively, embed the XML in the tag

<XML ID=“dsoAuthors” SRC=“Authors.xml”></XML>

<XML ID=“dsoAuthors”> <Authors> <Author>

<au_id>172-32-1176</au_id><au_lname>White</au_lname><au_fname>Johnson</au_fname><phone>408 496-7223</phone><contract>True</contract>

</Author> <Author>

<au_id>213-46-8915</au_id><au_lname>Green</au_lname><au_fname>Marjorie</au_fname><phone>415 986-7020</phone><contract>True</contract>

</Author> </Authors></XML>

Page 17: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Binding• Binding means setting up a relationship between some HTML elements and the

data control.– The data control is responsible for managing the data and supplying the data to

the HTML element– The element displays the data on the screen

• To bind an HTML element to a data source, specify two attributes for it:

– DATASRC, to identify the data control that contains the data. Always put a # in front of the data source name.

– DATAFLD, to identify the field in the data control to bind to. The fields are the names identifying the columns in the data that the data control manages.

– DATAFORMATS, to indicate how the data in the field should formatted, can be either HTML or TEXT. The default is TEXT

Page 18: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Binding<HTML><HEAD><TITLE>RDSSingleBinding.asp</TITLE></HEAD><BODY><H1>Data Binding with RDS<HR></H1><OBJECT CLASSID="clsid:BD96C556-65A3-11D0-983A-00C04FC29E33"

ID="dsoAuthors" WIDTH="0" HEIGHT="0"><PARAM NAME="URL" Value="DataPage.asp"></OBJECT>

<DIV DATASRC="#dsoAuthors" DATAFLD="au_fname"></DIV><DIV DATASRC="#dsoAuthors" DATAFLD="au_lname"></DIV></BODY></HTML>

Page 19: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Binding1. <HTML>2. <HEAD><TITLE>TDCDataFormat.asp</TITLE></HEAD>3. <BODY>4. <H1>Data Formatting in bound data<HR></H1>5. <OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"6. ID="dsoImages" WIDTH="0" HEIGHT="0">7. <PARAM NAME="DataURL" Value="images.csv">8. <PARAM NAME = "UseHeader" VALUE = "TRUE">9. </OBJECT>

10. <TABLE ID="tblData" DATASRC="#dsoImages" BORDER="1">11. <THEAD>12. <TR>13. <TD>Description</TD>14. <TD>Image</TD>15. </TR>16. </THEAD>17. <TBODY>18. <TR>19. <TD><SPAN DATAFLD="Description"></SPAN></TD>20. <TD><SPAN DATAFLD="Image" DATAFORMATAS="HTML"></SPAN></TD>21. </TR>22. </TBODY>23. </TABLE>24. </BODY>25. </HTML>

Page 20: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Binding

Description,ImageThe asian cartoon,<IMG SRC=images\asian_cartoon.gif>The Badz-maru,<IMG SRC=images\badtz-maru.gif>The Businessman,<IMG SRC=images\biz_man.jpg>

Images.csv:

Page 21: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Table Binding

• Table binding differs from single record binding, because we bind to the TABLE element. This allows to see more than one record at a time.

• Table binding has one attribute useful to data binding – DATAPAGESIZE. This determines how many records are shown in the table

Page 22: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Table Binding1. <HTML>2. <HEAD><TITLE>RDSTableBinding.asp</TITLE></HEAD>3. <BODY>4. <H1>Table Data Binding with RDS<HR></H1>5. <OBJECT CLASSID="clsid:BD96C556-65A3-11D0-983A-00C04FC29E33“ 6. ID="dsoData" WIDTH="0" HEIGHT="0">7. <PARAM NAME="URL" Value="DataPage.asp">8. </OBJECT>

9. <TABLE ID="tblData" DATASRC="#dsoData" DATAPAGESIZE="5" >10. <THEAD>11. <TR>12. <TD>au_id</TD>13. <TD>au_fname</TD>14. <TD>au_lname</TD>15. <TD>phone</TD>16. <TD>address</TD>17. <TD>city</TD>18. <TD>state</TD>19. <TD>zip</TD>20. <TD>contract</TD>21. </TR>22. </THEAD>23. <TBODY>24. <TR>25. <TD><INPUT TYPE="TEXT" DATAFLD="au_id"></INPUT></TD>26. <TD><INPUT TYPE="TEXT" DATAFLD="au_fname"></INPUT></TD>27. <TD><INPUT TYPE="TEXT" DATAFLD="au_lname"></INPUT></TD>28. <TD><INPUT TYPE="TEXT" DATAFLD="phone"></INPUT></TD>29. <TD><INPUT TYPE="TEXT" DATAFLD="address"></INPUT></TD>30. <TD><INPUT TYPE="TEXT" DATAFLD="city"></INPUT></TD>31. <TD><INPUT TYPE="TEXT" DATAFLD="state"></INPUT></TD>32. <TD><INPUT TYPE="TEXT" DATAFLD="zip"></INPUT></TD>33. <TD><INPUT TYPE="TEXT" DATAFLD="contract"></INPUT></TD>34. </TR>35. </TBODY>36. </TABLE>37. </BODY>38. </HTML>

Page 23: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Table Binding

Page 24: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Transfer Between Server and Client

• Some problem we need to solve– We need a secure way to retrieve data from a server and

use it on the client– We need a way that allows data to be updated– We don’t want to compromise flexibility

• The way around this problem is to use components.• A component allows us to encapsulate all of our data

access functionality, including the source of the data, and ensures that data are only exposed to Web page.

Page 25: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Transfer Between Server and Client• A server based component

– The component creator uses a programming language that can create COM components specification. For example, Visual Basic(VB) or Visual C++(VC++).

– The client has a programming language or tool (Such as Microsoft Word or ASP) that know how to instantiate and use COM components, by following the rules laid down in the COM specification.

Example – creating Book ComponentStart Visual Basic and create a new ActiveX DLL project, changing the name

of the default class module to BookTitles.Next, change the name of the project to Book.Add a reference that we need in the project.

Page 26: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

1. Option Explicit2. Private mobjConn As ADODB.Connection3. Private mrsBooks As ADODB.Recordset4. 'open the connection and recordset5. Public Sub OpenTitles()6. Set mobjConn = New ADODB.Connection7. Set mrsBooks = New ADODB.Recordset8. 9. mobjConn.ConnectionString="Provider=SQLOLEDB; Data Source=hogwartz; Initial Catalog=pubs;

User ID=sa; “ & _ “Password= letmein"10. mobjConn.Open11. mrsBooks.CursorLocation = adUseClient12. mrsBooks.Open "SELECT * FROM Titles", mobjConn13. End Sub14. 'Close the list15. Public Sub CloseTitles()16. 'Close the recordset17. If Not (mrsBooks Is Nothing) Then18. If mrsBooks.State = adStateOpen Then19. mrsBooks.Close20. End If21. Set mrsBooks = Nothing22. End If

23. 'Close the connection24. If Not (mobjConn Is Nothing) Then25. mobjConn.Close26. Set mobjConn = Nothing27. End If28. End Sub

Page 27: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

30. 'returns the next title31. Public Sub NextTitle(Id As Variant, Title As Variant, Price As Variant, Notes As Variant)

32. 'Ignore errors and more specifically null values in this demo33. On Error Resume Next

34. If IsEOF = True Then35. Err.Raise vbObject + 1, "BookTitles Component", "End of Cursor"36. Exit Sub37. End If38. 39. 'Copy the fields back to the caller40. Id = mrsBooks.Fields("Title_id")41. Title = mrsBooks.Fields("Title")42. Price = mrsBooks.Fields("Price")43. Notes = mrsBooks.Fields("Notes")44. 45. 'Move to the next title46. If Not mrsBooks.EOF Then mrsBooks.MoveNext47. End Sub

48. Public Function IsEOF() As Boolean49. IsEOF = mrsBooks.EOF50. End Function

If you tried defining the NextTitle procedure Parameter using the String and Currency types, you’d get an error. Because the Active Scripting engine only know the Variant type.

Page 28: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Transfer Between Server and Client1. <% Dim objTitles2. Set objTitles = Server.CreateObject("Book.BookTitles")3. %>4. <HTML><HEAD><TITLE>Titles List</TITLE></HEAD>5. <BODY>6. <H1>Book Titles</H1><HR>7. <P>The following books are currently defined in the pubs database:8. <TABLE cellspacing="2" cellpadding="0">9. <TR> <TD bgcolor="#3AC2EF"><STRONG>ID</STRONG></TD>10. <TD bgcolor="#3AC2EF"><STRONG>Title</STRONG></TD>11. <TD bgcolor="#3AC2EF"><STRONG>Price</STRONG></TD>12. <TD bgcolor="#3AC2EF"><STRONG>Notes</STRONG></TD> </TR>13. <TR></TR>14. <%15. Dim strID16. Dim strTitle17. Dim strPrice18. Dim strNotes19. 'Initialize the list20. objTitles.OpenTitles21. 'Process each author22. While objTitles.IsEOF = False23. objTitles.NextTitle strID, strTitle, strPrice, strNotes24. %>25. <TR> <TD bgcolor="#FFFF6C"><%= strID %></TD>26. <TD bgcolor="#FFFF6C"><%= strTitle %></TD>27. <TD bgcolor="#FFFF6C"><%= strPrice %></TD>28. <TD bgcolor="#FFFF6C"><%= strNotes %></TD></TR>29. <% Wend %>30. </TABLE></BODY></HTML>

Page 29: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Transfer Between Server and Client

Page 30: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Data Transfer Between Server and Client

– Advantages of using Server Side Components• There are no connection details visible to the user – which

gives you a securer system. The component also has to be specially registered, so there is the added security restriction.

• You can build components in any COM compliant language. We use VB in this example, because it’s easy to understand.

• Your components isn’t limited to just returning and updating recordsets but you could build in lots of logic.

• You could encapsulate all of that conflict resolution code in the Recordset method, and just return details of any errors. This means your client-side script is neater and users can’t rip off your hard-worked code.

Page 31: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Recordset Paging• Using ADO Paging1. <%2. Dim rsData3. Dim intPage4. Dim intTotalPages5. Dim fldF6. Dim intRec7. Dim strQuote8. Dim strScriptName9. Dim strConn

10. strConn = " Provider = SQLOLEDB; Data Source= Hogwartz; Initial Catalog= pubs; User ID= sa; Password=letmein"

11. strQuote = Chr(34) 'the double quote character

12. Set rsData = Server.CreateObject("ADODB.Recordset")

13. 'set the page size14. rsData.PageSize = 1015. rsData.CursorLocation = 3 'adUseClient=3

16. 'Open the data17. rsData.Open "Authors", strConn,0,1,2 'adOpenForwardOnly=0, adLockReadOnly=1, adCmdTable=2

Page 32: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

18. If Request.QueryString("PAGE")="" Then19. intPage=120. Else21. intPage = CInt( Request.QueryString("PAGE") )22. If intPage < 1 then23. intPage=124. Else25. If intPage > rsData.PageCount Then26. intPage = rsData.PageCount27. Else28. intPage = CInt( Request.QueryString("PAGE") )29. End If30. End If31. End If

32. rsData.AbsolutePage = intPage33. Response.Write "Page = " & rsData.AbsolutePage & "<BR>"

34. Response.Write "<TABLE BORDER=1><THEAD><TR>"35. For Each fldF In rsData.Fields36. Response.Write "<TD>" & fldF.Name & "</TD>"37. Next38. Response.Write "</TR></THEAD><TBODY>"

39. For intRec = 1 to rsData.PageSize40. If Not rsData.EOF Then41. Response.Write "<TR>"42. For Each fldF In rsData.Fields43. Response.Write "<TD>" & fldF.Value & "</TD>"44. Next45. Response.Write "</TR>"46. rsData.MoveNext47. End If48. Next49. Response.Write "</TBODY></TABLE></P>"

Page 33: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

50. strScriptName = Request.ServerVariables("SCRIPT_NAME")51. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE=1" & strQuote & "> First Page

</A>"

52. 'Only give an active previous page if there are previous page53. If intPage= 1 Then54. Response.Write "&nbsp;<SPAN>Previous Page</SPAN>"55. Else56. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE="& intPage - 1 &

strQuote & "> Previous Page </A>"57. End If

58. 'Only give an active next page if there are more pages59. If intPage = rsData.PageCount Then60. Response.Write "&nbsp;<SPAN>Next Page</SPAN>"61. Else62. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE="& intPage + 1 &

strQuote & "> Next Page </A>"63. End If

64. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE=" & rsData.PageCount & strQuote & "> LastPage </A>"

65. rsData.Close66. Set rsData =Nothing67. %>

Page 34: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Recordset Paging

Page 35: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Recordset Paging

• Using SQL ServerCREATE PROCEDURE usp_PagedAuthors

@iPage int,@iPageSize int

ASBEGIN

SET NOCOUNT ON -- disable row counts

-- declare variablesDECLARE @iStart int -- start recordDECLARE @iEnd int -- end recordDECLARE @iPageCount int -- total number of pages

CREATE TABLE #PagedAuthors -- create the temporary table(

ID int IDENTITY,au_id varchar(11) NOT NULL,au_lname varchar(40) NOT NULL,au_fname varchar(20) NOT NULL,phone char(12) NOT NULL,address varchar(20) NULL,city varchar(20) NULL,state char(2) NULL,zip char(5) NULL,contract bit NOT NULL

)

Page 36: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

-- populate the temporary tableINSERT INTO #PagedAuthors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract)SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contractFROM authors

-- work out how many pages there are in totalSELECT @iPageCount = COUNT(*)FROM authors

SELECT @iPageCount = 3

-- check the page numberIF @iPage < 1

SELECT @iPage = 1IF @iPage > @iPageCount

SELECT @iPage = @iPageCount

-- calculate the start and end recordsSELECT @iStart = (@iPage - 1) * @iPageSizeSELECT @iEnd = @iStart + @iPageSize +1

-- select only those records that fall within our pageSELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contractFROM #PagedAuthorsWHERE ID > @iStartAND ID < @iEnd

DROP TABLE #PagedAuthors

SET NOCOUNT OFF -- turn back on record counts

RETURN @iPageCount END

Page 37: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Recordset Paging1. <HTML><HEAD><TITLE>SQL Paging</TITLE></HEAD>2. <BODY>3. <%4. Dim rsData5. Dim intPage6. Dim intLastPage7. Dim fldF8. Dim cmdAuthors9. Dim strQuote10. Dim strScriptName11. Dim strConn

12. strConn="Provider=SQLOLEDB;Data Source=Hogwartz;Initial Catalog=pubs;User ID=sa; Password=letmein"

13. strQuote = Chr(34) 'the double quote character

14. If Request.QueryString("PAGE")="" Then15. intPage=116. Else17. intPage = CInt( Request.QueryString("PAGE") )18. If intPage < 1 then19. intPage=120. End If21. End If

22. Set cmdAuthors = Server.CreateObject("ADODB.Command")23. Set rsData = Server.CreateObject("ADODB.Recordset")

Page 38: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

24. With cmdAuthors25. .ActiveConnection = strConn26. .CommandText = "usp_PagedAuthors"27. .CommandType = 4 'adCmdStoredProc=4

28. .Parameters.Append .CreateParameter("RETURN_VAL", 3, 4) 'adInteger=3 adParamReturnValue=4

29. .Parameters.Append .CreateParameter("@iPage", 3, 1, 8, intPage) 'adInteger=3 adParamInput=130. .Parameters.Append .CreateParameter("@iPageSize", 3, 1, 8, 10) 'adInteger=3 adParamInput=1

31. Set rsData = .Execute32. .Execute

33. intLastPage = .Parameters("RETURN_VAL")34. End WIth

35. Response.Write "Page = " & intPage & "<BR>“

36. Response.Write "<TABLE BORDER=1><THEAD><TR>"37. For Each fldF In rsData.Fields38. Response.Write "<TD>" & fldF.Name & "</TD>"39. Next40. Response.Write "</TR></THEAD><TBODY>"

41. For intRec = 1 to rsData.PageSize42. If Not rsData.EOF Then43. Response.Write "<TR>"44. For Each fldF In rsData.Fields45. Response.Write "<TD>" & fldF.Value & "</TD>"46. Next47. Response.Write "</TR>"48. rsData.MoveNext49. End If50. Next51. Response.Write "</TBODY></TABLE></P>"

Page 39: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

52. strScriptName = Request.ServerVariables("SCRIPT_NAME")53. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE=1" & strQuote & ">

First Page </A>"

54. 'Only give an active previous page if there are previous page55. If intPage= 1 Then56. Response.Write "&nbsp;<SPAN>Previous Page</SPAN>"57. Else58. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE="& intPage

- 1 & strQuote & "> Previous Page </A>"59. End If

60. 'Only give an active next page if there are more pages61. If intPage = intLastPage Then62. Response.Write "&nbsp;<SPAN>Next Page</SPAN>"63. Else64. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE="& intPage

+ 1 & strQuote & "> Next Page </A>"65. End If

66. Response.Write "&nbsp;<A HREF=" & strQuote & strScriptName & "?PAGE=" & intLastPage & strQuote & "> LastPage </A>"

67. rsData.Close68. Set rsData =Nothing69. Set cmdAuthors = Nothing70. %>71. </BODY>72. </HTML>

Page 40: Web-based Programming Lanjut Pertemuan 8 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007

Bina Nusantara

Recordset Paging