asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Creation and Maintenance of Appealing ASP Pages with Dynamically Generated Contents
By Boaz Sigelman
Rating: 3.7 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Often in ASP-related forums and newsgroups I find questions written by ASP beginners regarding input and output of database fields containing textual information. I decided to group some of these FAQs and answer them in this article. This should help Web creators who wish to use dynamically generated contents (stored in text files or memo fields).

    First, it will help to include apostrophes and pipe characters in databases. It will also show how to make that text look better by replacing CRLFs with BR tags and spaces with " " sequences. And it will preach the right way to store textual contents in database memo fields.

    I will be using the light version of the ASP component called 1touch. 1touch Light is free and can be downloaded form the 1touch Web site (http://www.1touchASP.com).

    Part 1: How can I insert the apostrophe symbol in my database?

    This is a common problem when dealing with databases. Databases reserve the ‘ sign to start and end strings (e.g., ‘restaurant of McDonald’). So by typing ‘ in the text you actually enter a third apostrophe in the middle of the string and "confuse" the database (e.g., ‘McDonald’s’). An important note is that there is no problem with regular quotes. The " symbol can be inserted to any database field without special manipulations.

    The solution: Under the same MS conventions, a string containing two consecutive apostrophes will be entered to the database as one apostrophe. So, double your apostrophes – instead of McDonald’s write ’McDonald’’s ’.

    So now you think, "That’s good, but what if I gather information from Web users? I can’t force them to double each ‘ they type, can I?" True. But you can manipulate what users enter into forms with ASP and a VBSscript function called Replace. Here is how you can use the Replace function to replace each ‘ with ‘’:

    
    
    MemoField = Request.Form("MemoField") ' Field from form
    Replace (MemoField,"'", "''")
    
    
    
    Note: In 1touch we created the InsertApos function for similar purposes, yet more elegant syntax, which despite of its name, replaces not only the ‘ character but also the | (pipe character) with their doubles. Look at this ASP code, which is a part of an ASP file that inserts data to a database (called "registration") from a Web form:
    
    
    
    <%
    ' Create the 1touch Object on the page.
    set 1t = Server.CreateObject("OneTouchASP.StrFunctions")
    
    ' Define ADODB parameters
    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open "dsn=DSNname;uid=UserID;pwd=UserPassword"
    
    ' Get parameters from the form collection and
    ' apply 1t.InsertApos on them
    First_Name=1t.InsertApos(request.form("First_Name"))
    Last_Name=1t.InsertApos(request.form("Last_Name"))
    Company=1t.InsertApos(request.form("Company_Name"))
    Email=1t.InsertApos(request.form("Email"))
    WWW_Site=1t.InsertApos(request.form("WWW_Site"))
    ' id - is a parameter you get from the DB's id field.
    
    ' Put parameters in registration database.
    SQLStmt = "INSERT INTO Registration (id, " &_
    "First_Name, Last_Name, Company, " &_
    "Resume, Email, Www_Site)"
    SQLStmt = SQLStmt & "VALUES (" & id
    SQLStmt = SQLStmt & ",'" & First_Name & "'" 
    SQLStmt = SQLStmt & ",'" & Last_Name & "'"
    SQLStmt = SQLStmt & ",'" & Company & "'"  
    SQLStmt = SQLStmt & ",'" & Resume & "'"  
    SQLStmt = SQLStmt & ",'" & Email & "'"  
    SQLStmt = SQLStmt & ",'" & Www_Site & "'" & ")"
    
    Application.Lock
    Conn.Execute(SQLStmt)
    Conn.Close
    Application.Unlock 
    ...
    %>
    
    
    

    Part 2: Line breaks originally entered "disappear."

    Well, those line breaks do not disappear, but actually become invisible or inactive. MS Windows translates the key <enter> into special characters called CR LF AKA chr(13) chr(10) or vbCrLf, but Web browsers are not familiar with any of these symbols. Browsers need the <br> tag to break between HTML lines. So what you need to do is to convert those CR LF symbols to <br> tags (actually to <br> and vbCrLf, so the source code would be readable for debugging). After reading the answer to the previous question, you should be able to do it yourself:

    
    
    MemoField = RSlist("MemoField") ' Field from database
    Replace(MemoField,chr(13) & chr(10), "<br>")
    
    
    
    Note: In 1touch we created the ConvertBreak function for similar purposes. Look at this code, which is a part of an ASP file, which retrieves fields from a database and shows them on the page. Notice the memo field "Resume":
    
    
    <%
    ' Create the 1touch Object on the page.
    set 1t = Server.CreateObject("OneTouchASP.StrFunctions")
    
    ' Define ADODB parameters
    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open "dsn=DSNname;uid=UserID;pwd=UserPassword"
    
    ' Get parameters from the registration DB using SQL Query
    
    SQLQuery = ("SELECT * FROM registration")
    Application.Lock
    RSlist = Conn.Execute(SQLQuery) 
    RSlist.Movefirst
    
    ' Retrieve the records, Run Convert Break on the memo field 
    ' and write to the HTML
    Do while not RSlist.EOF
         First_Name=RSlist("First_Name")
         Last_Name=RSlist("Last_Name")
         Company=RSlist("Company")
         Email=RSlist("Email")
         Www_Site=RSlist("Www_Site")
         Resume = 1t.ConvertBreak(RSlist("Resume"))
    
         Response.write First_Name & " " & Last_Name & "<br>"
         Response.write "Works in " & Company & "<br>"
         Response.write "Email : " & Email &_
     " , URL : " & Www_Site &_
     "<br><br>"
         Response.write "Resume : <br>"
         Response.write Resume
         Response.write "<hr>" ' add a delimiter at the end of the record
         RSlist.Movenext
    Loop
    
    Conn.Close
    Application.Unlock 
    ...
    %>
    
    
    
    You can also use ConvertSpace in 1touch, which turns a streak of at least two spaces to HTMLs   chars, like this:
    
    
    <%=1t.ConvertBreak(1t.ConvertSpace(MemoField))%>
    
    
    
    Writing your own ConvertSpace using Replace is a bit tricky and is where 1touch Light comes in handy.

    Part 3: Why not use both functions together and store HTML code in the database?

    It seems that Part #1 and Part #2 could be used together in one multipurpose function. This would result in less trouble for the programmer, who would only have to to manipulate the content once vs. twice, and in HTML code (<br> tags and   chars) stored in memo fields in the database.

    Well, so it seems. However, doing this would be poor practice. It is best to keep the content of your database HTML free.

    Even though you may not plan it, it is probable that the contents of these database fields may some day serve you in the desktop environment (Visual Basic, MS Access applications or plain "copy and paste" to Office files). It will be a real pain to get rid of those <br> tags and   chars then.

    Use InsertApos when inserting content into a database (going IN) and ConvertBreak and ConverSpace when retrieving content from a database (going OUT).

    Conclusion

    Now, using 1touch Light, you can enter apostrophes into your database, display line breaks and spaces when retrieving it to a Web page, and you know to keep your content in the database HTML code free.

    About the Author

    Boaz Sigelman is vice president of Zplusplus Systems and Communication Ltd. in Israel (http://zplusplus.co.il). He is a BSc graduate of Industrial Engineering and Management in the Tel Aviv University. In his 3 years of experience in Zplusplus, he has managed dozens of Web application projects for Zplusplus customers, mostly in the ASP environment. He wrote a seven-article HTML guide titled "Make Your Own Web Site," which was published in Ziff-Davis' Israeli PC Media magazine in 1997-98. Boaz likes to relax with Jazz music and a good glass of whiskey, and hates the fact that he is unable to resist checking his E-mail from his home computer.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    Proposion N2N
    Proposion N2N connects Microsoft .NET applications to Lotus Notes and Lotus Domino databases. This ADO.NET managed data provider allows you to perform blindingly fast queries and updates of Notes data from ASP.NET pages, .NET web services, Windows, or Mobile applications. An innovative SQL-like query language leverages the unique features of Notes and makes collaborative software accessible to relational database programmers.
    [Top]
    Other Articles
    Aug 7, 2002 - Using MySQL in the Win32 Environment
    Developers who don't want to spend a lot of money on SQL Server and who want a database that's more robust than Access may find MySQL to be a pleasant alternative. This introductory article covers the bare essentials for getting MySQL installed and running in the Win32 environment.
    [Read This Article]  [Top]
    Jul 17, 2002 - Software Development: Steps To Better Ensure Success
    There is never a guarantee of project success when endeavoring to build a sophisticated application. However, there are established steps to follow that will ensure a clear, concise scope, support for the team involved, and a solid opportunity for successful deployment.
    [Read This Article]  [Top]
    Jul 15, 2002 - Securing SQL Server for Web Applications
    If your SQL Server is exposed to the Internet, then hackers are probing it. This article shows how to secure a SQL Server database that's being used with a Web application
    [Read This Article]  [Top]
    Jul 1, 2002 - Protecting Your Web Application Against Dangerous Requests
    Enrico Di Cesare provides a solution for hiding and securing querystring values that pass through a url.
    [Read This Article]  [Top]
    Apr 2, 2002 - Object-Oriented Programming for VBScripters
    Feel intimidated by .NET? This article by Rob Chartier is designed to ease any level VBScripter (ASP) into .NET by clarifying some OOP concepts.
    [Read This Article]  [Top]
    Mar 27, 2002 - A Best Practice for Using ADO Objects
    A few members of the 15 Seconds discussion list talk about the proper way to use methods in order to prevent ADO object errors.
    [Read This Article]  [Top]
    Jan 2, 2002 - The ASP.NET Page Life Cycle
    Solomon Shaffer explores the life cycle of an ASP.NET page from initialization to unloading. He also explains the various methods to override ASP.NET server-side events.
    [Read This Article]  [Top]
    Dec 19, 2001 - Application Architecture: An N-Tier Approach - Part 2
    Rob Chartier creates a simple portable and reusable address book in .NET to demonstrate the power of N-tier application architecture. Complete source code included!
    [Read This Article]  [Top]
    Oct 23, 2001 - Application Architecture: An N-Tier Approach - Part 1
    Learn about N-tier application architecture and realize that developing with multiple layers produces a flexible and reusable application for distribution to any number of client interfaces.
    [Read This Article]  [Top]
    Oct 23, 2001 - Application Architecture: An N-Tier Approach - Part 1
    Learn about N-tier application architecture and realize that developing with multiple layers produces a flexible and reusable application for distribution to any number of client interfaces.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    IBM eBook: Planning a Service Oriented Architecture
    IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
    Intel Go Parallel Article: Getting Started with TBB on Windows
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Avaya Article: How to Feed Data into the Avaya Event Processor
    IBM Article: Developing a Software Policy for Your Organization
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    Intel Go Parallel Article: Intel Threading Tools and OpenMP
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    HP Video: StorageWorks EVA4400 and Oracle
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
    IBM Article: Enterprise Search--Do You Know What's Out There?
    HP Demo: StorageWorks EVA4400
    Microsoft Article: The Progress and Promise of Deep Zoom
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES