[Tutor] Re: ASP examples

Sam Corder samus@feudalkingdoms.tzo.org
Thu, 17 May 2001 14:48:09 +0000


NHYTRO@compuserve.com wrote:
>
>Message text written by INTERNET:samus@feudalkingdoms.tzo.org
>>Are there any specific types of examples you would like to see?  I'll po=
>st
>what I =
>can to the tutor list when I get your reply.  I have code that uses ado,
>the =
>session object and a couple of the other asp objects.  Most of the stuff =
>I
>write in =
>asp interacts with specific classes and com components that I have writte=
>n.
> The =
>asp is just the bare bones presentation logic.
>-Sam<
>Thanks Sam for replying!
>I really would like to see the use of ADO, Sessions and IE manipulation.
>best regards
>Sharriff

Heres a quick example of ADO.  If you want to call stored procs then you will need 
the line cmd.CommandType = constants.adCmdStoredProc somewhere before rs.Open.  The 
default is adCmdText.

import win32com.client
constants = win32com.client.constants

cmd = win32com.client.Dispatch('ADODB.Command')
rs = win32com.client.Dispatch('ADODB.Recordset')

cmd.ActiveConnection = connectstr
cmd.CommandText = sql
rs.Open(cmd, CursorType = constants.adOpenForwardOnly, LockType = 
constants.adLockReadOnly)

You can also do rs=cmd.Execute() and pass any optional parameters.  Execute may 
return things in a tuple.  I forget since I don't use it often in Python.  You can 
take a look at http://groups.google.com/groups?
hl=en&lr=lang_en&newwindow=1&safe=off&ic=1&th=c9f66eafa42195a1,4&seekm=CPEKKMGLLILAJ
EKJNAPDAEEBCBAA.scorder%40incigna.com#p for a basic python class that has the 
startings of an encapsulation of this stuff.  I never went very far with this class 
because we usually use a vb dll that has business and security logic in it to 
access our databases.  Its still pretty similar.  The Python code just gets 
shorter.  Heres a code fragment that illustrates this.
        sacsApp = win32com.client.Dispatch('SACS.Application')
        sacsApp.Initialize("sacsowner")
        sr = sacsApp.Reports
        rs = win32com.client.Dispatch('ADODB.Recordset')
        rs = sr.StateFilingStatus(CompanyList, StateList, EditionDate, FormNumber, 
Plancode, FormType, FormUserList)

The first rs = is probably not necessary because the last method returns a 
recordset.  For your vb dlls make sure that the parameters passed in are declared 
as byval.  Default is byref which for com means its an in/out parameter and python 
will return a tuple back to you.

Here is getting some data out by using GetRows.  GetRows returns stuff back weirdly 
so we have to rotate the rows around.  
        rows = rs.GetRows(1000)        
        #First rotate the cols to rows and rows to cols
        rows = zip(rows[0],rows[1],rows[2],rows[3],rows[4],rows[5],rows[6],rows
[7],rows[8],rows[9],rows[10],rows[11])

If you just want to spit out a table or something then I would suggest using 
GetString and then regular expressions to format the the table nicer.  Its much 
faster than looping through a result set and doing Response.write(blahblahblah)

Here are some asp object uses.

This code writes out a session variable
Response.Write(Session.Contents.Item("UserName"))

Thats about the jist of it all.  The main thing that gets me all the time is the 
lack of "shortcuts" that vbscript has.  These are the default methods of objects.  
Python doesn't pay any attention to them.  I.E in vbscript the same session 
expression above would be session("UserName").  You can find these by using a com 
object browser or looking at the platform sdk documentation on msdn.microsoft.com

As for IE manipulations thats best done in javascript on the client side.  One 
thing we do is based on a page and a persons access writes we will generate some 
form validation javascript that gets called in an onSubmit event of a form.  For 
you to do IE manipulation in python the client browser will need the python stuff 
installed and configured.  Hope all this helps.

-Sam