Looking into Strongly Typed Data Controls in ASP.Net 4.5 and Visual Studio 2012
In this post I will be demonstrating a brand new feature which available in ASP.Net 4.5.I will be demonstrating the numerous features added in .Net 4.5 in future posts. Have a a look if you want in my last post that investigates enhancements added to EF 5.0 Designer.
I will be demonstrating with a hands-on example on Strongly Typed Data controls and how we can have Intellisense and compile type checking using this new feature.
I assume you have downloaded VS 2012 (Express edition will do).I have also downloaded and installed AdventureWorksLT2012 database.You can download this database from the codeplex website.
I will be creating a simple website without using this feature. Then I will show you what the problem is without using this feature.
1) Launch VS 2012. Create a new ASP.Net Web Forms Site. Choose C# as the development language.Give an appropriate name to your site.
2) Now we will add a data access layer to our site in order to fetch some data to the user interface.I will use EF database first approach.
3) Add a new item in your project, an ADO.Net Entity Data Model. I have named it AdventureWorksLT.edmx.Then we will create the model from the database and click Next.Create a new connection by specifying the SQL Server instance and the database name and click OK.Then click Next in the wizard.In the next screen of the wizard select only the Customer table from the database and hit Finish.You will see the Customer entity in the Entity Designer.
4) Add a new web form to your site.Name is Customer.aspx.We will add a new web server control a GridView that will get the data from the database through EF.
This is the code for the web server control.I am using the Bind syntax.We are using strings to represent the property names (FirstName,LastName).
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lbName" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
5) In the Page_Load event handling routine of the Customer.aspx page type the following code
AdventureWorks2012Entities ctx = new AdventureWorks2012Entities();
var query = from cust in ctx.Customers
select new {cust.FirstName,cust.LastName};
GridView1.DataSource = query.ToList();
GridView1.DataBind();
I am not going to explain the code here. Have a look in my blog for other EF posts if you have any trouble with this.
6) Build and Run the application. You will see the data appearing on the page.
7) Now let's do a simple typo.Replace the following line
<asp:Label ID="lbName" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
with this
<asp:Label ID="lbName" runat="server" Text='<%# Bind("LLastName") %>'></asp:Label>
Build your site. It will compile. The compiler does not know anything . Guess where you will get the exception, at runtime.Run your site and you will get an exception.
8) Let's rewrite the code in the Customers.aspx page.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ItemType="AdventureWorks2012Entities.Customer">
<Columns>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Item.FirstName %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lbName" runat="server" Text='<%# Item.LastName %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now we can tell our application what type of data the control will be bound to using the ItemType
ItemType="AdventureWorks2012Entities.Customer"
Now we need to alter the code in the template. Have a look at those 2 lines
<asp:Label ID="lblName" runat="server" Text='<%# Item.FirstName %>'></asp:Label>
<asp:Label ID="lbName" runat="server" Text='<%# Item.LastName %>'></asp:Label>
Now we have compile type checking and Intellisense in our sample application.As we type the compiler informs us if it recognizes the property. This is a great enhancement since I do not want to face exceptions on runtime because of typos.
9) Build and run your application again. The sample application works fine.
Hope it helps!!!