IIS ASP - Building Dynamic Strings from Database Values

This is Interesting: Free IT Magazines  
Home > Archive > IIS ASP > April 2005 > Building Dynamic Strings from Database Values





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Building Dynamic Strings from Database Values
JP SIngh

2005-04-21, 5:55 pm

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.


Bob Barrows [MVP]

2005-04-21, 5:55 pm

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.


Steven Burn

2005-04-21, 5:55 pm

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

--=20
Regards

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

Keeping it FREE!

"JP SIngh" <none@none.com> wrote in message =
news:uJauilnRFHA.3336@TK2MSFTNGP09.phx.gbl...
> Hi All
>=20
> Hope someone can help us with an issue here.
>=20
> We use an ASP application in which we send out emails using cdo for =

various
> events.
>=20
> At present the email text is hard coded into the code. for example
>=20
> strMessage =3D "Dear " & rs("firstname")
> strMessage =3D strMessage & "Your request has been approved with =

reference id
> " & rs("id")
>=20
> 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.
>=20
> 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.
>=20
> and then use something like
>=20
> strFirstName =3D rs("firstname")
>=20
> Set rst =3D Server.CreateObject("ADODB.Recordset")
> sql =3D "select * from templates where id =3D 1"
> rst.Open sql, conn
> strText =3D rst("templatedata")
>=20
> Response.write "templatedate " & strText
>=20
> Now is the strText Contains the text Dear {FirstName} This is a =

contract
> between the two parties.
>=20
> How do I change all occurances of the {FirstName} with strFirstName =

such
> that if firstname is "SIMON" the strText should be
>=20
> "Dear SIMON This is a contract between the two parties."
>=20
> Any help is appreciated.
>=20
>=20


JP SIngh

2005-04-21, 5:55 pm

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]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:%23UxZC2nRFHA.2736@TK2MSFTNGP09.phx.gbl...
> JP SIngh wrote:
>
> Don't use selstar: http://www.aspfaq.com/show.asp?id=2096
>
> 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]

2005-04-21, 5:55 pm

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:[vbcol=seagreen]
> 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]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:%23UxZC2nRFHA.2736@TK2MSFTNGP09.phx.gbl...

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

2005-04-21, 5:55 pm

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

Patrice

--

"JP SIngh" <none@none.com> a écrit dans le message de
news:u9LKH7nRFHA.2680@TK2MSFTNGP09.phx.gbl...
> 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]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:%23UxZC2nRFHA.2736@TK2MSFTNGP09.phx.gbl...
>
>



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com