Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Displaying Session and Application variables

Often when using ASP or Active Server Pages you will find it necessary to do some troubleshooting. Below is some neat code you can run that will show you all the current Session and Application variables and really give you a good idea of what sort of information is being saved in them. At the bottom of this page we also show you a way to erase/clear all this information all at once.

<font face=arial size=1>
Session Variables - <% =Session.Contents.Count %> Found<br><br>
<%
Dim item, itemloop
For Each item in Session.Contents
  If IsArray(Session(item)) then
    For itemloop = LBound(Session(item)) to UBound(Session(item))
%>
<% =item %>  <% =itemloop %> <font color=blue><% =Session(item)(itemloop) %></font><BR>
<%
    Next
  Else
%>
<% =item %> <font color=blue><% =Session.Contents(item) %></font><BR>
<%
  End If
Next
%>

<hr>

Application Variables - <% =Application.Contents.Count %> Found<br><br>
<%
For Each item in Application.Contents
  If IsArray(Application(item)) then
    For itemloop = LBound(Application(item)) to UBound(Application(item))
%>
<% =item %>   <% =itemloop %> <font color=blue><% =Application(item)(itemloop) %></font><BR>
<%
    Next
  Else
%>
<% =item %> <font color=blue><% =Application.Contents(item) %></font><BR>
<%
  End If
Next
%>
</font>

Additionally, here is some handy code you can run that will wipe that information clean.

<%
Session.Abandon
Application.Contents.RemoveAll()
%>

asp :: calculate age

asp :: calculate age

Function Age(ByVal dtBirthDate As Date) As Integer
    Dim intAge As Integer
    intAge = DateDiff(DateInterval.Year, dtBirthDate, Today())
    If Today() < DateSerial(Year(Today()), Month(dtBirthDate), Day(dtBirthDate)) Then
        intAge = intAge - 1
    End If
    Return intAge
End Function 

asp :: list database tables

asp :: list database tables

<%@ Page Language="VB" %>
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cnnTables As New System.Data.SqlClient.SqlConnection
        cnnTables.ConnectionString = "Data Source=localhost;Initial Catalog=DB_NAME;User ID=USER;Password=PASS;"

        cnnTables.Open()

        gridTables.DataSource = cnnTables.GetSchema("Tables")
        gridTables.DataBind()

        cnnTables.Close()
    End Sub

</script>
<html>
<head>
    <title>List Tables In Database</title>
</head>
<body>
<form id="myForm" runat="server">

<asp:GridView ID="gridTables" runat="server" />

</form>
</body>
</html> 

Selected items from a multiple select or checkboxes in VB.net

My co-worker found this code for VB.net that is used to access selected items from check boxes or multiple select boxes. If you've been wondering how to do this, this may help :)

The URL is http://dotnetjunkies.com/weblog/davetrux/archive/2003/11/14/3557.aspx

// The results of a mulitple-select list are also a NameValueCollection
Request.Form.GetValues(i)
// and the individual items are accessed like this:
Request.Form.GetValues(i)(j)

ASP/VBScript ADO parameterized query


use like:

Set rs = execute_query(conn, "SELECT custid, custname FROM customers WHERE (somefield > ?) AND (someotherfield < ?)", Array(someValue, someOtherValue));

where conn is a "ADODB.Connection"

Function create_variant_input_parameter(command, name, value)
  Dim param
  ' 12 -> adVariant
  ' 1 -> adParamInput
  Set param = command.CreateParameter(name, 12, 1, 0, value)  
  Set create_variant_input_parameter = param
End Function

Function execute_query(connection, querytext, parameters)
  Dim cmd, i, rs
  Set cmd = Server.CreateObject("ADODB.Command")
  cmd.CommandText = querytext
  ' 1 -> adCmdText
  cmd.CommandType = 1
  For i = 0 To UBound(parameters)
    cmd.Parameters.Append(create_variant_input_parameter(cmd, "", parameters(i)))    
  Next
  Set cmd.ActiveConnection = connection 
  Set rs = cmd.Execute()
  Set execute_query = rs
End Function

ASP/JScript ADO parameterized query

ASP/JScript ADO parameterized query.

use like:

var rs = executeQuery(conn, "SELECT custid, custname FROM customers WHERE somefield > ?", [someValue]);

where conn is a "ADODB.Connection"

Requires that you first include the "adojavas.inc" include file to get definitions for the adXXX constants.

function executeQuery(conn, qry, params) {
  if (arguments.length == 2) { params = []; }
  var cmd, i;
  cmd = Server.CreateObject("ADODB.Command");
  cmd.CommandText = qry;
  cmd.CommandType = adCmdText;
  for (i = 0; i < params.length; i++) {
    cmd.Parameters.Append(cmd.CreateParameter("", adVariant, adParamInput, 0, params[i]));
  }
  cmd.ActiveConnection = conn;
  return cmd.Execute();
}