School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,739 people online right now. Registration is fast and FREE... Join Now!




Working with asp:Table w/ C# & LINQ

 
Reply to this topicStart new topic

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

W3bDev
Group Icon



post 13 Oct, 2009 - 06:11 AM
Post #1


For a while now, I wanted to learn how to use the asp.net 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:

CODE

    [Table(Name="Customers")]
    public class Customers
    {
        [Column(IsPrimaryKey=true)]
        public int customerID;
        [Column]
        public string firstName;
        [Column]
        public string lastName;
        [Column]
        public string phone;
    }


QUOTE(hint)
The following code goes in the Page_Load event handler



Step 2: Setup your DataContext:

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



Step 4: Create instance of customer class:

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



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


Step 5: Get results using LINQ:

CODE

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:

CODE

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


QUOTE(note)

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:

CODE

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:

CODE

      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:

CODE

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


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

CODE

TableRow row1 = new TableRow();


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

CODE

      row1.Cells.Add(cell1);
      row1.Cells.Add(cell2);
      row1.Cells.Add(cell3);
      row1.Cells.Add(cell4);


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

CODE

      custTable.Rows.Add(row1);
}



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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 04:09PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month