p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

proasp_codeclinic thread: Problem calling function from more than one place on a page ???

Message #1 by "Tony Cooper" <tony.cooper@h...> on Fri, 8 Mar 2002 16:16:46
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

Content-Type: text/plain

I have a suggestion. Take your Ultradev CD and put it in the microwave on
High for 10 minutes. Now go to your computer and uninstall Ultradev. Try
using Notepad, Textpad, Interdev, Ultraedit or something that doesn't
generate code for you.

More seriously, I am not going to read through your code, which is largely
illegible (thanks Ultradev) trying to find your error. However I can point
you in the direction of a good place to start. Try commenting out all of the
code in the function, and instead adding a Response.Write statement to do a
screen dump the values passed in. Debug from there using more Response.Write
statements. You might also find Response.End is helpful.

David Cameron

-----Original Message-----
From: Tony Cooper [mailto:tony.cooper@h...]
Sent: Saturday, 9 March 2002 3:17 AM
To: Code Clinic
Subject: [proasp_codeclinic] Problem calling function from more than one
place on a page ???


I'm developing an asp page that needs to insert Person and Org records 
into an existing application. This app. uses a Guid as the ID for most of
its records.

Sample layout of tables and form:
Record - Person
Per_ID    (guid)
Org_ID    (guid)
Record Org
Org_ID    (guid)
Per_ID    value="<%=getguid%>"
I've enclosed an shorten version of the code below ( hope I haven't cut 
out anything that's relevant).

Basically, during the insert phase I check to see if there is an existing 
Org record, by reading Org record with the Request.Form("OrgName"). If so 
I use that Org_ID, if not I need to create a new Org record and set a new 
However the insert fails. I tracked it down the the fact that the function
getguid is being called twice. However, it's not the fact that the function
is called again, rather the content. If I hard code a string to be 
returned, everything works fine.
Can you help?


<!--#include file="Connections/eFOSQL.asp" -->
' *** Edit Operations: declare variables

MM_editAction = CStr(Request("URL"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Request.QueryString
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) <> "") Then

  MM_editConnection = MM_eFOSQL_STRING
  MM_editTable = "dbo.Person"
  MM_editRedirectUrl = ""
= "FirstName|value|LastName|value|Group_ID|value|Title|value|Per_ID|value|P
= "FirstName|',none,''|LastName|',none,''|Group_ID|',none,''|Title|',none,'

  ' create the MM_fields and MM_columns arrays
  MM_fields = Split(MM_fieldsStr, "|")
  MM_columns = Split(MM_columnsStr, "|")

  ' set the form values
  For i = LBound(MM_fields) To UBound(MM_fields) Step 2
  ' This is a little dangerous as this ASSUMES that Org_ID is in position 
12 on the screen !!!
    If i = 12 Then 
        MM_hOrgID = GetOrgID(Request.Form("OrgName"))
        MM_fields(i+1) = MM_hOrgID
      MM_fields(i+1) = CStr(Request.Form(MM_fields(i)))
  End If

  ' append the query string to the redirect URL
  If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
    If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And 
Request.QueryString <> "") Then
      MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
      MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
    End If
  End If

End If
' *** Insert Record: construct a sql insert statement and execute it

If (CStr(Request("MM_insert")) <> "") Then

  ' create the sql insert statement
  MM_tableValues = ""
  MM_dbValues = ""
  For i = LBound(MM_fields) To UBound(MM_fields) Step 2
    FormVal = MM_fields(i+1)
    MM_typeArray = Split(MM_columns(i+1),",")
    Delim = MM_typeArray(0)
    If (Delim = "none") Then Delim = ""
    AltVal = MM_typeArray(1)
    If (AltVal = "none") Then AltVal = ""
    EmptyVal = MM_typeArray(2)
    If (EmptyVal = "none") Then EmptyVal = ""
    If (FormVal = "") Then
      FormVal = EmptyVal
      If (AltVal <> "") Then
        FormVal = AltVal
      ElseIf (Delim = "'") Then  ' escape quotes
        FormVal = "'" & Replace(FormVal,"'","''") & "'"
        FormVal = Delim + FormVal + Delim
      End If
    End If
    If (i <> LBound(MM_fields)) Then
      MM_tableValues = MM_tableValues & ","
      MM_dbValues = MM_dbValues & ","
    End if
    MM_tableValues = MM_tableValues & MM_columns(i)
    MM_dbValues = MM_dbValues & FormVal
  MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues 
& ") values (" & MM_dbValues & ")"

  If (Not MM_abortEdit) Then
    ' execute the insert
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery

    If (MM_editRedirectUrl <> "") Then
    End If
  End If

End If
set ExtPP = Server.CreateObject("ADODB.Recordset")
ExtPP.ActiveConnection = MM_eFOSQL_STRING
ExtPP.Source = "SELECT Person.*, Org.OrgName  FROM dbo.Person, Org  WHERE 
Per_ID = 'XXX' AND Person.Org_ID = Org.Org_ID"
ExtPP.CursorType = 0
ExtPP.CursorLocation = 2
ExtPP.LockType = 3
ExtPP_numRows = 0
<title>CRM - Person Details ADD</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body bgcolor="#00CCCC" leftmargin="0" topmargin="0">
<noscript>Your browser does not support script</noscript> 
      <form name="formUPD" method="POST" action="<%=MM_editAction%>">
        <table width="100%" border="1">
            <td><input type="text" name="FirstName" size="30" ></td>
            <td><input type="text" name="LastName" size="30" ></td>
            <td><input type="hidden" name="Group_ID" size="32" 
            <td><input type="text" name="Title" size="40" 
            <td><input type="text" name="Per_ID" size="32" value="<%
=getGuid %>"></td>
            <td><input type="hidden" name="PriceClass_ID" ></td>
            <td><input type="text" name="OrgName" size="40" ></td>
            <td><input type="text" name="Org_ID" size="32" ></td>
              <input type="hidden" name="MM_update" value="true">
              <input type="hidden" name="MM_recordId" >
              <input type="submit" name="submit" value="submit">
        <input type="hidden" name="MM_insert" value="true">

Function GetOrgID(pstOrgName)
  Dim stSQL, rst, stNewGuid
  If Len(pstOrgName) = "0" Then
    GetOrgId = "0"  ' No Company name entered
    Set rst = Server.CreateObject("ADODB.Recordset")
    rst.Open "Select Org_ID From Org Where OrgName = '" & pstOrgName 
& "'", "dsn=eFOSQL"
    If rst.EOF Then
      stNewGuid = GetGuid
      GetOrgID = stNewGuid ' Need to create a new Org Record here
          GetOrgID = rst.Fields("Org_ID")
    End If
    Set rst = Nothing
  End If

End Function

Function GetGuid()

  strNewGuid = ""
  Set TypeLib = Server.CreateObject("Scriptlet.TypeLib")
  strTemp = CStr(TypeLib.Guid)
  For i = 1 To Len(strTemp)
    strChar = Mid(strTemp,i,1)
    Select Case strChar
      Case "{","}","-"
      Case Else
        strNewGuid = strNewGuid & strChar
    End Select
  GetGuid = strNewGuid
  Set TypeLib = Nothing

End Function



  Return to Index