Collapsible Repeater Demo

This Repeater condenses the data in collapsible regions, making the data much easier to consume.

How do you convert a string to all uppercase or all lowercase?
Submitted By: Scott Mitchell
Views: 53397
To convert a string to uppercase use the UCase function. To convert a string to lowercase use the LCase function.

Dim str
str = "ThIs iS a sTRInG wITh mIxED casE."

'This will print out the string all in lowercase
Response.Write LCase(str)

'This will print out the string all in uppercase
Response.Write UCase(str)

How can I display line breaks in an email message?
Submitted By: Scott Mitchell
Views: 48000
When starting out with sending email messages through ASP pages, many developers have trouble inserting line breaks. To accomplish this, simply insert a vbCrLf wherever you want a line break in your email's body. For example, using CDONTS, we'd have:

Dim objMail
Set objMail = Server.CreateObject("CDONTS.NewMail")

'Set the various CDONTS properties...
objMail.To = ""
  ' ...
objMail.Body = "This is on line 1" & vbCrLf & _
               "And this is on line 2!"

Simply throw in a vbCrLf wherever you need a line break in your email!

Can I send emails without using CDONTS?
Submitted By: Scott Mitchell
Views: 130322
Heavens yes! CDONTS is just one email component, there are many others. CDONTS has its limitations - it can only be used on Windows NT Server and does not provide very many options in sending emails. According to Microsoft's documentation, CDONTS was designed for sending quick, text-based emails. The nice thing about CDONTS is that it is likely already installed on your Web server and it's free!

Other email components include ASPEmail, SA-SMTPEmail, and others. For more information be sure to read: Sending Emails Without Using CDONTS.
How can I determine the length of a string (how many characters are in a string)?
Submitted By: Scott Mitchell
Views: 45354
The Len function takes a string value as an input and returns the number of characters in the string as an integer. A simple example can be seen below:

Dim strName
strName = "Scott"

Dim iCharCount
iCharCount = Len(strName) 'iCharCount = 5

