Building Dynamic Strings from Database Values

Discussion in 'ASP General' started by JP SIngh, Apr 21, 2005.

  1. JP SIngh

    JP SIngh Guest

    Hi All

    Hope someone can help us with an issue here.

    We use an ASP application in which we send out emails using cdo for various
    events.

    At present the email text is hard coded into the code. for example

    strMessage = "Dear " & rs("firstname")
    strMessage = strMessage & "Your request has been approved with reference id
    " & rs("id")

    This causes an administration overhead as everytime the text of the message
    needs to be changed we need to alter the code to incorporate text or
    database values.

    What we want is to store the message text into a table so it can be altered
    by the application administrators through GUI by adding the text through
    freetext entry and field names though a dropdown.

    and then use something like

    strFirstName = rs("firstname")

    Set rst = Server.CreateObject("ADODB.Recordset")
    sql = "select * from templates where id = 1"
    rst.Open sql, conn
    strText = rst("templatedata")

    Response.write "templatedate " & strText

    Now is the strText Contains the text Dear {FirstName} This is a contract
    between the two parties.

    How do I change all occurances of the {FirstName} with strFirstName such
    that if firstname is "SIMON" the strText should be

    "Dear SIMON This is a contract between the two parties."

    Any help is appreciated.
     
    JP SIngh, Apr 21, 2005
    #1
    1. Advertising

  2. JP SIngh wrote:
    > Set rst = Server.CreateObject("ADODB.Recordset")
    > sql = "select * from templates where id = 1"


    Don't use selstar: http://www.aspfaq.com/show.asp?id=2096

    > rst.Open sql, conn
    > strText = rst("templatedata")
    >
    > Response.write "templatedate " & strText
    >
    > Now is the strText Contains the text Dear {FirstName} This is a
    > contract between the two parties.
    >
    > How do I change all occurances of the {FirstName} with strFirstName
    > such that if firstname is "SIMON" the strText should be
    >
    > "Dear SIMON This is a contract between the two parties."
    >

    Use the Replace function:

    strText = Replace(strText,"{FirstName}", strFirstName)

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Apr 21, 2005
    #2
    1. Advertising

  3. JP SIngh

    Steven Burn Guest

    strText = Replace(rst("templatedata"), "FIRSTNAME", rst("firstname"))

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "JP SIngh" <> wrote in message news:...
    > Hi All
    >
    > Hope someone can help us with an issue here.
    >
    > We use an ASP application in which we send out emails using cdo for various
    > events.
    >
    > At present the email text is hard coded into the code. for example
    >
    > strMessage = "Dear " & rs("firstname")
    > strMessage = strMessage & "Your request has been approved with reference id
    > " & rs("id")
    >
    > This causes an administration overhead as everytime the text of the message
    > needs to be changed we need to alter the code to incorporate text or
    > database values.
    >
    > What we want is to store the message text into a table so it can be altered
    > by the application administrators through GUI by adding the text through
    > freetext entry and field names though a dropdown.
    >
    > and then use something like
    >
    > strFirstName = rs("firstname")
    >
    > Set rst = Server.CreateObject("ADODB.Recordset")
    > sql = "select * from templates where id = 1"
    > rst.Open sql, conn
    > strText = rst("templatedata")
    >
    > Response.write "templatedate " & strText
    >
    > Now is the strText Contains the text Dear {FirstName} This is a contract
    > between the two parties.
    >
    > How do I change all occurances of the {FirstName} with strFirstName such
    > that if firstname is "SIMON" the strText should be
    >
    > "Dear SIMON This is a contract between the two parties."
    >
    > Any help is appreciated.
    >
    >
     
    Steven Burn, Apr 21, 2005
    #3
  4. JP SIngh

    JP SIngh Guest

    Great. Thanks a quick reply.

    Now I have potentially 20-30 field names in the same message. Do I have to
    have 30 replace statements or can i write something generic

    the string name in the message will always correspond to the fieldname in
    the database

    for example

    {FirstName} - rs("FirstName")
    {LastName} - rs("LastName")
    {Department} - rs("Department")


    "Bob Barrows [MVP]" <> wrote in message
    news:%...
    > JP SIngh wrote:
    > > Set rst = Server.CreateObject("ADODB.Recordset")
    > > sql = "select * from templates where id = 1"

    >
    > Don't use selstar: http://www.aspfaq.com/show.asp?id=2096
    >
    > > rst.Open sql, conn
    > > strText = rst("templatedata")
    > >
    > > Response.write "templatedate " & strText
    > >
    > > Now is the strText Contains the text Dear {FirstName} This is a
    > > contract between the two parties.
    > >
    > > How do I change all occurances of the {FirstName} with strFirstName
    > > such that if firstname is "SIMON" the strText should be
    > >
    > > "Dear SIMON This is a contract between the two parties."
    > >

    > Use the Replace function:
    >
    > strText = Replace(strText,"{FirstName}", strFirstName)
    >
    > Bob Barrows
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >
     
    JP SIngh, Apr 21, 2005
    #4
  5. You'll need to do a loop:

    for each fld in rs.Fields
    strText=Replace(strText,"{" & fld.Name & "}", fld.Value)
    next

    Bob Barrows

    JP SIngh wrote:
    > Great. Thanks a quick reply.
    >
    > Now I have potentially 20-30 field names in the same message. Do I
    > have to have 30 replace statements or can i write something generic
    >
    > the string name in the message will always correspond to the
    > fieldname in the database
    >
    > for example
    >
    > {FirstName} - rs("FirstName")
    > {LastName} - rs("LastName")
    > {Department} - rs("Department")
    >
    >
    > "Bob Barrows [MVP]" <> wrote in message
    > news:%...
    >> JP SIngh wrote:
    >>> Set rst = Server.CreateObject("ADODB.Recordset")
    >>> sql = "select * from templates where id = 1"

    >>
    >> Don't use selstar: http://www.aspfaq.com/show.asp?id=2096
    >>
    >>> rst.Open sql, conn
    >>> strText = rst("templatedata")
    >>>
    >>> Response.write "templatedate " & strText
    >>>
    >>> Now is the strText Contains the text Dear {FirstName} This is a
    >>> contract between the two parties.
    >>>
    >>> How do I change all occurances of the {FirstName} with strFirstName
    >>> such that if firstname is "SIMON" the strText should be
    >>>
    >>> "Dear SIMON This is a contract between the two parties."
    >>>

    >> Use the Replace function:
    >>
    >> strText = Replace(strText,"{FirstName}", strFirstName)
    >>
    >> Bob Barrows
    >>
    >> --
    >> Microsoft MVP -- ASP/ASP.NET
    >> Please reply to the newsgroup. The email account listed in my From
    >> header is my spam trap, so I don't check it very often. You will get
    >> a quicker response by posting to the newsgroup.


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Apr 21, 2005
    #5
  6. JP SIngh

    Patrice Guest

    You could also use the standard .NET placeholder {0} for first parameter,
    {1} for second etc...

    Patrice

    --

    "JP SIngh" <> a écrit dans le message de
    news:...
    > Great. Thanks a quick reply.
    >
    > Now I have potentially 20-30 field names in the same message. Do I have to
    > have 30 replace statements or can i write something generic
    >
    > the string name in the message will always correspond to the fieldname in
    > the database
    >
    > for example
    >
    > {FirstName} - rs("FirstName")
    > {LastName} - rs("LastName")
    > {Department} - rs("Department")
    >
    >
    > "Bob Barrows [MVP]" <> wrote in message
    > news:%...
    > > JP SIngh wrote:
    > > > Set rst = Server.CreateObject("ADODB.Recordset")
    > > > sql = "select * from templates where id = 1"

    > >
    > > Don't use selstar: http://www.aspfaq.com/show.asp?id=2096
    > >
    > > > rst.Open sql, conn
    > > > strText = rst("templatedata")
    > > >
    > > > Response.write "templatedate " & strText
    > > >
    > > > Now is the strText Contains the text Dear {FirstName} This is a
    > > > contract between the two parties.
    > > >
    > > > How do I change all occurances of the {FirstName} with strFirstName
    > > > such that if firstname is "SIMON" the strText should be
    > > >
    > > > "Dear SIMON This is a contract between the two parties."
    > > >

    > > Use the Replace function:
    > >
    > > strText = Replace(strText,"{FirstName}", strFirstName)
    > >
    > > Bob Barrows
    > >
    > > --
    > > Microsoft MVP -- ASP/ASP.NET
    > > Please reply to the newsgroup. The email account listed in my From
    > > header is my spam trap, so I don't check it very often. You will get a
    > > quicker response by posting to the newsgroup.
    > >
    > >

    >
    >
     
    Patrice, Apr 21, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Meyer

    building strings with variable input

    Olaf Meyer, Jan 12, 2004, in forum: Python
    Replies:
    12
    Views:
    476
    Dave Benjamin
    Jan 17, 2004
  2. Gal Diskin

    building strings from variables

    Gal Diskin, Oct 5, 2006, in forum: Python
    Replies:
    3
    Views:
    336
    Gal Diskin
    Oct 7, 2006
  3. wesley chun

    Re: building strings from variables

    wesley chun, Oct 6, 2006, in forum: Python
    Replies:
    4
    Views:
    261
    Gal Diskin
    Oct 7, 2006
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  5. Midex
    Replies:
    24
    Views:
    970
Loading...

Share This Page