<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://strangenut.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.NET Programming Blog</title><link>http://strangenut.com/blogs/clr-programming-blog/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2008 SP1 (Build: 30619.63)</generator><item><title>How to Get Started With ADO.NET Entity Framework 4 and Visual Studio 2010 (Beta 2)</title><link>http://strangenut.com/blogs/clr-programming-blog/archive/2010/01/31/how-to-get-started-with-ado-net-entity-framework-4-and-visual-studio-2010-beta-2.aspx</link><pubDate>Sun, 31 Jan 2010 23:03:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:54</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/clr-programming-blog/rsscomments.aspx?PostID=54</wfw:commentRss><comments>http://strangenut.com/blogs/clr-programming-blog/archive/2010/01/31/how-to-get-started-with-ado-net-entity-framework-4-and-visual-studio-2010-beta-2.aspx#comments</comments><description>&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;The upcoming release of the .NET Framework version 4.0 and the simultaneous release of Visual Studio 2010 comes with a host of upgrades to the Entity Framework.&amp;nbsp; The Entity Framework is Microsofts officially supported Object Relational Mapping (ORM) tool used to connect .NET based applications and websites to the databases that run them.&amp;nbsp; It provides strongly typed access to the underlying tables and comes with full integration with LINQ.&amp;nbsp; The goal of this post is to help you get your feet wet with a step by step tutorial on the Entity Framework 4 and integrating it with a simple ASP.NET based web application.&lt;/p&gt;
&lt;p&gt;What you will need to get started to follow along with this tutorial on your own and to use the associated code download:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the &lt;a target="_blank" title="Download Visual Studio 2010 Ultimate Beta 2" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=92c65d2d-0a6b-4507-a4dc-767f4cc6e823&amp;amp;displaylang=en"&gt;Visual Studio 2010 Ultimate Beta 2&lt;/a&gt; release, and do a full install.&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" title="Download SQL Server 2008 Express" href="http://www.microsoft.com/express/Database/"&gt;SQL 2008 Express&lt;/a&gt; installed locally on your development workstation&lt;/li&gt;
&lt;li&gt;&lt;a title="The optional code download for this project" href="http://strangenut.com/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.00.00.54/EntityFrame4Test.zip.zip"&gt;The optional code download of this project&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Getting Started&lt;/h2&gt;
&lt;p&gt;First, open up Visual Studio and click on File -&amp;gt; New -&amp;gt; Project, select &amp;quot;ASP.NET Web Application&amp;quot; and name it EntityFramework4Test, and click OK.&amp;nbsp; This will create a very basic web application with all of the references that we need already set.&amp;nbsp; The project will open to the source view of the Default.aspx page, with some stub code that Microsoft puts there by default.&amp;nbsp; Go ahead and clear out everything inside of the &amp;lt;asp:Content ID=&amp;quot;&lt;b&gt;BodyContent&lt;/b&gt;&amp;quot;....&amp;gt; tag.&amp;nbsp; We will be back here in a moment.&lt;/p&gt;
&lt;p&gt;Next, right click on the &lt;b&gt;EntityFramework4Test&lt;/b&gt; project in the solution explorer (the line directly below &amp;quot;Solution &amp;#39;EntityFramework4Test&amp;#39; (1 Project) at the top), and click Add -&amp;gt; New Item.&amp;nbsp; From this dialog window click on &lt;b&gt;Data&lt;/b&gt; in the left pane; this will filter down to a shorter list of items, from this list select &lt;b&gt;ADO.NET Entity Data Model&lt;/b&gt;, name it &lt;b&gt;Demo.edmx&lt;/b&gt; and click Add.&amp;nbsp; The next window will ask &amp;quot;What should the model contain?&amp;quot; and give you the choice between &amp;quot;Generate from database&amp;quot; and &amp;quot;Empty Model&amp;quot;.&amp;nbsp; Select &lt;b&gt;Empty Model&lt;/b&gt; and click Next.&amp;nbsp; The next screen will ask which data connection to use.&amp;nbsp; We&amp;#39;ll name our database here Demo, so if you have a Demo database that you are already using, just substitute what ever name you like for Demo.&amp;nbsp; Set up a new connection to this database and if it needs created, let the process create it.&amp;nbsp; When it is done, it will bring up the Demo.edmx designer window with a blank slate.&lt;/p&gt;
&lt;h2&gt;Creating the Entities&lt;/h2&gt;
&lt;p&gt;Right click on the blank designer and click &lt;b&gt;Add&lt;/b&gt; -&amp;gt; &lt;b&gt;Entity...&lt;/b&gt; leave all of the fields in their default state, except for the Entity Name field, let&amp;#39;s fill that out with &amp;quot;Person&amp;quot;.&amp;nbsp; When we do this, notice lower down on the dialog window, the &amp;quot;Entity Set&amp;quot; field.. it automatically types with you and pluralizes the word for you.&amp;nbsp; In this case, its &amp;quot;People&amp;quot; - this is the property by which we will later refer to a Collection of Person objects.&amp;nbsp; Click OK, and the entity will be added to the designer.&amp;nbsp; Repeat this process and add an entity named Group.&lt;/p&gt;
&lt;p&gt;Since these entities will refer directly to database tables, we need to construct them in a manner consistent with how we design tables.&amp;nbsp; The entities by default have a primary key defined on them called &amp;quot;Id&amp;quot;; however these fields are not automatically marked as identity.&amp;nbsp; Right click on each of the Id fields, select &lt;b&gt;Properties&lt;/b&gt; and in the Properties window, look for the field labeled &amp;quot;&lt;b&gt;StoreGeneratedPattern&lt;/b&gt;&amp;quot;&amp;nbsp; - this will indicate that the Store (data repository, i.e. database) will generate this value and that we do not need to be concerned with setting this value.&lt;/p&gt;
&lt;p&gt;To add columns to the data entities, right click on them (note: right clicking on different areas of the Entity will give you different context menus, for this part, right click on the top of the Entity and Add should be the first context menu item.&amp;nbsp; When you find the Add menu item, select Add -&amp;gt; Scalar Property. It will add the field to the entity and allow you to change the name; after you name it, you can right click on the property field and select &amp;quot;Properties&amp;quot; and change the &lt;b&gt;Type&lt;/b&gt; to what ever is appropriate for the field we&amp;#39;re adding.&lt;/p&gt;
&lt;p&gt;Add the following fields to Person:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;FirstName - give it a type of String&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;LastName - give it a type of String&lt;/li&gt;
&lt;li&gt;EmailAddress - give it a type of String&lt;/li&gt;
&lt;li&gt;DateCreated - give it a type of DateTime&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Add the following fields to Group:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Name - give it a type of String&lt;/li&gt;
&lt;li&gt;DateCreated - give it a type of DateTime&lt;/li&gt;
&lt;li&gt;Description - give it a type of String&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Next, right click on the canvas again, and select Add -&amp;gt; Association; since we only have two entities right now, the the &lt;b&gt;Entity:&lt;/b&gt; drop down will already be populated with each of the entities.&amp;nbsp; Make sure that the &lt;b&gt;Multiplicity:&lt;/b&gt; drop downs both have &lt;b&gt;* (Many)&lt;/b&gt; selected.&amp;nbsp; This will allow this to be a Many to Many association.&amp;nbsp; We now have the basic data model setup for us to use, and we just need a database to back it all up.&lt;/p&gt;
&lt;p&gt;Right click on the canvas again, and select &lt;b&gt;Generate Database from Model&lt;/b&gt; and click Finish; this will bring up a T-SQL Editor window with the code necessary to create the schema.&amp;nbsp; The only change we should make alter the behavior of the foreign keys on the PersonGroup table.&amp;nbsp; The last line of the last ALTER TABLE statement is the one we want.&amp;nbsp; Change the ON DELETE NO ACTION ON UPDATE NO ACTION to ON DELETE CASCADE ON UPDATE CASCADE.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;This will automatically clear the records from the PersonGroup table when we delete rows from either Person or Group and prevent errors from being thrown or records being orphaned.&amp;nbsp; Hit CTRL+SHIFT+E to execute the SQL, it may prompt you to connect to the database server, if so go ahead and do so.&amp;nbsp; Provided it returns a message that the Command(s) completed successfully, we are now ready to use our Demo database.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;b&gt;Note: If you are running the downloaded project, you will need complete this step yourself - my project assumes that you are using SQL Server Express 2008, and not a file based SQL Server Database.&lt;/b&gt;&lt;/i&gt;&lt;/p&gt;
&lt;h2&gt;Accessing the Data from ASP.NET&lt;/h2&gt;
&lt;p&gt;Now that our entities are set up and the database and tables are created, let&amp;#39;s open add a new Web Content Form to our web project.&amp;nbsp; Call it &lt;b&gt;AddPeopleAndGroup.aspx&lt;/b&gt; and click OK.&amp;nbsp; Inside the Page_Load method, past the following code (or reference the download project):&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span style="color:green;"&gt;//DemoEntities is the Type created that &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span style="color:green;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //is the
context to the database.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;color:blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using&lt;/span&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt;
context = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DemoEntities&lt;/span&gt;()) &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:green;"&gt;//let&amp;#39;s
create two people that we can add to a group&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Person&lt;/span&gt;
me = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Person&lt;/span&gt;()
&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{ &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FirstName = &lt;span style="color:#a31515;"&gt;&amp;quot;Ryan&amp;quot;&lt;/span&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;LastName = &lt;span style="color:#a31515;"&gt;&amp;quot;Crowley&amp;quot;&lt;/span&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;EmailAddress = &lt;span style="color:#a31515;"&gt;&amp;quot;dontspamme@gmail.com&amp;quot;&lt;/span&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DateCreated = &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Person&lt;/span&gt;
you = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Person&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FirstName = &lt;span style="color:#a31515;"&gt;&amp;quot;Your&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;LastName = &lt;span style="color:#a31515;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;EmailAddress = &lt;span style="color:#a31515;"&gt;&amp;quot;dontspamyou@gmail.com&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DateCreated = &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Group&lt;/span&gt;
ourGroup = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Group&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Name = &lt;span style="color:#a31515;"&gt;&amp;quot;Our Group!&amp;quot;&lt;/span&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;Description = &lt;span style="color:#a31515;"&gt;&amp;quot;Our Group Description!&amp;quot;&lt;/span&gt;, &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DateCreated= &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:green;"&gt;//Add
the people to the group&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ourGroup.People.Add(me);&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ourGroup.People.Add(you);&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:green;"&gt;//Now
add the newly created Group to our table of Groups&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;context.Groups.AddObject(ourGroup);&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:green;"&gt;//
this is the method call that will &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span style="color:green;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // persist the entire set of objects to the
database&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;context.SaveChanges();&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Running this code won&amp;#39;t output anything to the screen of course, but it will illustrate the simplicity of adding records to the database.&amp;nbsp; If we now go and select from all of our tables, we can see the data that we just saved.&amp;nbsp; Now, on to reading that data back out and displaying it!&lt;/p&gt;
&lt;h2&gt;Reading The Data&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s go into the Default.aspx file and add a GridView with the ID of &amp;quot;gvGroups&amp;quot; and one with the ID of &amp;quot;gvPeople&amp;quot;, and leave everything else default - this will let the GridViews auto generate the column names.&lt;/p&gt;
&lt;p&gt;Now let&amp;#39;s open up the Default.aspx.cs and paste the following code into it (again, or reference the download):&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt;
(&lt;span style="color:blue;"&gt;var&lt;/span&gt; context = &lt;span style="color:blue;"&gt;new&lt;/span&gt;
&lt;span style="color:#2b91af;"&gt;DemoEntities&lt;/span&gt;())&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;gvGroups.DataSource =
context.Groups;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;gvGroups.DataBind();&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;gvPeople.DataSource =
context.People;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;gvPeople.DataBind();&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:9.5pt;font-family:Consolas;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Voila! Running this will display two grids: one with the group that we just created and one with the people we just created.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s all for this tutorial.&amp;nbsp; Soon there will be more examples posted as well as a series involving the construction of a more complex, fully functional application.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=54" width="1" height="1"&gt;</description><enclosure url="http://strangenut.com/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.00.00.54/EntityFrame4Test.zip.zip" length="195574" type="application/zip" /><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/C_2300_+2008/default.aspx">C# 2008</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/.Net+4.0/default.aspx">.Net 4.0</category></item><item><title>ASP.NET MVC 2 and Html.RenderAction not working in Visual Studio 2010 Beta</title><link>http://strangenut.com/blogs/clr-programming-blog/archive/2010/01/21/asp-net-mvc-2-and-html-renderaction-not-working-in-visual-studio-2010-beta.aspx</link><pubDate>Fri, 22 Jan 2010 05:29:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:52</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/clr-programming-blog/rsscomments.aspx?PostID=52</wfw:commentRss><comments>http://strangenut.com/blogs/clr-programming-blog/archive/2010/01/21/asp-net-mvc-2-and-html-renderaction-not-working-in-visual-studio-2010-beta.aspx#comments</comments><description>&lt;p&gt;So this week I&amp;#39;ve been working on an ASP.NET MVC 2 application and am using VS2010 Beta, and was stumped when Html.RenderAction() didn&amp;#39;t exist!&amp;nbsp; Documentation everywhere indicates that it did in fact become part of the main MVC product, but why does it not exist?&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Well, it does, but it appears to have been moved... though I am not quite sure why this hasn&amp;#39;t been documented or even yet referenced anywhere online. &lt;/p&gt;
&lt;p&gt;At first I had registered the Microsoft.Web.Mvc namespace in my web.config.. nothing.. &lt;/p&gt;
&lt;p&gt;Registered, imported and otherwise used fully qualified paths to Microsoft.Web.Mvc directly in my Master page.&amp;nbsp; Still nothing.&lt;/p&gt;
&lt;p&gt;After 2 days of digging around for this, and coming up empty handed... I went to the source.. CodePlex.&lt;/p&gt;
&lt;p&gt;I downloaded the source for ASP.NET MVC 2 Futures and did a solution-wide search for RenderAction, and found it in the ViewExtensions class in the Microsoft.Web.Mvc namespace. Formerly, the method signature was Html.RenderAction(&amp;quot;Action&amp;quot;, &amp;quot;Controller&amp;quot;,somethingElseHere)&lt;/p&gt;
&lt;p&gt;Now it seems to require an HtmlHelper object to be passed in as the first argument... so, I&amp;#39;ve found that I needed to call it like this:&lt;/p&gt;
&lt;p&gt;&amp;lt;% ViewExtensions.RenderAction(this.Html, &amp;quot;Menu&amp;quot;, &amp;quot;Nav&amp;quot;, new { highlightCategory = ViewData[&amp;quot;CurrentCategory&amp;quot;] }); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Et voila!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=52" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/.net/default.aspx">.net</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/MVC+2/default.aspx">MVC 2</category></item><item><title>How to use the Namespace Alias in .net 3.5</title><link>http://strangenut.com/blogs/clr-programming-blog/archive/2009/02/23/how-to-use-the-namespace-alias-in-net-3-5.aspx</link><pubDate>Mon, 23 Feb 2009 19:21:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:48</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/clr-programming-blog/rsscomments.aspx?PostID=48</wfw:commentRss><comments>http://strangenut.com/blogs/clr-programming-blog/archive/2009/02/23/how-to-use-the-namespace-alias-in-net-3-5.aspx#comments</comments><description>&lt;p&gt;Recently in one application I am working on, there was a ambiguous reference between two namespaces.&amp;nbsp; One was an object that was generated by our ORM tool (EntitySpaces), and one that was in the System.Web.Security namespace.&amp;nbsp; The class in both was Roles; a namespace alias was the perfect way to resolve this.&amp;nbsp; Setting a namespace alias is easy, in the using section do this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;using&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt; WebSec = System.Web.Security;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Then in the code where you have the ambigous reference, just use the WebSec namespace as shorthand like so:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;_groupRoles = _orgManager.GetAccessGroupRoles(groupid,  WebSec.&lt;span style="color:#2b91af;"&gt;Roles&lt;/span&gt;.IsUserInRole(&lt;span style="color:#a31515;"&gt;&amp;quot;SystemAdmin&amp;quot;&lt;/span&gt;));&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;.. and that&amp;#39;s it!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=48" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/C_2300_+2008/default.aspx">C# 2008</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/.net/default.aspx">.net</category></item><item><title>How to set a date format in a gridview control in asp.net</title><link>http://strangenut.com/blogs/clr-programming-blog/archive/2009/01/10/how-to-set-a-date-format-in-a-gridview-control-in-asp-net.aspx</link><pubDate>Sun, 11 Jan 2009 02:37:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:43</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/clr-programming-blog/rsscomments.aspx?PostID=43</wfw:commentRss><comments>http://strangenut.com/blogs/clr-programming-blog/archive/2009/01/10/how-to-set-a-date-format-in-a-gridview-control-in-asp-net.aspx#comments</comments><description>&lt;p&gt;Have you ever wanted to display a datetime value in a gridview, with a specific format?&amp;nbsp; It doesn&amp;#39;t always work intuitively because the &amp;lt;asp:BoundField&amp;gt; uses HtmlEncoding by default to prevent cross site scripting ( &lt;a target="_blank" title="MSDN Reference - BoundField HtmlEncode Property " href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.htmlencode.aspx"&gt;as referenced here&lt;/a&gt; ), so DateTime formatting techniques using the &amp;quot;DataFormatString&amp;quot; attribute of the BoundField will not work.&amp;nbsp; Fixing this is fairly straight forward thoug, all you need to do is either &lt;/p&gt;
&lt;p&gt;1.) Turn off HtmlEncode for the BoundField &lt;/p&gt;
&lt;p&gt;2.) Use&amp;nbsp; &amp;lt;asp:TemplateField&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is an example using the technique to turn the HtmlEncoding off:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;:&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;BoundField&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent:0.5in;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:red;"&gt;DataField&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;=&amp;quot;EndDate&amp;quot;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:red;"&gt;HeaderText&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;End Date&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:red;"&gt;DataFormatString&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;{0:M-dd-yyyy}&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;span style="color:red;"&gt;HtmlEncode&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is an example using the &amp;lt;asp:TemplateField&amp;gt; method:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;:&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;TemplateField&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt; &lt;span style="color:red;"&gt;HeaderText&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;End Date&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent:0.5in;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;ItemTemplate&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background:yellow none repeat scroll 0% 0%;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;#&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Parse(Eval(&lt;span style="color:#a31515;"&gt;&amp;quot;EndDate&amp;quot;&lt;/span&gt;).ToString()).ToShortDateString() &lt;span style="background:yellow none repeat scroll 0% 0%;-moz-background-clip:-moz-initial;-moz-background-origin:-moz-initial;-moz-background-inline-policy:-moz-initial;"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ItemTemplate&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;:&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:#a31515;"&gt;TemplateField&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=43" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/C_2300_+2008/default.aspx">C# 2008</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/.net/default.aspx">.net</category></item><item><title>How to access a control on a Master Page from an ASP.NET page using the Master Page</title><link>http://strangenut.com/blogs/clr-programming-blog/archive/2008/11/13/how-to-access-a-control-on-a-master-page-from-an-asp-net-page-using-the-master-page.aspx</link><pubDate>Thu, 13 Nov 2008 23:47:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:38</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/clr-programming-blog/rsscomments.aspx?PostID=38</wfw:commentRss><comments>http://strangenut.com/blogs/clr-programming-blog/archive/2008/11/13/how-to-access-a-control-on-a-master-page-from-an-asp-net-page-using-the-master-page.aspx#comments</comments><description>&lt;p&gt;Today I had to figure out how to access a label control on the masterpage my page was using.&amp;nbsp; It isn&amp;#39;t quite as straightforward as one might imagine, but still not difficult.&amp;nbsp; For this, I am using C# 2008 (v3.5)&lt;/p&gt;
&lt;p&gt;For this particular page, I have an &amp;lt;asp:Label/&amp;gt;&amp;nbsp; control on the line immediately following the &amp;lt;form&amp;gt; tag in the master page. This label is going to be used for displaying general messages, errors, warnings, etc from within the code of pages using it. The problem is that you cannot just do something like &amp;quot;this.Master.FindControl(&amp;quot;LabelDisplay&amp;quot;).Text = &amp;quot;Some Text Here&amp;quot;; (At least not yet).&lt;/p&gt;
&lt;p&gt;To find it you need to walk back up inheritance chain from your present location, to the page that has the control, then find the reference to the target control, cast that reference as the appropriate type (in this particular case Label), then you have access to all of its properties and methods.&lt;/p&gt;
&lt;p&gt;Here is the code to do this:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;((&lt;span style="color:#2b91af;"&gt;Label&lt;/span&gt;)&lt;span style="color:blue;"&gt;this&lt;/span&gt;.Master.Controls[1].Controls[1].FindControl(&lt;span style="color:#a31515;"&gt;&amp;quot;MessageDisplayLabel&amp;quot;&lt;/span&gt;)).Text = &lt;span style="color:#a31515;"&gt;&amp;quot;here we are&amp;quot;&lt;/span&gt;;&lt;/span&gt;&lt;span style="font-size:11pt;font-family:Calibri;"&gt; &lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=38" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/C_2300_+2008/default.aspx">C# 2008</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://strangenut.com/blogs/clr-programming-blog/archive/tags/.net/default.aspx">.net</category></item></channel></rss>