<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>Dynamically Retrieving Data From Any OleDb Source ( List View )</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_oledb.cs" />
<script language="C#" runat="server" src="displayList.cs" />
<script language="C#" runat="server">
void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
// connect to data source
OleDbConnection myConn = new OleDbConnection (
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|" + ".mdb" );
myConn.Open ( );
// obtain tables data from schema
object [ ] restricts = new object [ ] {null, null, null, "Table"};
DataTable dbTables = myConn.GetOleDbSchemaTable (
OleDbSchemaGuid.Tables, restricts );
// bind dbTables to tblList
tblList.DataSource = dbTables;
tblList.DataBind ( );
myConn.Close ( );
// initialize fields data
getFields ( null,null );
}
}
void getFields ( object src, EventArgs e ) {
// connect to data source
OleDbConnection myConn = new OleDbConnection (
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|dbtutor.mdb" );
myConn.Open ( );
// obtain columns data from schema
object [ ] restricts = new object [ ] {
null, null, tblList.SelectedItem.ToString ( ), null};
DataTable dbFields = myConn.GetOleDbSchemaTable (
OleDbSchemaGuid.Columns, restricts );
// bind dbFields to fldList
fldList.DataSource = dbFields;
fldList.DataBind ( );
myConn.Close ( );
}
void getData ( object src, EventArgs e ) {
if ( Request.Form [ "fldList" ] != null ) {
// fetch data and display
string query = "SELECT TOP 100 " + Request.Form [ "fldList" ] + " from " +
Request.Form [ "tblList" ];
displayList ( fetchData ( query, "dbtutor" ) );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Dynamically Retrieving Data From Any <b>OleDb</b> Source ( List View ) </h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<center>
<form runat="server">
<p>Use the Control or Shift key to select multiple fields.
<br>When changing tables, please wait until fields list has changed before clicking Fetch.</p>
<b>Select Table</b> <asp:dropdownlist id="tblList" datatextfield="Table_Name"
autopostback onSelectedIndexChanged="getFields" runat="server" />
<b>Select Fields</b> <asp:dropdownlist id="fldList" datatextfield="Column_Name"
multiple runat="server" />
<input type=submit value="Fetch" onServerClick="getData" runat="server" />
<p><%= html %></p>
</form>
</center>
<% if ( html != null ) {%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
<% } %>
</body>
</html>