Create a New ASP.NET Web Application
- Start Microsoft Visual Studio .NET.
- Create a new ASP.NET Web Application by using Microsoft Visual C# .NET
or Microsoft Visual Basic .NET. Name the project as WebApp1. By
default, WebForm1.aspx is created.
- Double-click WebForm1.aspx. The code-behind page is displayed.
- Add the following namespace reference at the beginning of
the code-behind class file:
Visual C# .NET Code
Visual Basic .NET CodeImports System.Data.OleDb
- Replace the Page_Load event handler with the following
code:
Visual C# .NET Code
private void Page_Load(object sender, System.EventArgs e)
{
// Use a string variable to hold the ConnectionString property.
string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=C:\\Program Files\\Microsoft Visual Studio\\VB98\\NWIND.MDB";
OleDbConnection cn = new OleDbConnection(connectString);
//Open the connection.
cn.Open();
// Use a variable to hold the SQL statement.
string selectString = "SELECT CustomerID, ContactName FROM Customers";
// Create an OleDbCommand object.
OleDbCommand cmd = new OleDbCommand(selectString,cn);
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Response.Write(reader["CustomerID"].ToString()+ " : "+reader["ContactName"].ToString()+"<br>");
}
// Close the reader and the related connection.
reader.Close();
cn.Close();
}
Visual Basic .NET CodePrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Use a string variable to hold the ConnectionString property.
Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\\Program Files\\Microsoft Visual Studio\\VB98\\NWIND.MDB"
Dim cn As OleDbConnection = New OleDbConnection(connectString)
'Open the connection.
cn.Open()
'Use a variable to hold the SQL statement.
Dim selectString As String = "SELECT CustomerID, ContactName, FROM Customers"
Dim cmd As OleDbCommand = New OleDbCommand(selectString, cn)
Dim reader As OleDbDataReader = cmd.ExecuteReader()
While (reader.Read())
Response.Write(reader("CustomerID").ToString() + " : " + reader("ContactName").ToString() + "<br>")
End While
'Close the reader and the related connection.
reader.Close()
cn.Close()
End Sub
Note Modify the connectString variable at the beginning of the code to
point to the location of your Northwind database.
Set Impersonation
- In Solution Explorer, double-click the Web.config file.
- Under the <System.web> section, add the following
element for impersonation:
<identity impersonate="true" />
- Save and close the Web.config file.
Run the ASP.NET 1.0 Application
On the
Build menu, click
Start.
Upgrade from .NET Framework 1.0 to .NET Framework 1.1
To download and to install the .NET Framework 1.1, visit the
following Microsoft Web site:
Run the ASP.NET 1.1 Application
To run the WebApp1 application, type the following URL in
Microsoft Internet Explorer:
http://locahost/WebApp1/WebForm1.aspx
You may notice the error message that is mentioned in the "Symptoms" section of this
article.