HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

ASP.NET Web Forms - Adding a Database


Learn ASP.NET Web Forms by building a web site from scratch.

Part III: Adding a Database.


What We Will Do

In this chapter we will:

  • Create a page to list data from a database

Displaying Data from Database

With Web Forms, you can easily display data from a database.

You can connect to an existing database, or create a new database from scratch.

In this example we will connect to an existing MS Access database.

You can download the database from this link: Northwind.zip


Adding a Customers Page

In the "DemoWebForms" folder, create a new ASPX file named "Customers.aspx".

Put the following code inside the file:

Customers.aspx

<%@ Page Title="Data" MasterPageFile="Site.master" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>


<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
<h1>Customers</h1>

<%
Dim dbconn As OleDbConnection
Dim objAdapter As OleDbDataAdapter
Dim objTable As DataTable
Dim objRow As DataRow
Dim objDataSet As New DataSet()
dbconn=New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\WebData\Northwind.mdb")
objAdapter=New OledbDataAdapter("SELECT CompanyName,City,Country FROM Customers",dbconn)
objAdapter.Fill(objDataSet,"myTable")
objTable=objDataSet.Tables("myTable")
response.write("<table border='1'>")
response.write("<tr><th>Name</th><th>City</th><th>Country</th></tr>")
for each x in objTable.Rows
response.write("<tr>")
response.write("<td>" & x("companyname") & "</td>")
response.write("<td>" & x("city") & "</td>")
response.write("<td>" & x("country") & "</td>")
response.write("</tr>")
next
response.write("</table>")
dbconn.Close()
%>

</asp:Content>

Run example »


Congratulations

You have added a database to your web project.



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE