Problem inserting/updating records

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Problem inserting/updating records

    Hi

    I'm having trouble trying to insert/update the records in SQL Server.

    I have created a form based on two tables and created a select statement so I
    can display the records on the form.

    But when it comes to inserting/updating the record it doesn't seem to run the
    function I created in the CFC file, and no error is displayed at all.

    Here's the insert function from the CFC file: -

    <!--- Insert a vacancy --->
    <cffunction name="addVacancy" returntype="string">
    <!--- Method arguments --->
    <cfargument name="VacancyRef"
    type="string"
    required="yes"
    hint="Vacancy reference">
    <cfargument name="VacancyTitle"
    type="string"
    required="yes"
    hint="Job title">
    <cfargument name="VacancyDept"
    type="string"
    required="yes"
    hint="Department">
    <cfargument name="VacancyDetails"
    type="string"
    required="no"
    hint="Vacancy Details">
    <cfargument name="Gender"
    type="string"
    required="yes"
    hint="Gender">
    <cfargument name="MaritalStatus"
    type="string"
    required="yes"
    hint="Marital status">
    <cfargument name="AgeBand"
    type="string"
    required="yes"
    hint="Age band">
    <cfargument name="EthnicGroup"
    type="string"
    required="yes"
    hint="Ethnic group">
    <cfargument name="EthnicOrigin"
    type="string"
    required="yes"
    hint="Ethnic origin">
    <cfargument name="OtherEthnicDesc"
    type="string"
    required="no"
    hint="Ethnic background">
    <cfargument name="Disability"
    type="any"
    required="yes"
    hint="Disability">
    <cfargument name="DisabilityDetails"
    type="string"
    required="no"
    hint="Disability details">
    <cfargument name="AdvertSource"
    type="string"
    required="no"
    hint="Publication">

    <cftransaction>
    <!---Insert vacancy --->
    <cfquery datasource="#ds#">
    INSERT INTO dbo.Vacancy(VacancyRef,
    VacancyTitle,
    VacancyDept,
    VacancyDetails)
    VALUES('#Trim(ARGUMENTS.VacancyRef)#',
    '#Trim(ARGUMENTS.VacancyTitle)#',
    '#Trim(ARGUMENTS.VacancyDept)#',
    '#Trim(ARGUMENTS.VacancyDetails)#')
    </cfquery>
    <cfreturn true>

    <!--- Insert Monitoring Data --->
    <cfquery datasource="#ds#">
    INSERT INTO dbo.MonitoringData(VacancyRef,
    Gender,
    MaritalStatus,
    AgeBand,
    EthnicGroup,
    EthnicOrigin,
    OtherEthnicDesc,
    Disability,
    DisabilityDetails,
    AdvertSource)
    VALUES('#Trim(ARGUMENTS.VacancyRef)#',
    '#Trim(ARGUMENTS.Gender)#',
    '#Trim(ARGUMENTS.MaritalStatus)#',
    '#Trim(ARGUMENTS.AgeBand)#',
    '#Trim(ARGUMENTS.EthnicGroup)#',
    '#Trim(ARGUMENTS.EthnicOrigin)#',
    '#Trim(ARGUMENTS.OtherEthnicDesc)#',
    #ARGUMENTS.Disability#,
    '#Trim(ARGUMENTS.DisabilityDetails)#',
    '#Trim(ARGUMENTS.AdvertSource)#')
    </cfquery>
    <cfreturn true>
    </cftransaction>
    </cffunction>

    Since I am inserting into two tables I have used the <cftransaction> tab to
    use more than one query, am I using this tag correctly? I've never used it
    before.

    KevCB Guest

  2. Similar Questions and Discussions

    1. Having trouble inserting/updating, please look @ code
      Hello, I'm getting an error while trying to insert a record from a form, or update a record from the datagrid. ERROR: at...
    2. Problem updating unique mutiple records
      What I need to do: Create an output form where users can update/delete a particular entry in my database. For example: Entry 1...
    3. Inserting and Updating Records at the same time
      Hi, Ive created a form which inserts a record into one table within the database, but i also need it to increment a field in another table of the...
    4. Updating and Inserting simoultaneously
      hello not sure if this is possible, i need to update a field in a table and then insert a new record into a different table from when the user...
    5. Updating/inserting >> Linking table
      A dilemma: If I am attempting to add multiple products to an existing Customer-Products linking table do I do this via an Insert or Update? ...
  3. #2

    Default Re: Problem inserting/updating records

    Just a quick update:

    I have managed to get the update function working now, but am still unable to
    get the insert working. For some reason it is not inserting the records, and I
    don't know why because I don't receive an error and nothing is mentioned in the
    logs.

    I have changed the code for the insert function slightly so it now reads:-

    <!--- Insert a vacancy --->
    <cffunction name="addVacancy" returntype="string">
    <!--- Method arguments --->
    <cfargument name="VacancyRef"
    type="string"
    required="yes"
    hint="Vacancy reference">
    <cfargument name="VacancyTitle"
    type="string"
    required="yes"
    hint="Job title">
    <cfargument name="VacancyDept"
    type="string"
    required="yes"
    hint="Department">
    <cfargument name="VacancyDetails"
    type="string"
    required="no"
    hint="Vacancy Details">
    <cfargument name="Gender"
    type="string"
    required="yes"
    hint="Gender">
    <cfargument name="MaritalStatus"
    type="string"
    required="yes"
    hint="Marital status">
    <cfargument name="AgeBand"
    type="string"
    required="yes"
    hint="Age band">
    <cfargument name="EthnicGroup"
    type="string"
    required="yes"
    hint="Ethnic group">
    <cfargument name="EthnicOrigin"
    type="string"
    required="yes"
    hint="Ethnic origin">
    <cfargument name="OtherEthnicDesc"
    type="string"
    required="no"
    hint="Ethnic background">
    <cfargument name="Disability"
    type="string"
    required="yes"
    hint="Disability">
    <cfargument name="DisabilityDetails"
    type="string"
    required="no"
    hint="Disability details">
    <cfargument name="AdvertSource"
    type="string"
    required="no"
    hint="Publication">

    <cftransaction>
    <!---Insert vacancy --->
    <cfquery datasource="#ds#">
    INSERT INTO dbo.Vacancy(VacancyRef,
    VacancyTitle,
    VacancyDept,
    VacancyDetails)
    VALUES('#Trim(ARGUMENTS.VacancyRef)#',
    '#Trim(ARGUMENTS.VacancyTitle)#',
    '#Trim(ARGUMENTS.VacancyDept)#',
    '#Trim(ARGUMENTS.VacancyDetails)#')
    </cfquery>

    <!--- Insert Monitoring Data --->
    <cfquery datasource="#ds#">
    INSERT INTO dbo.MonitoringData(VacancyRef,
    Gender,
    MaritalStatus,
    AgeBand,
    EthnicGroup,
    EthnicOrigin,
    OtherEthnicDesc,
    Disability,
    DisabilityDetails,
    AdvertSource)
    VALUES('#Trim(ARGUMENTS.VacancyRef)#',
    '#Trim(ARGUMENTS.Gender)#',
    '#Trim(ARGUMENTS.MaritalStatus)#',
    '#Trim(ARGUMENTS.AgeBand)#',
    '#Trim(ARGUMENTS.EthnicGroup)#',
    '#Trim(ARGUMENTS.EthnicOrigin)#',
    '#Trim(ARGUMENTS.OtherEthnicDesc)#',
    '#Trim(ARGUMENTS.Disability)#',
    '#Trim(ARGUMENTS.DisabilityDetails)#',
    '#Trim(ARGUMENTS.AdvertSource)#')
    </cfquery>
    </cftransaction>
    <cfreturn addVacancy>
    </cffunction>

    So just to clarify my problem, the function above doesn't seem to insert the
    records into the two tables at all.

    Can anyone help?

    KevCB Guest

  4. #3

    Default Re: Problem inserting/updating records

    What the code of the calling page where you invoke component and it's method addVacancy?
    CF_Oracle Guest

  5. #4

    Default Re: Problem inserting/updating records

    I've decided to place all the files that are used on my web hosting package
    with my ISP rather than copy and pasting the code on here, I've saved them as
    text files but for some reason it keeps running them as html files, so you will
    have to save them to view them.

    I have changed the code slightly for the CFC. I decided to try the insert
    with the cfinsert tags rather than using the SQL syntax, but it still didn't
    work.

    The order in which the files run are as follows:

    search.cfm - results.cfm - details.cfm - process.cfm

    The idea is that when the user searches it displays the results. If the
    VacancyRef is not passed to the details.cfm page then it knows that it is
    inserting a record, else if the VacancyRef is passed to the page then it
    displays the current record and allows you to update the record.

    The process.cfm file is run when the submit button is clicked in the
    details.cfm page, and it is this file that decides whether to update or insert
    the record.

    The text files can be found [url]http://kevincb.nadsl.net[/url].

    KevCB Guest

  6. #5

    Default Re: Problem inserting/updating records

    I see you are using cfinsert instead of cfquery. I never do that myself but I
    have read on these forums that if your form fields don't include a primary key,
    it won't work.

    Could be something else though.

    Dan Bracuk Guest

  7. #6

    Default Re: Problem inserting/updating records

    Had a look through the code again, and added the primary keys in both cfinsert tags but that didn't work either :(

    Thanks for the suggestion though.
    KevCB Guest

  8. #7

    Default Re: Problem inserting/updating records

    1. In result page, in query <cfquery name="vSearch" datasource="HR">
    SELECT *
    FROM dbo.Vacancy
    <!--- Search by vacancy reference --->
    <cfif FORM.VacancyRef IS NOT "">
    WHERE VacancyRef LIKE '%#FORM.VacancyRef#%'
    </cfif>
    .....
    put WHERE outside cfif and also put cfif IsDefined like this:

    SELECT *
    FROM dbo.Vacancy
    WHERE 1=1
    <cfif IsDefined ("FORM.VacancyRef" AND FORM.VacancyRef IS NOT "">
    VacancyRef LIKE '%#FORM.VacancyRef#%'
    </cfif>
    ......
    2. Provide duplicates checking before insert otherwise could be DB tables
    columns unique constraints violation


    CF_Oracle Guest

  9. #8

    Default Re: Problem inserting/updating records

    Hi

    Could you explain what you mean in point 2, I'm not quite sure what your
    talking about.

    Although I have managed to change the <cfif> tags in the results page like you
    mentioned, so thanks for that.

    If it helps I've genereated the SQL script for the tables in SQL Server, and
    placed them on my web space [url]http://kevincb.nadsl.net/[/url].

    Thanks

    KevCB Guest

  10. #9

    Default Re: Problem inserting/updating records

    Unless duplicate records are intentionally allowed, before invoke addVacancy
    mathod do something like:
    <cfquery name="qCheckDups" datasource="#REQUEST.DSN#">
    SELECT Band_Translation
    FROM Band
    WHERE Trim(VacancyRef) = '#Trim(ARGUMENTS.VacancyRef)#'
    AND Trim(VacancyTitle) = '#Trim(ARGUMENTS.VacancyTitle)#'
    AND Trim(VacancyDept) = '#Trim(ARGUMENTS.VacancyDept)#'
    AND Trim(VacancyDetails) = '#Trim(ARGUMENTS.VacancyDetails)#')
    </cfquery>
    <cfif qcheckDups.recordcount EQ 0>

    <cfelse>

    </cfif>

    Also (not related to duplicates but helps to pinpoint a problem), in the
    component within cftransaction use error handling like this:
    <cftry>


    <cfcatch type="database">
    <cftransaction action = "rollback"/>
    <cfinclude template="../ErrorHandlingTemplate.cfm">
    </cfcatch>
    </cftry>
    <cftransaction action="commit"/>
    </cftransaction>

    CF_Oracle Guest

  11. #10

    Default Re: Problem inserting/updating records

    OK, I've added the following to my process.cfm page above the invoke.

    <cfquery name="qCheckDups" datasource="HR">
    SELECT *
    FROM dbo.Vacancy
    WHERE Trim(VacancyRef) = '#Trim(ARGUMENTS.VacancyRef)#'
    AND Trim(VacancyTitle) = '#Trim(ARGUMENTS.VacancyTitle)#'
    AND Trim(VacancyDept) = '#Trim(ARGUMENTS.VacancyDept)#'
    AND Trim(VacancyDetails) = '#Trim(ARGUMENTS.VacancyDetails)#')
    </cfquery>
    <cfif qcheckDups.recordcount EQ 0>

    <!--- Do it --->
    <cfinvoke component="HR.components.vacancy"
    method="#method#">
    <!--- VacancyRef only if update method --->
    <cfif IsDefined("FORM.VacancyRef")>
    <cfinvokeargument name="VacancyRef"
    value="#Trim(FORM.VacancyRef)#">
    </cfif>
    <cfinvokeargument name="VacancyTitle"
    value="#Trim(FORM.VacancyTitle)#">
    <cfinvokeargument name="VacancyDept"
    value="#Trim(FORM.VacancyDept)#">
    <cfinvokeargument name="VacancyDetails"
    value="#Trim(FORM.VacancyDetails)#">
    <cfinvokeargument name="Gender"
    value="#Trim(FORM.Gender)#">
    <cfinvokeargument name="MaritalStatus"
    value="#Trim(FORM.MaritalStatus)#">
    <cfinvokeargument name="AgeBand"
    value="#Trim(FORM.AgeBand)#">
    <cfinvokeargument name="EthnicGroup"
    value="#Trim(FORM.EthnicGroup)#">
    <cfinvokeargument name="EthnicOrigin"
    value="#Trim(FORM.EthnicOrigin)#">
    <cfinvokeargument name="OtherEthnicDesc"
    value="#Trim(FORM.OtherEthnicDesc)#">
    <cfinvokeargument name="Disability"
    value="#Trim(FORM.Disability)#">
    <cfinvokeargument name="DisabilityDetails"
    value="#Trim(FORM.DisabilityDetails)#">
    <cfinvokeargument name="AdvertSource"
    value="#Trim(FORM.AdvertSource)#">
    </cfinvoke>
    </cfif>

    But when running this I get an error:

    Element VACANCYREF is undefined in ARGUMENTS.

    KevCB Guest

  12. #11

    Default Re: Problem inserting/updating records

    I replaced the ARGUMENTS with FORM and it came out with the following error:-

    'Trim' is not a recognized built-in function name.

    The error occurred in C:\Inetpub\wwwroot\HR\process.cfm: line 14

    12 : AND Trim(VacancyTitle) = '#Trim(FORM.VacancyTitle)#'
    13 : AND Trim(VacancyDept) = '#Trim(FORM.VacancyDept)#'
    14 : AND Trim(VacancyDetails) = '#Trim(FORM.VacancyDetails)#')
    15 : </cfquery>
    16 : <cfif qcheckDups.recordcount EQ 0>

    I also tried the code within the transaction and removed the above from
    process.cfm, but it didn't show any error, and didn't insert the record.

    KevCB Guest

  13. #12

    Default Re: Problem inserting/updating records

    TRIM is Oracle function . Use analog in SQL server or nothing if you have no one.
    CF_Oracle Guest

  14. #13

    Default Re: Problem inserting/updating records

    OK, taken out the trims. But now it is giving me an error about my data types.

    The data types text and varchar are incompatible in the equal to operator.

    The error occurred in C:\Inetpub\wwwroot\HR\process.cfm: line 14

    12 : AND VacancyTitle = '#Trim(FORM.VacancyTitle)#'
    13 : AND VacancyDept = '#Trim(FORM.VacancyDept)#'
    14 : AND VacancyDetails = '#Trim(FORM.VacancyDetails)#'
    15 : </cfquery>
    16 : <cfif qcheckDups.recordcount EQ 0>

    KevCB Guest

  15. #14

    Default Re: Problem inserting/updating records

    I've tried changing my data types, but now I keep getting the following error:

    The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
    KevCB Guest

  16. #15

    Default Re: Problem inserting/updating records

    At least one of the fields you're evaluating is text, ntext, or image. The
    error message is by design. You have to use the LIKE operator and wildcards to
    find strings in text, ntext, or image data.

    You should consider changing the data type to varchar, if possible, and if the
    contents never exceed 8000 characters.

    philh Guest

  17. #16

    Default Re: Problem inserting/updating records

    Thanks for clearing the problem up philh, I've managed to stop the error, but
    unfortunatley it is still not inserting records into SQL Server.

    I've tried both bits of code that CF_Oracle has, but it didn't produce an
    ErrorHandlingTemplate.cfm file, and brought back no errors.

    KevCB Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139