<%@ Page Inherits="ParentPage" src="Default.aspx.cs" %>
<html><body>
<form runat="server">
<asp:Label id="lblMessage" runat="server" />
<asp:DataGrid id="DataGrid1"
runat="server"
BorderColor="black"
GridLines="Vertical"
cellpadding="4"
cellspacing="0"
width="100%"
Font-Name="Arial"
Font-Size="8pt"
HeaderStyle-BackColor="#cccc99"
ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="True" />
</asp:DataGrid>
</form>
</body></html>
File: Default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
public class ParentPage : Page {
public Label lblMessage;
public DataGrid DataGrid1;
private OleDbConnection objConn;
void Page_Load(Object Sender, EventArgs e) {
objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("EmployeeDatabase.mdb"));
if (!Page.IsPostBack) {
FillDataGrid();
}
}
private void FillDataGrid() {
FillDataGrid(-1);
}
private void FillDataGrid(int EditIndex) {
OleDbCommand objCmd = new OleDbCommand("select * from employee", objConn);
OleDbDataReader objReader;
try {
objCmd.Connection.Open();
objReader = objCmd.ExecuteReader();
DataGrid1.DataSource = objReader;
DataGrid1.DataBind();
objReader.Close();
} catch (OleDbException ex) {
lblMessage.Text = "Error retrieving from the database.";
}
objCmd.Connection.Close();
}
}
|