ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP Source Code:
<!-- #include file="adovbs.inc" -->
<%
' ADO constants included above.  Questions about adovbs.inc?
' See "What is Adovbs.inc and Why Do I Need It?"
'   http://www.asp101.com/articles/john/adovbs/default.asp
Dim strConnString  ' SQL Server connection string
Dim cnnDbSort      ' ADO Connection object
Dim rstDbSort      ' ADO Recordset object
Dim strSqlQuery    ' Our SQL query
Dim strSortField   ' Field to sort by
Dim strSortOrder   ' "ASC" or "DESC"
Dim objField       ' Used for table display
Dim blnColor       ' Alternating color indicator
' Set our connection string
strConnString = "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
	& "Initial Catalog=samples;User Id=samples;Password=password;" _
	& "Connect Timeout=15;Network Library=dbmssocn;"
' Retrieve sorting parameters:
' Get the field name and make sure the input is one of our field names.
strSortField = Request.QueryString("field")
Select Case LCase(strSortField)
	Case "last_name", "first_name", "sales"
		strSortField = strSortField
	Case Else
		strSortField = "last_name"
End Select
' Check for descending o/w we default to ascending
Select Case LCase(Request.QueryString("order"))
	Case "desc"
		strSortOrder  = "desc"
	Case Else
		strSortOrder  = "asc"
End Select
' Build our SQL query
strSqlQuery = "SELECT * FROM [sample] ORDER BY [" & strSortField & "] " & strSortOrder & ";"
' Open connection
Set cnnDbSort = Server.CreateObject("ADODB.Connection")
cnnDbSort.Open strConnString
' Get recordset
Set rstDbSort = Server.CreateObject("ADODB.Recordset")
rstDbSort.Open strSqlQuery, cnnDbSort
' Build our table:
' Start the table
Response.Write "<table border=""1"" cellspacing=""0"">" & vbCrLf
' Write titles and include links to sort the table by each field
Response.Write vbTab & "<tr>" & vbCrLf
For Each objField in rstDbSort.Fields
	If objField.Name <> "id" Then
		Response.Write vbTab & vbTab & "<td bgcolor=""#CCCCCC""><strong>"
		If objField.Name = strSortField And strSortOrder = "asc" Then
			Response.Write "<a href=""?field=" & objField.Name & "&order=desc"">" & objField.Name & "</a>"
		Else
			Response.Write "<a href=""?field=" & objField.Name & "&order=asc"">" & objField.Name & "</a>"
		End If
		Response.Write "</strong>"
		If objField.Name = strSortField Then
			If LCase(strSortOrder) = "asc" Then
				Response.Write " <img src=""images/arr_up.gif"" width=""9"" height=""7"" border=""0"" alt=""Up Arrow"">"
			Else
				Response.Write " <img src=""images/arr_dn.gif"" width=""9"" height=""7"" border=""0"" alt=""Down Arrow"">"
			End If
		End If
		Response.Write "</td>" & vbCrLf
	End If
Next 'objField
Response.Write vbTab & "</tr>" & vbCrLf
' Display the data
blnColor = False
rstDbSort.MoveFirst
Do While Not rstDbSort.EOF
	'Response.Write rstDbSort.Fields(0).Value & "<br />" & vbCrLf
	Response.Write vbTab & "<tr>" & vbCrLf
	For Each objField in rstDbSort.Fields
		If objField.Name <> "id" Then
			Response.Write vbTab & vbTab & "<td bgcolor="""
			' Decide what color to output
			If blnColor Then
				Response.Write "#CCCCFF"  ' Light blueish
			Else
				Response.Write "#FFFFFF"  ' White
			End If
			Response.Write """><a title=""Salesman ID: " & rstDbSort.Fields("id").Value & """>" _
				& Trim(objField.Value) & "</a></td>" & vbCrLf
		End If
	Next 'objField
	Response.Write vbTab & "</tr>" & vbCrLf
	' Toggle our colors
	blnColor = Not blnColor
	rstDbSort.MoveNext
Loop
' End the table
Response.Write "</table>" & vbCrLf
' Close data access objects and free variables
rstDbSort.Close
Set rstDbSort = Nothing
cnnDbSort.Close
Set cnnDbSort = Nothing
%>

Back the the Sample Output

Back to the Sample Index


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers