Display result
CustomerID |
CompanyName |
Country |
WHITC |
White Clover Markets |
USA |
TRAIH |
Trail's Head Gourmet Provisioners |
USA |
THECR |
The Cracker Box |
USA |
THEBI |
The Big Cheese |
USA |
SPLIR |
Split Rail Beer & Ale |
USA |
SAVEA |
Save-a-lot Markets |
USA |
RATTC |
Rattlesnake Canyon Grocery |
USA |
OLDWO |
Old World Delicatessen |
USA |
LONEP |
Lonesome Pine Restaurant |
USA |
LETSS |
Let's Stop N Shop |
USA |
LAZYK |
Lazy K Kountry Store |
USA |
HUNGC |
Hungry Coyote Import Store |
USA |
GREAL |
Great Lakes Food Market |
USA |
Source code
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SqlClient" %>
<HTML>
<HEAD>
<Title> example of searching and sorting by DataTable </title>
<Script language = "C #" runat = "server">
Void Page_Load (object sender, System. EventArgs e)
{
String ConnectionString = System. Configuration. ConfigurationSettings. etettings ["ConnectionSqlServer"];
String SQL = "SELECT CustomerID, CompanyName, Country FROM Customers ";
SqlConnection thisConnection = new SqlConnection (ConnectionString );
SqlDataAdapter adapter = new SqlDataAdapter (SQL, thisConnection );
// Create a able object
DataTable table = new DataTable ();
// Fill in the data to the DataTable
Adapter. Fill (table );
// Define the filter condition string and sort string
String strExpr = "Country = 'usa '";
String strSort = "CompanyName DESC ";
// Obtain filtered and sorted data
DataRow [] resultRows = table. Select (strExpr, strSort );
// Display filtered and sorted data
DisplayRows (resultRows, DisplayLabel );
}
// Display the content in the DataRow Array
Public void DisplayRows (DataRow [] rows, Label label)
{
// Check whether the returned data is empty
If (rows. Length <= 0)
{
Label. Text = "no data ";
Return;
}
Label. Text = "";
// Traverse the rows and columns of the DataRow array to display data
Label. Text + = "<Table border = '1'> ";
Label. Text + = "<TR> <TH> CustomerID </TH> <TH> CompanyName </TH> <TH> Country </TH> </TR> ";
Foreach (DataRow row in rows)
{
Label. Text + = "<TR> ";
For (int I = 0; I <row. Table. Columns. Count; I ++)
{
Label. Text + = "<TD> ";
Label. Text + = row [I];
Label. Text + = "</TD> ";
}
Label. Text + = "</TR> ";
}
Label. Text + = "</Table> ";
}
</Script>
</HEAD>
<Body>
<Form id = "Form1" method = "post" runat = "server">
<H3> example of searching and sorting by DataTable </H3>
<Asp: Label id = "DisplayLabel" runat = "server"> Label </asp: Label>
</Form>
</Body>
</HTML> for the DataTable. Select method [C #], see