Microsoft ASP.NET


ASP.NET, also known as ASP+, is Microsoft's most recent version of ASP and is a feature of Microsoft's IIS (Internet Information Server). Like ASP, ASP.NET enables customization of HTML pages using database queries. ASP.NET, however, supports code written in Visual Basic, C++, C# and Perl. ASP.NET files end with the .aspx extension.
 

This section is designed to provide an overview of what you can do with ASP.NET. For detailed information, visit http://www.asp.net.

Viewing error messages

In order for you to see the errors generated by the server for your .aspx files, you will need to create a Web.Config file and place the following code within it.

<!-- Web.Config Configuration File -->

<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

This will enable you to view the errors generated by the server.

Querying databases

You can use ASP.NET to query a database. The following code illustrates one way to do this. To see this code "in action," visit your SiteSamples at http://www.[yourdomain.com]/sitesamples/.

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

<script language="VB" runat="server">
Sub Page_Load(Src as object, E as EventArgs)
If rblLTorGT.SelectedIndex = -1 Then rblLTorGT.SelectedIndex = 0
End Sub

Sub btnRunQuery_OnClick(Sender As Object, E As EventArgs)
Dim objConnection As OleDbConnection
Dim objCommand As OleDbCommand
Dim objDataReader As OleDbDataReader
Dim strSQLQuery As String

objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("database.mdb") & ";")

strSQLQuery = "SELECT * FROM sample " & _
"WHERE (Sales " & rblLTorGT.SelectedItem.Value.ToString() & " " & txtSalesAmount.Text.ToString() & ") " & _
"ORDER BY " & ddlSortBy.SelectedItem.Value.ToString() & ddlSortOrder.SelectedItem.Value.ToString()

objCommand = New OleDbCommand(strSQLQuery, objConnection)

objConnection.Open()

objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

lblSQLCommandLabel.Text = strSQLQuery

EmployeeDataGrid.DataSource = objDataReader
EmployeeDataGrid.DataBind()
End Sub
</script>

<form action="database.aspx" method="post" runat="server">
<strong>Sales Information:</strong><br />

<table border="0">
<tr><td>
<asp:RadioButtonList id="rblLTorGT" runat="server">
<asp:ListItem value="<" runat="server">Less Than</asp:ListItem>
<asp:ListItem value=">" runat="server">Greater Than</asp:ListItem>
</asp:RadioButtonList>
</td><td>
<asp:TextBox id="txtSalesAmount" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validSalesEntry" controlToValidate="txtSalesAmount"
errorMessage="You must enter a sales amount!"
display="dynamic"
/>
<asp:RangeValidator runat="server"
id="validSalesAmount" controlToValidate="txtSalesAmount"
type="Integer" minimumValue="-32000" maximumValue="+32000"
errorMessage="You must enter an integer!"
display="dynamic"
/>
</td></tr>
</table>

<strong>Sort By:</strong><br />

<asp:DropDownList id="ddlSortBy" runat="server">
<asp:ListItem value="id" >Id</asp:ListItem>
<asp:ListItem value="last_name" >Last Name</asp:ListItem>
<asp:ListItem value="first_name" >First Name</asp:ListItem>
<asp:ListItem value="sales">Sales</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList id="ddlSortOrder" runat="server">
<asp:ListItem value=" ASC" >Ascending</asp:ListItem>
<asp:ListItem value=" DESC">Descending</asp:ListItem>
</asp:DropDownList>

<asp:Button type="submit" id="btnRunQuery" text="Run Query" OnClick="btnRunQuery_OnClick" runat="server" />
</form>

<p class="body">
Results of:<asp:label id="lblSQLCommandLabel" runat="server" />
</p>

<asp:DataGrid id="EmployeeDataGrid" runat="server"
cellspacing="1"
cellpadding="2"
HeaderStyle-Font-Bold="True"
ToolTip="This is Cool!"
MaintainViewState="false"
/>

Working with variable values

You can use ASP.NET to take a variable containing data and convert that data to a specified type. The following code illustrates one way to do this. To see this code "in action," visit your SiteSamples at http://www.[yourdomain.com]/sitesamples/.

<script language="VB" runat="server">
' Takes a variable containing some data and attempts to convert
' it to the requested type. If it succeeds it returns the
' resulting value otherwise it returns "N/A".
Function ConvertToType(strInput As String, strType As String) As String
Dim vTemp As Object ' Variant is no longer supported!

' Catch exceptions for the ones that don't convert
Try
' Do the appropriate conversion
Select Case LCase(strType)
Case "bool", "boolean"
vTemp = CBool(strInput)
Case "byte"
vTemp = CByte(strInput)
Case "char"
vTemp = CChar(strInput)
' Support for currency as a data type seems to be gone
'Case "cur", "currency"
' vTemp = CCur(strInput)
Case "date"
vTemp = CDate(strInput)
Case "dbl", "double"
vTemp = CDbl(strInput)
Case "dec", "decimal"
vTemp = CDec(strInput)
Case "int", "integer"
vTemp = CInt(strInput)
Case "lng", "long"
vTemp = CLng(strInput)
Case "short"
vTemp = CShort(strInput)
Case "sng", "single"
vTemp = CSng(strInput)
Case "str", "string"
vTemp = CStr(strInput)
Case Else
' If the specified type isn't handled error out.
Throw New ArgumentException("The type specified is not handled by this conversion routine.")
End Select
Catch e As Exception
' Some debugging text... Should probably be done with
' Trace.Write, but I haven't had a chance to play with it much
'Response.Write("The following error occured:" & "<br />")
'Response.Write(e.ToString() & "<br />")

' If the specified conversion failed set our return
' value to something we can check for.
vTemp = "N/A"
' I've got nothing to do in the finally so I leave it out
'Finally
End Try

' Set return value
ConvertToType = CStr(vTemp)
End Function

Sub btnDoConversions_OnClick(Source As Object, E As EventArgs)
' The string that holds the text typed into the form
Dim strInput As String = "0"

strInput = txtInput.Text

'Response.Write(strInput & "<br />")

lblBoolean.Text = ConvertToType(strInput, "Boolean")
lblByte.Text = ConvertToType(strInput, "Byte")
lblChar.Text = ConvertToType(strInput, "Char")
lblDate.Text = ConvertToType(strInput, "Date")
lblDecimal.Text = ConvertToType(strInput, "Decimal")
lblDouble.Text = ConvertToType(strInput, "Double")
lblInteger.Text = ConvertToType(strInput, "Integer")
lblLong.Text = ConvertToType(strInput, "Long")
lblShort.Text = ConvertToType(strInput, "Short")
lblSingle.Text = ConvertToType(strInput, "Single")
lblString.Text = ConvertToType(strInput, "String")

End Sub
</script>