<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nebulae Spiral &#187; WCF</title>
	<atom:link href="http://inmyshadow.net/tag/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://inmyshadow.net</link>
	<description>Spiralling Golden... and maybe blogging about code.</description>
	<lastBuildDate>Tue, 23 Dec 2008 04:17:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WCF and Ajax and LINQ to SQL, oh my!</title>
		<link>http://inmyshadow.net/2008/12/06/wcf-and-ajax-and-linq-to-sql-oh-my/</link>
		<comments>http://inmyshadow.net/2008/12/06/wcf-and-ajax-and-linq-to-sql-oh-my/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 04:14:47 +0000</pubDate>
		<dc:creator>Nebulae</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://inmyshadow.net/?p=202</guid>
		<description><![CDATA[If you haven&#8217;t taken a dive into the Windows Communication Foundation until now, this is a good place to start.  I just recently dove in myself, and I found that accessing a .svc service from Ajax isn&#8217;t as straightforward as the old .asmx services, but it&#8217;s not that complicated either.
While we&#8217;re at it, lets [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t taken a dive into the Windows Communication Foundation until now, this is a good place to start.  I just recently dove in myself, and I found that accessing a .svc service from Ajax isn&#8217;t as straightforward as the old .asmx services, but it&#8217;s not that complicated either.</p>
<p>While we&#8217;re at it, lets look at some linq too.</p>
<p>As a testament to my detest of the usual customer &#8211; order database example that has been strewn all over the internet for examples such as these, lets do something a little more interesting and fun, how about a content rating system, from the ground up?</p>
<p>For this example, we&#8217;re going to pretend that we&#8217;re working with a database that already exists.  you could reverse this action and use the visual studio interface to create your tables for you, but that&#8217;s a whole blog unto itself and I&#8217;m anxious to get to the Ajax part.  more info and an oh-so stimulating read here: <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/bb425822.aspx</a></p>
<p>Define some tables &#8211; we&#8217;ll need users, content, comments on said content, and a rating.  a comment is content in itself, isn&#8217;t it?</p>
<p>To start off, create a database called &#8220;ContentRatingExample&#8221; and run these scripts against your sql server:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #66cc66;">&#91;</span>ContentRatingExample<span style="color: #66cc66;">&#93;</span>
GO
<span style="color: #808080; font-style: italic;">/****** Object:  Table [dbo].[Users]    Script Date: 12/06/2008 18:45:35 ******/</span>
<span style="color: #993333; font-weight: bold;">SET</span> ANSI_NULLS <span style="color: #993333; font-weight: bold;">ON</span>
GO
<span style="color: #993333; font-weight: bold;">SET</span> QUOTED_IDENTIFIER <span style="color: #993333; font-weight: bold;">ON</span>
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>Users<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>
	<span style="color: #66cc66;">&#91;</span>ID<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>UserName<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>nvarchar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> COLLATE SQL_Latin1_General_CP1_CI_AS <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>DateCreated<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>datetime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
 CONSTRAINT <span style="color: #66cc66;">&#91;</span>PK_Users<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> CLUSTERED
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #66cc66;">&#91;</span>ID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>PAD_INDEX  <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> STATISTICS_NORECOMPUTE  <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> IGNORE_DUP_KEY <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> ALLOW_ROW_LOCKS  <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">ON</span><span style="color: #66cc66;">,</span> ALLOW_PAGE_LOCKS  <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">ON</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #66cc66;">&#91;</span>ContentRatingExample<span style="color: #66cc66;">&#93;</span>
GO
<span style="color: #808080; font-style: italic;">/****** Object:  Table [dbo].[Content]    Script Date: 12/06/2008 18:45:13 ******/</span>
<span style="color: #993333; font-weight: bold;">SET</span> ANSI_NULLS <span style="color: #993333; font-weight: bold;">ON</span>
GO
<span style="color: #993333; font-weight: bold;">SET</span> QUOTED_IDENTIFIER <span style="color: #993333; font-weight: bold;">ON</span>
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>Content<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>
	<span style="color: #66cc66;">&#91;</span>ID<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>CreatedBy<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>Content<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>nvarchar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>max<span style="color: #66cc66;">&#41;</span> COLLATE SQL_Latin1_General_CP1_CI_AS <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>Title<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>nvarchar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> COLLATE SQL_Latin1_General_CP1_CI_AS <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>DateCreated<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>datetime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>DateModified<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>datetime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>Parent<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
 CONSTRAINT <span style="color: #66cc66;">&#91;</span>PK_content<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> CLUSTERED
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #66cc66;">&#91;</span>ID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>PAD_INDEX  <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> STATISTICS_NORECOMPUTE  <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> IGNORE_DUP_KEY <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> ALLOW_ROW_LOCKS  <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">ON</span><span style="color: #66cc66;">,</span> ALLOW_PAGE_LOCKS  <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">ON</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #66cc66;">&#91;</span>ContentRatingExample<span style="color: #66cc66;">&#93;</span>
GO
<span style="color: #808080; font-style: italic;">/****** Object:  Table [dbo].[Ratings]    Script Date: 12/06/2008 18:46:44 ******/</span>
<span style="color: #993333; font-weight: bold;">SET</span> ANSI_NULLS <span style="color: #993333; font-weight: bold;">ON</span>
GO
<span style="color: #993333; font-weight: bold;">SET</span> QUOTED_IDENTIFIER <span style="color: #993333; font-weight: bold;">ON</span>
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>Ratings<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>
	<span style="color: #66cc66;">&#91;</span>ID<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>UserID<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>ContentID<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>uniqueidentifier<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>Rating<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>tinyint<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#91;</span>DateCreated<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>datetime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
 CONSTRAINT <span style="color: #66cc66;">&#91;</span>PK_Ratings<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> CLUSTERED
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #66cc66;">&#91;</span>ID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>PAD_INDEX  <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> STATISTICS_NORECOMPUTE  <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> IGNORE_DUP_KEY <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">,</span> ALLOW_ROW_LOCKS  <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">ON</span><span style="color: #66cc66;">,</span> ALLOW_PAGE_LOCKS  <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">ON</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>Have a look, pretty simple little database.  A table for users, a table for Content, a table for ratings.  Note that the Content table has a parent column, this will be a reference to the id of the content so we can recurse and get all the comments for a content object, and all of their comments, allowing n-depth threading.  The user db is really simplistic, we don&#8217;t care about user profiles or anything at this point other then a friendly name to display.  The rest is self explanatory.</p>
<p>Lets jump over to visual studio now and create a new project.  Select a Class Library project and call it Common, Create a new solution for it and Call that ContentRating, and make sure &#8220;Create Directory for solution&#8221; is checked.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-2.jpg"><img class="alignnone size-medium wp-image-208" title="untitled-2" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-2-300x193.jpg" alt="" width="300" height="193" /></a></p>
<p>Now, Add another project to that solution, call it &#8220;ContentRatingService&#8221; and select the type &#8220;WCF Service Application&#8221; from the Visual C# -&gt; Web Templates.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-2.jpg"><img class="alignnone size-medium wp-image-208" title="untitled-2" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-2-300x193.jpg" alt="" width="300" height="193" /></a></p>
<p>Then add a website to the solution, call it UI.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-3.jpg"><img class="alignnone size-medium wp-image-209" title="untitled-3" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-3-300x193.jpg" alt="" width="300" height="193" /></a></p>
<p>To this UI project, add a couple new folders: Includes and UserControls.  Inside the Includes folder, add 2 more folders: images and scripts.</p>
<p>your solution should now look like this:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-5.jpg"><img class="alignnone size-medium wp-image-210" title="untitled-5" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-5-120x300.jpg" alt="" width="120" height="300" /></a></p>
<p>Now we just need to do a little cleanup and the skeleton of our solution will be ready.</p>
<p>To the &#8220;Common&#8221; Project:</p>
<ol>
<li>Right click on the Common project and select properties.</li>
<li>Under the Application tab, change the Assembly name and the Default Namespace to &#8220;ContentRating.Common&#8221; and save.</li>
<li>Delete Class1.cs</li>
</ol>
<p>To the &#8220;ContentRatingService&#8221; project:</p>
<ol>
<li>Right click on the Common project and select properties.</li>
<li>Under the Application tab, change the Assembly name and the Default Namespace to to ContentRating.Service and save.</li>
<li>Delete Service1.svc and IService1.cs</li>
<li>Add a reference to the Common project</li>
</ol>
<p>To the UI project:</p>
<ol>
<li>Right click on the Common project and select properties.</li>
<li>Under the Application tab, change the Assembly name and the Default Namespace to ContentRating.UI and save &#8211; are you seeing a pattern here?</li>
<li>Delete Default.aspx</li>
</ol>
<p>Your solution should now look like:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-7.jpg"><img class="alignnone size-medium wp-image-212" title="untitled-7" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-7-106x300.jpg" alt="" width="106" height="300" /></a></p>
<p>Build the solution and make sure that the assembly names generated are ContentRating.UI.dll, ContentRating.Common.dll, and ContentRating.Service.dll</p>
<p>ok, now we can start :-)</p>
<p>The first project we&#8217;re going to modify is the &#8220;Common&#8221; project.  Right click on that bad boy and go &#8220;Add New Item&#8221;.  Select &#8220;Linq to SQL classes&#8221; and name it &#8220;ContentRatingClasses.dbml&#8221;.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-8.jpg"><img class="alignnone size-medium wp-image-214" title="untitled-8" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-8-300x181.jpg" alt="" width="300" height="181" /></a></p>
<p>open up the server explorer and right click &#8220;Data Connections&#8221; -&gt; &#8220;Add Connection&#8221;.  Select &#8220;Microsoft SQL Sever&#8221; as the Data source and the .NET framework Data Provider for SQL as the Data Provider.   Hit Continue.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-9.jpg"><img class="alignnone size-medium wp-image-215" title="untitled-9" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-9-300x135.jpg" alt="" width="300" height="135" /></a></p>
<p>select your server name, the ContentRatingExample db, and test the connection to make sure its all good, click ok.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-10.jpg"><img class="alignnone size-medium wp-image-216" title="untitled-10" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-10-202x300.jpg" alt="" width="202" height="300" /></a></p>
<p>expand and drill down the dataconnections in the server explorer and you should see your tables:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-13.jpg"><img class="alignnone size-medium wp-image-217" title="untitled-13" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-13-231x300.jpg" alt="" width="231" height="300" /></a></p>
<p>if not, you&#8217;ve done something wrong and need to fix it before continuing.</p>
<p>drag your tables onto the Object Relational Designer, and you&#8217;ll see them drawn out all pretty with the relationships defined.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-15.jpg"><img class="alignnone size-medium wp-image-219" title="untitled-15" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-15-300x99.jpg" alt="" width="300" height="99" /></a></p>
<p>** note: because I have named some of the columns similar to the table name, visual studio took it upon itself to change the names for a couple of the properties: in the Ratings table, Ratings is now Rating1, and in the Content table, Content is now Content1.  my bad.  I should have conformed to a more robust naming convention.</p>
<p>Build and then have a look at the code behind by opening ContentRatingClasses.designer.cs.  you&#8217;ll see all the tables here have been generated into classes, and you should notice a lot of opportunity in this file &#8211; a lot more than we&#8217;re going to get into today but have a look at the partial classes and the partial methods for OnCreated, OnChanging and OnChanged, etc.  obviously, if you were to create another partial class of this type on the same namespace and implement the partial method, you&#8217;d be able to execute some code on each of these events.  Keep that in mind.</p>
<p>Here is where we stop and contemplate a few things.  In our service we are yet to create, we will need to return deserialized entities to be consumed by the client, in this case an Ajax application.  There is the option of creating a custom object to populate with the result of the linq query to these objects that the dbml generated, and apply the [DataContract] and [DataMember] attributes to those objects and properties respectively, so they can be deserialized and used at the other end.  This seems a little silly though, as it would be redundant, we already have the objects and types defined, why rewrite them?  Instead, lets take the easy way out:</p>
<p>double click the ContentRatingClasses.dbml file and right click anywhere inside the designer pane that has empty space and click properties, or otherwise fine the DataContext properties for the dbml.  Set the serializationMode to Unidirectional, save, and build.</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-17.jpg"><img class="alignnone size-medium wp-image-221" title="untitled-17" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-17-300x242.jpg" alt="" width="300" height="242" /></a></p>
<p>you&#8217;ll see that all of the objects in the ContentRatingClasses.designer.cs file now have the DataContract() attribute, and the properties have the DataMember() attribute, and we&#8217;re golden :-)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #000000;">&#91;</span>Table<span style="color: #000000;">&#40;</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;dbo.Content&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Content <span style="color: #008000;">:</span> INotifyPropertyChanging, INotifyPropertyChanged
	<span style="color: #000000;">&#123;</span>
                .
                .
                .
                <span style="color: #000000;">&#91;</span>Column<span style="color: #000000;">&#40;</span>Storage<span style="color: #008000;">=</span><span style="color: #666666;">&quot;_ID&quot;</span>, DbType<span style="color: #008000;">=</span><span style="color: #666666;">&quot;UniqueIdentifier NOT NULL&quot;</span>, IsPrimaryKey<span style="color: #008000;">=</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#40;</span>Order<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Guid</span> ID
		<span style="color: #000000;">&#123;</span>
                    .
                    .
                    .</pre></div></div>

<p>Righto &#8211; onto the service.</p>
<p>in the ContentRatingService project, add a new item &#8211; select WCF Service, name it Service.svc.  You&#8217;ll notice that IService.cs and Service.svc were created and added to the project.  IService.cs behaves as any other interface, add your rules here for the services you will expose.  a default DoWork service was created for you, with the attribute OperationContract.   First off, add a reference to System.ServiceModel.Web.  This will provide us with the Attribute we need to define our response format: WebInvoke(). Add some more contracts:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ServiceModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ServiceModel.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">ContentRating.Common</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ContentRating.<span style="color: #0000FF;">Service</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>ServiceContract<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IService
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        List GetUsers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        User GetUser<span style="color: #000000;">&#40;</span>Guid id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">void</span> AddUser<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> username<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        List GetMainContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        List GetChildContent<span style="color: #000000;">&#40;</span>Guid ParentID<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">void</span> AddContent<span style="color: #000000;">&#40;</span>Guid userID, <span style="color: #FF0000;">string</span> title, <span style="color: #FF0000;">string</span> text<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">void</span> AddComment<span style="color: #000000;">&#40;</span>Guid userID, <span style="color: #FF0000;">string</span> title, <span style="color: #FF0000;">string</span> text, Guid contentID<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        List GetRatingsForContent<span style="color: #000000;">&#40;</span>Guid ContentID<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        List GetRatingsFromUser<span style="color: #000000;">&#40;</span>Guid UserID<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>WebInvoke<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat.<span style="color: #0000FF;">Xml</span>,
            BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle.<span style="color: #0000FF;">WrappedRequest</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">void</span> AddRating<span style="color: #000000;">&#40;</span>Guid contentID, Guid userID, <span style="color: #FF0000;">byte</span> rated<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The next step is to implement these members in the object:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">ContentRating.Common</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ContentRating.<span style="color: #0000FF;">Service</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Service <span style="color: #008000;">:</span> IService
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> List GetUsers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var users <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from u <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Users</span>
                             select u<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> users<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> User GetUser<span style="color: #000000;">&#40;</span>Guid id<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var user <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from u <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Users</span>
                            where u.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">==</span> id
                             select u<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> user<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddUser<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> username<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                User user <span style="color: #008000;">=</span><span style="color: #008000;">new</span> User <span style="color: #000000;">&#123;</span>DateCreated <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>, ID <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, UserName <span style="color: #008000;">=</span> username<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                ctx.<span style="color: #0000FF;">Users</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>user<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> List GetMainContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var mainContent <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from content <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Contents</span>
                                   where content.<span style="color: #0000FF;">Parent</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span>
                                   select content<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> mainContent<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> List GetChildContent<span style="color: #000000;">&#40;</span>Guid ParentID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var childContent <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from content <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Contents</span>
                                   where content.<span style="color: #0000FF;">Parent</span> <span style="color: #008000;">==</span> ParentID
                                   select content<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> childContent<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddContent<span style="color: #000000;">&#40;</span>Guid userID, <span style="color: #FF0000;">string</span> title, <span style="color: #FF0000;">string</span> text<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Content content <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Content
                                      <span style="color: #000000;">&#123;</span>
                                          ID <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                                          DateModified <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>,
                                          DateCreated <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>,
                                          CreatedBy <span style="color: #008000;">=</span> userID,
                                          Title <span style="color: #008000;">=</span> title,
                                          Content1 <span style="color: #008000;">=</span> text
                                      <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">Contents</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddComment<span style="color: #000000;">&#40;</span>Guid userID, <span style="color: #FF0000;">string</span> title, <span style="color: #FF0000;">string</span> text, Guid contentID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Content content <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Content
                                      <span style="color: #000000;">&#123;</span>
                                          ID <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                                          DateModified <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>,
                                          DateCreated <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>,
                                          CreatedBy <span style="color: #008000;">=</span> userID,
                                          Title <span style="color: #008000;">=</span> title,
                                          Parent <span style="color: #008000;">=</span> contentID,
                                          Content1 <span style="color: #008000;">=</span> text
                                      <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">Contents</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> List GetRatingsForContent<span style="color: #000000;">&#40;</span>Guid ContentID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var ratings <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from rating <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Ratings</span>
                                    where rating.<span style="color: #0000FF;">ContentID</span> <span style="color: #008000;">==</span> ContentID
                                    select rating<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> ratings<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> List GetRatingsFromUser<span style="color: #000000;">&#40;</span>Guid UserID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var ratings <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from rating <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Ratings</span>
                               where rating.<span style="color: #0000FF;">UserID</span> <span style="color: #008000;">==</span> UserID
                               select rating<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> ratings<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddRating<span style="color: #000000;">&#40;</span>Guid contentID, Guid userID, <span style="color: #FF0000;">byte</span> rated<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>ContentRatingClassesDataContext ctx
                <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Rating rating <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Rating
                                    <span style="color: #000000;">&#123;</span>
                                        ID <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                                        DateCreated <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>,
                                        ContentID <span style="color: #008000;">=</span> contentID,
                                        UserID <span style="color: #008000;">=</span> userID,
                                        Rating1 <span style="color: #008000;">=</span> rated
                                    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">Ratings</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>rating<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ctx.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>now open up the web.config in the ContentRatingService project.  near the bottom of the file, you&#8217;ll see a service and a behavior.  You&#8217;ll need to modify it to look like this:</p>
<p>&lt;system.serviceModel&gt;<br />
&lt;services&gt;<br />
&lt;service behaviorConfiguration=&#8221;ContentRating.Service.ServiceBehavior&#8221; name=&#8221;ContentRating.Service.Service&#8221;&gt;<br />
&lt;endpoint address=&#8221;" binding=&#8221;wsHttpBinding&#8221; contract=&#8221;ContentRating.Service.IService&#8221;&gt;<br />
&lt;identity&gt;<br />
&lt;dns value=&#8221;localhost&#8221;/&gt;<br />
&lt;/identity&gt;<br />
&lt;/endpoint&gt;<br />
&lt;endpoint address=&#8221;mex&#8221; binding=&#8221;mexHttpBinding&#8221; contract=&#8221;IMetadataExchange&#8221;/&gt;</p>
<p>&lt;!&#8211; the ajax endpoint &#8211;&gt;<br />
&lt;endpoint address=&#8221;ajaxEndpoint&#8221; behaviorConfiguration=&#8221;ajaxBehavior&#8221; binding=&#8221;webHttpBinding&#8221; contract=&#8221;ContentRating.Service.IService&#8221; /&gt;</p>
<p>&lt;/service&gt;<br />
&lt;/services&gt;<br />
&lt;behaviors&gt;<br />
&lt;serviceBehaviors&gt;<br />
&lt;behavior name=&#8221;ContentRating.Service.ServiceBehavior&#8221;&gt;<br />
&lt;serviceMetadata httpGetEnabled=&#8221;true&#8221;/&gt;<br />
&lt;!&#8211; changed to true so you can see anything that goes wrong &#8211;&gt;<br />
&lt;serviceDebug includeExceptionDetailInFaults=&#8221;true&#8221;/&gt;<br />
&lt;/behavior&gt;</p>
<p>&lt;/serviceBehaviors&gt;</p>
<p>&lt;!&#8211; the ajax endpoint behavior &#8211;&gt;<br />
&lt;endpointBehaviors&gt;<br />
&lt;behavior name=&#8221;ajaxBehavior&#8221;&gt;<br />
&lt;enableWebScript /&gt;<br />
&lt;/behavior&gt;<br />
&lt;/endpointBehaviors&gt;</p>
<p>&lt;/behaviors&gt;<br />
&lt;/system.serviceModel&gt;</p>
<p>and that&#8217;s all for the service.</p>
<p>I should have mentioned before,  or maybe i wanted you to get this far before breaking the news,  the Ajax implementation of this application will NOT be using the microsoft ajax dll or anything of that nature.  Ajax is easy and simple and you don&#8217;t need someone to handle it for you, and you should understand how it works from the inside anyway.</p>
<p>Go here and get the best cross browser ajax library you could need:</p>
<p><a href="http://code.google.com/p/xmlhttprequest/" target="_blank">http://code.google.com/p/xmlhttprequest/</a></p>
<p>and extract it to your Includes/scripts folder in the UI project.  This is only javascript, nothing to be scared of.  I will never understand the irrational fear of javascript some people have&#8230; anyway, add a file to your UI project, just a normal html file, call it default.html, or index.html, or whatever you want, it doesnt matter to me.  in the head section of that file, add a script reference the the XMLHttpRequest.js file you just saved.</p>
<p>Ajax is sandboxed, meaning that a request can only be made to the current server, you can&#8217;t cross domains.  to avoid any pain and suffering through running the site in debig mode by pressing F5 or play in visual studio, the next step is to create an site on localhost of the machine youre running this on so we don&#8217;t have any comminuication issues.  Go into IISManager, right click your server instance and select create new website.  go with the defaults for port 80 and set the path to be the root path of your project.  Allow anonymous access, and select Read, Run, and Browse.  Click finish to create it.  Make sure that the site is set to use asp.net 2 and not 1 in the asp.net settings tab under properties.  You should have a site that resembles this (without the resharper files unless you use resharper.. and if you dont, uhm&#8230; wtf?):</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-18.jpg"><img class="alignnone size-medium wp-image-233" title="untitled-18" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-18-300x124.jpg" alt="" width="300" height="124" /></a></p>
<p>now right click on the UI folder, click properties, click create to create it as an application.  do the same for the ContentRatingService folder.  you should have this:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-19.jpg"><img class="alignnone size-medium wp-image-234" title="untitled-19" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-19-300x202.jpg" alt="" width="300" height="202" /></a></p>
<p>surf over to <a href="http://localhost/ContentRatingService/Service.svc">http://localhost/ContentRatingService/Service.svc</a> and you should see a &#8220;Service Service&#8221; definition &#8211; ahha!  those pesky bad naming conventions habits of mine&#8230; my bad&#8230; then go here <a href="http://localhost/ContentRatingService/Service.svc?wsdl">http://localhost/ContentRatingService/Service.svc?wsdl</a> or just click the link on the svc page.  There&#8217;s a lot of crazy info in there, the blatantly useful stuff is at the bottom.</p>
<p>go back to the default.html page4 in the UI project, and add some javascript in the head (please remove the code tags, they are there so the xml doesnt get parsed out by the browser, the javascript will break if you leave them in.  ) :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> AddUser<span style="color: #009900;">&#40;</span>username<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">&lt;</span>code<span style="color: #339933;">&gt;</span>
        <span style="color: #003366; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> username <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        client.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/AddUser'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        client.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-length&quot;</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        client.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        client.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>client.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">||</span> client.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> XMLHttpRequest.<span style="color: #660066;">DONE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>client.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        client.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> GetUsers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        client.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/GetUsers'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        client.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-length&quot;</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        client.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        client.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>client.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">||</span> client.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> XMLHttpRequest.<span style="color: #660066;">DONE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>client.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        client.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>code<span style="color: #339933;">&gt;</span></pre></div></div>

<p>and then in the body tags, create a couple controls to interact with this javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=button onclick=&quot;GetUsers()&quot; value=&quot;Get Users&quot; /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;input type=text id='username' /&gt;
&lt;input type=button onclick=&quot;AddUser(document.getElementById('username').value)&quot; value=&quot;Add User&quot; /&gt;</pre></div></div>

<p>When you surf over to <a href="http://localhost/UI/default.html" target="_blank">http://localhost/UI/default.html</a> you should see:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-20.jpg"><img class="alignnone size-medium wp-image-241" title="untitled-20" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-20-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>when you click the Get Users Button, you will be alerted:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-21.jpg"><img class="alignnone size-medium wp-image-242" title="untitled-21" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-21-300x44.jpg" alt="" width="300" height="44" /></a></p>
<p>since we have no users, nothing was returned.</p>
<p>add a name to the text box and then click Add User.  you will see a blank alert, or an error.  a blank alert is good.  click Get Users again, and you will see:</p>
<p><a href="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-22.jpg"><img class="alignnone size-medium wp-image-243" title="untitled-22" src="http://inmyshadow.net/wp-content/uploads/2008/11/untitled-22-300x37.jpg" alt="" width="300" height="37" /></a></p>
<p>with the name of the user you just added.  pretty cool huh?</p>
<p>you can compare the data string sent into the ajax call to the properties that the service is expecting, and note that they are exactly the same, these are case sensitive.</p>
<p>Head back over to the ContentRatingClasses.dbml and right click on one of the tables to view code.  the file ContentRatingClasses.cs should come up, and into that file we want to implement an average rating property on the Content class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.Serialization</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ContentRating.<span style="color: #0000FF;">Common</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Content
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> AverageRating
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">partial</span> <span style="color: #0600FF;">void</span> OnLoaded<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span> ContentRatingClassesDataContext ctx <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentRatingClassesDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var ratings <span style="color: #008000;">=</span> from r <span style="color: #0600FF;">in</span> ctx.<span style="color: #0000FF;">Ratings</span>
                              where r.<span style="color: #0000FF;">ContentID</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ID</span>
                              select r<span style="color: #008000;">;</span>
&nbsp;
                var number <span style="color: #008000;">=</span> ratings.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> total <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var rate <span style="color: #0600FF;">in</span> ratings<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    total <span style="color: #008000;">+=</span> rate.<span style="color: #0000FF;">Rating1</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #FF0000;">decimal</span> average <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">decimal</span>.<span style="color: #0000FF;">TryParse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>total<span style="color: #008000;">/</span>number<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">out</span> average<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    AverageRating <span style="color: #008000;">=</span> average<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                AverageRating <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>At this point, all the functionality you will need on the server is complete.  the rest is javascript &#8211; some ajax calls and a little dhtml.</p>
<p>go back to your default.html page, and replace the script between the head tags with this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #003366; font-weight: bold;">function</span> SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> callBack<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
            client.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            client.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-length&quot;</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            client.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            client.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>client.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">||</span> client.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> XMLHttpRequest.<span style="color: #660066;">DONE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    callBack<span style="color: #009900;">&#40;</span>client.<span style="color: #660066;">responseXML</span><span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            client.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> AddComment<span style="color: #009900;">&#40;</span>title<span style="color: #339933;">,</span> comment<span style="color: #339933;">,</span> userID<span style="color: #339933;">,</span> contentID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;AddComment xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;userID&gt;'</span> <span style="color: #339933;">+</span> userID <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/userID&gt;&lt;title&gt;'</span> <span style="color: #339933;">+</span> title <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/title&gt;&lt;text&gt;'</span> <span style="color: #339933;">+</span> comment <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/text&gt;&lt;contentID&gt;'</span> <span style="color: #339933;">+</span> contentID <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/contentID&gt;&lt;/AddComment&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/AddComment'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            args<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> contentID<span style="color: #339933;">;</span>
            SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> GetAllMainContent<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> GetAllMainContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;GetMainContent xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;/GetMainContent&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/GetMainContent'</span><span style="color: #339933;">;</span>
            SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> DrawContent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> GetChildContent<span style="color: #009900;">&#40;</span>parentID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;GetChildContent xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;ParentID&gt;'</span> <span style="color: #339933;">+</span> parentID <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/ParentID&gt;&lt;/GetChildContent&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/GetChildContent'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            args<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> parentID<span style="color: #339933;">;</span>
            SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> DrawContent<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> SubmitRating<span style="color: #009900;">&#40;</span>contentID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            RateContent<span style="color: #009900;">&#40;</span>contentID<span style="color: #339933;">,</span>
                document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">options</span><span style="color: #009900;">&#91;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selectedIndex</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span>
                document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rating_'</span> <span style="color: #339933;">+</span> contentID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">options</span><span style="color: #009900;">&#91;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rating_'</span> <span style="color: #339933;">+</span> contentID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selectedIndex</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> RateContent<span style="color: #009900;">&#40;</span> contentID<span style="color: #339933;">,</span> userID<span style="color: #339933;">,</span> rated <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;AddRating xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;contentID&gt;'</span> <span style="color: #339933;">+</span> contentID <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/contentID&gt;&lt;userID&gt;'</span> <span style="color: #339933;">+</span> userID <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/userID&gt;&lt;rated&gt;'</span> <span style="color: #339933;">+</span> rated <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/rated&gt;&lt;/AddRating&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/AddRating'</span><span style="color: #339933;">;</span>
            SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> DrawContent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> AddContent<span style="color: #009900;">&#40;</span>title<span style="color: #339933;">,</span> content<span style="color: #339933;">,</span> userID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;AddContent xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;userID&gt;'</span> <span style="color: #339933;">+</span> userID.<span style="color: #660066;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/userID&gt;&lt;title&gt;'</span> <span style="color: #339933;">+</span> title <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/title&gt;&lt;text&gt;'</span> <span style="color: #339933;">+</span> content <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/text&gt;&lt;/AddContent&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/AddContent'</span><span style="color: #339933;">;</span>
&nbsp;
            SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> GetAllMainContent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> SubmitComment<span style="color: #009900;">&#40;</span>contentID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            AddComment<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title_'</span> <span style="color: #339933;">+</span> contentID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span>
                document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'reply_'</span> <span style="color: #339933;">+</span> contentID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span>
                document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">options</span><span style="color: #009900;">&#91;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selectedIndex</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span>
                contentID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> AddUser<span style="color: #009900;">&#40;</span>username<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;AddUser xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;username&gt;'</span> <span style="color: #339933;">+</span> username <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/username&gt;&lt;/AddUser&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/AddUser'</span><span style="color: #339933;">;</span>
        SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> GetUsers<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> GetUsers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;GetUsers xmlns=&quot;http://tempuri.org/&quot;&gt;&lt;/GetUsers&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> postUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://localhost/ContentRatingService/Service.svc/ajaxEndpoint/GetUsers'</span><span style="color: #339933;">;</span>
        SendRequest<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> postUrl<span style="color: #339933;">,</span> PopulateUsers<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> DrawContent<span style="color: #009900;">&#40;</span>responseXML<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> contentNodes <span style="color: #339933;">=</span> responseXML.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!!</span>contentNodes <span style="color: #339933;">&amp;&amp;</span> contentNodes.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> contentNodes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> contentNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ID'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'1px solid #CCCCCC'</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">style</span>.<span style="color: #660066;">margin</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'25px'</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">style</span>.<span style="color: #660066;">padding</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'5px'</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'b'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                b.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> contentNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Title'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                div.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; - rating: &quot;</span> <span style="color: #339933;">+</span> contentNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'AverageRating'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
&nbsp;
                div.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; - rate: &quot;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> select <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                select.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'rating_'</span> <span style="color: #339933;">+</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">11</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> option <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Option<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    select.<span style="color: #660066;">options</span><span style="color: #009900;">&#91;</span>select.<span style="color: #660066;">options</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> option<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                div.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>select<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
                div.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;a href=&quot;javascript:SubmitRating(<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>)&quot;&gt;submit&lt;/a&gt;&lt;br /&gt;'</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&lt;p&gt;&quot;</span> <span style="color: #339933;">+</span> contentNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Content1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
                div.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;br /&gt;&lt;br /&gt;reply to this:&lt;br /&gt;&lt;input type=&quot;text&quot; id=&quot;title_'</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; /&gt;&lt;br /&gt;&lt;textarea id=&quot;reply_'</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;&lt;/textarea&gt;&lt;br /&gt;'</span><span style="color: #339933;">;</span>
                div.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;a href=&quot;javascript:SubmitComment(<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>)&quot;&gt;submit&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>args <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    GetChildContent<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> parentID <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>parentID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    GetChildContent<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> PopulateUsers<span style="color: #009900;">&#40;</span>responseXML<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> userNodes <span style="color: #339933;">=</span> responseXML.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!!</span>userNodes <span style="color: #339933;">&amp;&amp;</span> userNodes.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> userNodes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> option <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Option<span style="color: #009900;">&#40;</span>userNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'UserName'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">,</span> userNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ID'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> count <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
                document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'users'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">options</span><span style="color: #009900;">&#91;</span>count<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> option<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> PrepPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        GetUsers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        GetAllMainContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then replace the body:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;body onload=&quot;PrepPage()&quot;&gt;
    be user: &lt;select id='users'&gt;&lt;/select&gt;
    &lt;input type=&quot;text&quot; id='username' /&gt;
    &lt;input type=&quot;button&quot; onclick=&quot;AddUser(document.getElementById('username').value)&quot; value=&quot;Add User&quot; /&gt;
&nbsp;
    &lt;div id='content'&gt;
&nbsp;
    &lt;/div&gt;
    &lt;br /&gt;
    &lt;br /&gt;&lt;br /&gt;new content:&lt;br /&gt;
    &lt;input type=&quot;text&quot; id=&quot;title&quot; /&gt;
    &lt;br /&gt;&lt;textarea cols='30' rows='3' id=&quot;reply&quot;&gt;&lt;/textarea&gt;
    &lt;br /&gt;&lt;input type=&quot;button&quot; onclick=&quot;AddContent(document.getElementById('title').value, document.getElementById('reply').value,document.getElementById('users').options[document.getElementById('users').selectedIndex].value)&quot; value=&quot;Add&quot; /&gt;
&nbsp;
&nbsp;
&lt;/body&gt;</pre></div></div>

<p>what you will see will be ugly but functional, and pretty much self explanitory.  </p>
<p>complete solution here, all you&#8217;ll have to do is create the db, run the scripts above, and create the sites and apps in iis:  <a href='http://inmyshadow.net/wp-content/uploads/2008/12/contentratingservice.zip'>contentratingservice</a></p>
<p>Enjoy!  any questions, requests, please comment.</p>
<p>peace,<br />
neb</p>
]]></content:encoded>
			<wfw:commentRss>http://inmyshadow.net/2008/12/06/wcf-and-ajax-and-linq-to-sql-oh-my/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
