3.0 beta 2

ASP.NET/ADO.NET example (VB)

Simple grid example in VB using ASP.NET/ADO.NET. This code was simply translated from the classic ASP example - probably there is a better/more efficient way to do the same thing in ASP.NET.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

    Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/authors.mdb")
    Dim strCommand As String = "SELECT * FROM authors"
    
    Dim oConnection As OleDbConnection
    Dim oCommand As OleDbCommand
    Dim oDataReader As OleDbDataReader
    
    Sub page_load()
       
        oConnection = New OleDbConnection(strConnection)
        oConnection.Open()
        
        oCommand = New OleDbCommand(strCommand, oConnection)
        oDataReader = oCommand.ExecuteReader()
    End Sub

    Sub page_unload()

        oDataReader.Close()
        oConnection.Close()
    End Sub

    Private Function aw_string(ByVal s As String) As String

        s = s.Replace("\", "\\")
        s = s.Replace("""", "\""") 'replace javascript control characters - ", \
        rem s = s.Replace(vbCr, "\r")
        rem s = s.Replace(vbLf, "\n")

        Return """" & s & """"
    End Function

    Function aw_headers(ByVal oDataReader As OleDb.OleDbDataReader) As String

        Dim i, count, headers()

        count = oDataReader.FieldCount
        
        ReDim headers(count - 1)

        For i = 0 To count - 1
            headers(i) = aw_string(oDataReader.GetName(i))
        Next

        Response.Write("[" & Join(headers, ", ") & "];")
        
        Return ""
    End Function

    Function aw_cells(ByVal oDataReader As OleDbDataReader) As String

        Dim i, col_count, row_count, columns(), rows()

        row_count = 0
        col_count = oDataReader.FieldCount
        
        ReDim columns(col_count - 1)

        While (oDataReader.Read())

            For i = 0 To col_count - 1
                columns(i) = aw_string(oDataReader(i).ToString())
            Next

            ReDim Preserve rows(row_count)

            rows(row_count) = vbTab & "[" & Join(columns, ", ") & "]"
            row_count = row_count + 1

        End While

        Response.Write("[" & vbNewLine & Join(rows, "," & vbNewLine) & vbNewLine & "];" & vbNewLine)
        Return ""
        
    End Function

</script>

<html>
<head>
    <script src="runtime/lib/aw.js"></script>
    <link href="runtime/styles/xp/aw.css" rel="stylesheet" />
</head>
<body>
<script>

//    insert javascript arrays produced by ASP.NET functions
    var myHeaders = <%= aw_headers(oDataReader) %>
    var myCells = <%= aw_cells(oDataReader) %>

//    create grid control
    var obj = new AW.UI.Grid;

//    set grid text
    obj.setHeaderText(myHeaders);
    obj.setCellText(myCells);

//    set number of columns/rows
    obj.setColumnCount(myHeaders.length);
    obj.setRowCount(myCells.length);

//    write grid to the page
    document.write(obj);

</script>
</body>
</html>
Alex (ActiveWidgets)
July 31,

This topic is archived.

See also:


Back to support forum

Search

Version 2

Looking for AW 2.6 ?
The old site is here.