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.

------_=_NextPart_001_01C1C881.AAA7BAE0
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.


regards
David Cameron
nOw.b2b
dcameron@i...

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


Hello,

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)
FirstName
LastName
 
Record Org
Org_ID    (guid)
OrgName
 
Form
Per_ID    value="<%=getguid%>"
Org_ID
OrgName    
FirstName
LastName
 
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 
GUID.
 
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?

Thanks
Tony


<%@LANGUAGE="VBSCRIPT"%>
<!--#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 = ""
  MM_fieldsStr  
= "FirstName|value|LastName|value|Group_ID|value|Title|value|Per_ID|value|P
riceClass_ID|value|Org_ID|value"
  MM_columnsStr 
= "FirstName|',none,''|LastName|',none,''|Group_ID|',none,''|Title|',none,'
'|Per_ID|',none,''|PriceClass_ID|',none,''|Org_ID|',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
  Else
      MM_fields(i+1) = CStr(Request.Form(MM_fields(i)))
  End If
  Next

  ' 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
    Else
      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
    Else
      If (AltVal <> "") Then
        FormVal = AltVal
      ElseIf (Delim = "'") Then  ' escape quotes
        FormVal = "'" & Replace(FormVal,"'","''") & "'"
      Else
        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
  Next
  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
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    If (MM_editRedirectUrl <> "") Then
      Response.Redirect(MM_editRedirectUrl)
    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.Open()
ExtPP_numRows = 0
%>
<html>
<head>
<title>CRM - Person Details ADD</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<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">
          <tr> 
            <td>First</td>
            <td><input type="text" name="FirstName" size="30" ></td>
            <td>Last</td>
            <td><input type="text" name="LastName" size="30" ></td>
            <td><input type="hidden" name="Group_ID" size="32" 
value="0"></td>
          </tr>
          <tr> 
            <td></td> 
            <td><input type="text" name="Title" size="40" 
maxlength="50"></td>
            <td></td>
            <td><input type="text" name="Per_ID" size="32" value="<%
=getGuid %>"></td>
            <td><input type="hidden" name="PriceClass_ID" ></td>
          </tr>
          <tr> 
            <td>Company</td>
            <td><input type="text" name="OrgName" size="40" ></td>
            <td></td>
            <td><input type="text" name="Org_ID" size="32" ></td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td>Primary</td>
            <td</td>
            <td>Alternate</td>
            <td></td>
            <td>
              <input type="hidden" name="MM_update" value="true">
              <input type="hidden" name="MM_recordId" >
              <input type="submit" name="submit" value="submit">
            </td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="true">
      </form>
</body>
</html>
<%
ExtPP.Close()
%>
<%

Function GetOrgID(pstOrgName)
  Dim stSQL, rst, stNewGuid
  
  If Len(pstOrgName) = "0" Then
    GetOrgId = "0"  ' No Company name entered
  Else  
    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
    Else
          GetOrgID = rst.Fields("Org_ID")
    End If
  
    rst.Close
    
    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
  Next
  GetGuid = strNewGuid
  Set TypeLib = Nothing

End Function

%>



$subst('Email.Unsub').


  Return to Index