<%@ Page language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Dynamically Retrieving Data From Any SQL Server Source ( List View )</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script runat="server" src="fetchData_sql.cs" />
<script runat="server" src="displayList.cs" />
<script runat="server">
protected void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
// define the schema.tables query
string query = "SELECT * FROM Information_Schema.Tables WHERE Table_Type='Base Table' ORDER BY Table_Name";
// bind lstTables to reader
lstTables.DataSource = fetchReader ( query );
lstTables.DataBind ( );
// initialize fields data
getFields ( null,null );
}
}
void getFields ( object src, EventArgs e ) {
// define the schema.columns query
string query = "SELECT * FROM Information_Schema.Columns WHERE Table_Name='" +
lstTables.SelectedItem + "'";
// bind lstFields to reader
lstFields.DataSource = fetchReader ( query );
lstFields.DataBind ( );
}
void getData ( object src, EventArgs e ) {
if ( Request.Form [ "lstFields" ] != null ) {
// fetch data and display
string query = "SELECT TOP 100 [" + Request.Form [ "lstFields" ].Replace ( ",", "],[" ) + "] FROM [" + Request.Form [ "lstTables" ] + "]";
displayList ( fetchData ( query ) );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Dynamically Retrieving Data From Any <b>SQL Server</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="lstTables" datatextfield="Table_Name"
autopostback onSelectedIndexChanged="getFields" runat="server" />
<b>Select Fields</b> <asp:dropdownlist id="lstFields" 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>