If you are interested in the number of bytes that make up a string (as opposed to the number of characters in a string, use the LenB function. (Unicode characters are composed of two bytes per character instead of one.)

For more information be sure to check out the technical docs! Happy Programming!

How can I find the position of a certain character (or substring) within a string?
Submitted By: Scott Mitchell
Views: 65717
Use the InStr function. The InStr function has the following definition:

InStr(start, StringToSearch, StringToFind [, compare])

start specifies where to begin searching the StringToSearch looking for StringToFind. If you want to start searching at the beginning of the string, set start to 1.

The optional compare parameter specifies if a binary or textual compare should be performed. If you set compare to the system constant vbBinaryCompare (or the hardcoded value of 0) then a binary search will be performed; setting compare to vbTextCompare (or the hardcoded value of 1), a textual compare will be performed. The difference is as follows: a binary search takes case into consideration while a textual comparison does not. So, if you were seaching for the substring "foo" in the string "FooBar", a textual comparison would find "foo" while a binary comparison would not. (By default, a binary comparison is performed.)

Dim str
str = "No beer and no TV makes Homer something-something."

Response.Write InStr(1, str, "Homer") & "<BR>"
Response.Write InStr(1, str, "homer") & "<BR>"
Response.Write InStr(1, str, "homer", vbTextCompare) & "<BR>"
Response.Write InStr(1, str, "TV") & "<BR>"
Response.Write InStr(1, str, "Scott") & "<BR>"
Response.Write InStr(1, str, "no", vbBinaryCompare) & "<BR>"
Response.Write InStr(1, str, "no", vbTextCompare) & "<BR>"

The output from the above script is:

For more information on InStr, be sure to read the technical docs! Happy Programming!

How can I return the current date/time?
Submitted By: Scott Mitchell
Views: 58677
To retrieve the current date and/or time (according to the time set on the Web server you are executing the script on), use the following functions:

Now() - returns the current date and time
Date() - returns the current date
Time() - returns the current time

For more information be sure to read the technical docs for Now(), Date(), and Time().
What is recursion?
Submitted By: Scott Mitchell
Views: 43629
Recursion is a powerful technique that can be used when calling subroutines or functions. Simply put, recursion is repeatedly recalling the same function from within the function (or subroutine). There are many applications that can be defined recusively. For example, the factorial function (among many others) can be defined recursively.

Rather than go into the details of recursion here, I will save that for a highly recommended reading: Recursion: Why it's Cool. Be sure to read that article for a good understanding of recursion!
How do you create a database table through a SQL statement (via an ASP page, perhaps)?
Submitted By: Scott Mitchell
Views: 74803
To create a database table, use the SQL command CREATE TABLE. The command is pretty complex, and before you start creating tables via SQL code, be sure to read up on the command in the SQL Books On-Line. The basic structure of the CREATE TABLE statement is as follows:

   ColumnName1 Datatype Properties
   ColumnName2 Datatype Properties
   ColumnNameN Datatype Properties

So, to create a table named FooBar with Name and Age columns, you could use the following SQL script:

  Name varchar(50),
  Age int

To view a short example on how to accomplish this through an ASP page, take a moment to read this ASPMessageboard post!

The above example works well for creating a database table... but what if you want to create, say, an entire Access database (including the .MDB file itself)? Fortunately Microsoft has provided a set of libraries referred to as ADOX which allow for this functionality. To learn more about ADOX be sure to read: Working with ADOX.

Happy Programming!

Using VBScript, how can I determine if a variable is a valid date or not?
Submitted By: Scott Mitchell
Views: 53379
Use the IsDate function. IsDate accepts a variable as a single parameter and returns a boolean value - True if the variable can successfully be converted to a date and false if it cannot.

Dim dtValidDate, dtNotValid
dtValidDate = "08/01/78"
dtNotValid = "Scott"

If IsDate(dtValidDate) then
  Response.Write (dtValidDate & " is valid<BR>")
  Response.Write (dtValidDate & " is NOT valid<BR>")
End If

If IsDate(dtNotValid) then
  Response.Write (dtNotValid & " is valid<BR>")
  Response.Write (dtNotValid & " is NOT valid<BR>")
End If

The output of the above script is:
08/01/78 is valid
Scott is NOT valid

For more information be sure to read the technical docs.

How can I easily display a nicely formatted date variable?
Submitted By: Scott Mitchell
Views: 75619
Use the FormatDateTime function. FormatDateTime takes two input paraemeters, a required date variable and an optional date format. There are five different date formats available:

vbGeneralDate - displays a date as a short date and a time as a long time.
vbLongDate - displays a date in the long format
vbShortDate - displays a date in the short format
vbLongTime - displays a time using the long format
vbShortTime - displays a time using the 24-hour format

The long and short formats are specified by the regional settings on the Web server. An example of using the FormatDateTime function can be seen below:

Dim dtNow
dtNow = Now()

Response.Write FormatDateTime(dtNow, vbGeneralDate) & "<BR>"
Response.Write FormatDateTime(dtNow, vbLongDate) & "<BR>"
Response.Write FormatDateTime(dtNow, vbShortDate) & "<BR>"
Response.Write FormatDateTime(dtNow, vbLongTime) & "<BR>"
Response.Write FormatDateTime(dtNow, vbShortTime) & "<BR>"

The output of the above script is:
9/24/00 3:23:48 PM
Sunday, September 24, 2000
3:23:48 PM

For more information be sure to read the technical docs! Also be sure to check out this FAQ: How can I display dates in a completely custom format (that might not be supported by FormatDateTime)?

How do I dynamically resize an array?
Submitted By: Scott Mitchell
Views: 71897
An array can be resized with the Redim statement. The Redim statement takes the following form:

Redim [Preserve] varname(subscripts)

The Preserve should be used if your array contains information that you do not wish to lose when you resize the array. However, if you use the Preserve keyword you can only resize the last dimension of the array; furthermore, use of the Preserve keyword prevents you can't from altering the number of dimensions in the array. (Of course the Preserve won't save the excess arrray information if you specify a smaller size than the orginal array.)

