Page 1 of 1

Working with asp:Table w/ C# & LINQ Basic tutorial working with LINQ to SQL and ASP.NET Table Control

#1 W3bDev   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Posted 13 October 2009 - 07:11 AM

For a while now, I wanted to learn how to use the table control, but every article I came across tended to be very complex, and didn't really cover the point well. This example uses LINQ to hook up to the database, as I've been trying to practice using LINQ more. You can use regular SQL commands to produce a result set and fill the table.

In this example we will be using a Table name of "Customers" from a fictional database.

Step 1: Build your data class:

	public class Customers
		public int customerID;
		public string firstName;
		public string lastName;
		public string phone;

hint said:

The following code goes in the Page_Load event handler

Step 2: Setup your DataContext:

DataContext db = new DataContext("Server=localhost\\SqlExpress;Database=myDatabaseName;Integrated Security=True");

Step 4: Create instance of customer class:

Table<Customers> customerTable = db.GetTable<Customers>();

hint said:

To utilize linq, ensure that the following are included:
using System.Data.Linq;
using System.Data.Linq.Mapping;

Step 5: Get results using LINQ:

var resultSet = from c
in Customers 
select c;

Step 6: Setup your table on your aspx page:

Since we are going to start accessing and adding to the table on your aspx page, if you have not yet created your table, then this is the time it is needed. In this example we have four columns, so lets setup the headers and get the table ready for output:

	<asp:Table ID="custTable" runat="server">
			<asp:TableHeaderCell>Customer ID</asp:TableHeaderCell>
			<asp:TableHeaderCell>First Name</asp:TableHeaderCell>
			<asp:TableHeaderCell>Last Name</asp:TableHeaderCell>
			<asp:TableHeaderCell>Phone Number</asp:TableHeaderCell>

note said:

Now that the headers are setup, additional formatting can be done to style the table. In this tutorial we will not go over advanced styling of our table.

Step 7: Loop through results and create rows:

First, since we will be looping through the result set, we need to create a for loop:

foreach (var customerResult in resultSet) // resultSet from step 5

Now within the loop...

asp:Tables are built to logic. Each row contains cells, in our example we have four cells, so each row we fill will need to have four cells. Lets set up these cells:

	  TableCell cell1 = new TableCell();
	  TableCell cell2 = new TableCell();
	  TableCell cell3 = new TableCell();
	  TableCell cell4 = new TableCell();

Now that we have our cells, it's time to fill the cells with relative information:

	  cell1.Text = Convert.ToString(customerID);
	  cell2.Text = customerResult.firstName;
	  cell3.Text = customerResult.lastName;
	  cell4.Text =;

Now we have all of the data required to add a row to the table, lets create a row:

TableRow row1 = new TableRow();

And add the cell's we have previously created to the row:


All that is left to do is add the row to the table, and close the for loop:


There you have it, not too hard to utilize the asp:Table control is it? I hope this tutorial has helped you understand the control a little better than the documentation I found for it.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1