An example of using the Redim statement can be seen below:

'MyArray has five elements
Dim MyArray(5)

'Resize MyArray so it has 10 elements
ReDim MyArray(10)

To learn more about Redim, read the technical documents.

How can I have my ASP page send HTML-formatted email?
Submitted By: Scott Mitchell
Views: 62047
Using CDONTS, you can send email messages in HTML format. CDONTS contains two properties named BodyFormat and MailFormat. These properties should be set to the value of zero (0), and the email will be sent in HTML format! With HTML formatted email, you can embed hyperlinks and even images into your email messages!

For a great code example, be sure to check out: Sending HTML-Formatted Email by Rob Taylor!
How can I tell what version of ADO I am using?
Submitted By: Scott Mitchell
Views: 43809
Use the Version property of the ADODB.Connection object. In this quick and dirty example we'll create a Connection object and print out its Version property:

'Create Connection object
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")

'Print out version property
Response.Write("You are using ADO Version " & objConn.Version)

For more information on ADO, visit Microsoft's Universal Data Access site:

Or, to go directly to a reference manual for ADO, try this Microsoft site.

How do you send email attachments through an ASP page?
Submitted By: Scott Mitchell
Views: 111649
The CDONTS mail object can be used to send email attachments - it contains a method named AttachFile. The file you wish to attach must exist on the Web server (or be accessible on a remote machine by the IUSR_MachineName account) and the IUSR_MachineName must have Read permission on the file you wish to attach. Furthermore, you must know the file's physical path.

The AttachFile method can be used like:

CDONTS_Object.AttachFile(Physical_Path_to_File, English_Name_For_File)

Below is a sample script (written by Rob Taylor) that sends an email attachment:

  Option Explicit

  Dim objMail
  Set objMail = Server.CreateObject("CDONTS.NewMail")

  objMail.From = ""
  objMail.Subject = "A message TO you"
  objMail.To = ""
  objMail.Body = "This is the body of the Message"

  Response.write("Mail was Sent")

  'You should always do this with CDONTS.
  set objMail = nothing

If you attempt to attach a file that does not exist you will receive an Unspecified Error error message...

For more information on using CDONTS, be sure to check out the article Sending Emails in ASP Using CDONTS. You can also check out a live demo of the above script.

Happy Programming!

Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  'Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
    End If
  End Sub

  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT * FROM tblFAQ WHERE FAQID <= 20"

    'Set the datagrid's datasource to the datareader and databind
    Dim resultsDataSet as New DataSet()
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection) 

    rptFAQs.DataSource = resultsDataSet
  End Sub

<script language="JavaScript">
  function ToggleDisplay(id)
    var elem = document.getElementById('d' + id);
    if (elem) 
      if ( != 'block') 
      { = 'block'; = 'visible';
      { = 'none'; = 'hidden';

    .header { font-size: larger; font-weight: bold; cursor: hand; cursor:pointer;
               background-color:#cccccc; font-family: Verdana; }
    .details { display:none; visibility:hidden; background-color:#eeeeee; 
               font-family: Verdana; }

<asp:Repeater id="rptFAQs" runat="server">
     <div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header"
          onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
       <%# DataBinder.Eval(Container.DataItem, "Description") %>
     <div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
       <b>Submitted By:</b> <%# DataBinder.Eval(Container.DataItem, "SubmittedByName") %><br />
       <b>Views:</b> <%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:d}") %><br />
       <b>FAQ:</b><br />
       <%# DataBinder.Eval(Container.DataItem, "Answer") %>

[Return to the article]