<?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>allen\'s codeSource</title>
	<atom:link href="http://allenringgold.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://allenringgold.com</link>
	<description>Tips, Tricks, Code Samples</description>
	<lastBuildDate>Sat, 12 Sep 2009 03:34:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Search and Filter controls on an asp.net GridView/DetailsView Header Row (breaking out of a NamingContainer)</title>
		<link>http://allenringgold.com/?p=244</link>
		<comments>http://allenringgold.com/?p=244#comments</comments>
		<pubDate>Sat, 12 Sep 2009 03:29:44 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Other Tech]]></category>
		<category><![CDATA[.net 3.5]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[autoCompleteExtender]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[EntityDataSource]]></category>
		<category><![CDATA[gridview]]></category>
		<category><![CDATA[key/value]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=244</guid>
		<description><![CDATA[This post will explain two different ways to bind a GridView (or DetailsView, or any other NamingContainer) to its EntityDataSource when your filtering/search controls are in the header row.  This method can be used to break out of pretty much any NamingContainer and assign values to a control that's on the form (same level as your dataSource).]]></description>
			<content:encoded><![CDATA[<p>In this post we&#8217;re going to discuss breaking out of a naming container to set the value of a hiddenField for binding to a dataSource.  The example will deal specifically with putting search and filter controls on the HeaderRow/HeaderTemplate of an asp.net GridView, but the applications go far beyond that.</p>
<p>To start, let&#8217;s have a look at our desired gridview filtering controls, starting with the AjaxControlToolkit AutoCompleteExtender deep-text search textBox discussed in <a title="Deep-text multi-field search using autoComplete" href="http://allenringgold.com/?p=227" target="_blank">this post</a>:</p>
<div id="attachment_228" class="wp-caption alignnone" style="width: 584px"><img class="size-full wp-image-228" title="TripGridviewInitial" src="http://allenringgold.com/wp-content/uploads/2009/09/TripGridviewInitial.JPG" alt="This is a gridview with search and filtering controls in the header" width="574" height="341" /><p class="wp-caption-text">This is a gridview with search and filtering controls in the header</p></div>
<p>As discussed, the search textbox searches multiple fields in our entity to help the user find the desired record:</p>
<div id="attachment_232" class="wp-caption alignnone" style="width: 363px"><img class="size-full wp-image-232" title="FilteredOnDate" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnDate.JPG" alt="Looking for all trips in October" width="353" height="131" /><p class="wp-caption-text">Looking for all trips in October</p></div>
<p>Ordinarily, the problem you encounter when trying to put search/filter controls in a HeaderTemplate is that you can&#8217;t set your DataSource&#8217;s whereParameter ControlID to a control on a gridview, because they&#8217;re not in the same scope (NamingContainer), you&#8217;ll get an error like this:</p>
<p>System.InvalidOperationException: Could not find control &#8216;yourControlID&#8217; in ControlParameter &#8216;yourParam&#8217;</p>
<p>If you&#8217;ve ever done a view source on an aspx page, you&#8217;ll notice that a control you made like this in Visual Studio:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">asp</span><span style="color: #0000ff;">:</span><span style="color: #800000;">HiddenField</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtTripId_hidden"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #0000ff;">/&gt;</span></pre>
</div>
<p>Looks something like this after it&#8217;s rendered in the browser:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre>&lt;input type="hidden" id="ctl00__yourForm_txtTripId_hidden" /&gt;</pre>
</div>
<p>Basically, any asp.net control that&#8217;s nested inside a NamingContainer (gridviewrow, content, etc) is going to have a unique ID generated at runtime.  That&#8217;s why you&#8217;re able to set the id of multiple textboxes to the same value if they&#8217;re on different templates of a gridviewrow.  If your hiddenfield is in a different container than your datasource, the datasource can&#8217;t &#8220;see&#8221; your hiddenfield.</p>
<p>We&#8217;re going to discuss two different approaches to this problem, and both build on what I outlined in the post on <a title="key/value pairs form an ajax AutoCompleteExtender" href="http://allenringgold.com/?p=156" target="_blank">retrieving key/value pairs from an AutoCompleteItem</a>.</p>
<blockquote><p>Method 1 &#8211; Putting a hiddenField on your form and using the EntityDataSource&#8217;s  WhereParameter:</p></blockquote>
<p>Note the javascript in that post, because we&#8217;re going to be adding to it for this method.  I&#8217;m not going to re-type it all here, but you can stick the following snippet into the &#8220;autoCompleteItemSelected&#8221; method and be on your way:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre><span style="color: #008000;">//the underscore is a token in the id of the rendered control:</span>
<span style="color: #0000ff;">var</span> splitSourceId = source.get_id().split(<span style="color: #800000;">'_'</span>);

<span style="color: #0000ff;">if</span>(splitSourceId.length &gt; 2) {
     <span style="color: #008000;">//this is just based on the namingConventions,
     //    try alert(splitSourceId) to see why we do this:</span>
     <span style="color: #0000ff;">var</span> strippedSourceId = splitSourceId[0] + <span style="color: #800000;">'__'</span> + splitSourceId[2] + <span style="color: #800000;">'_'</span> + _
           splitSourceId[splitSourceId.length-1] + <span style="color: #800000;">'_hidden'</span>;

     <span style="color: #008000;">//try to find the hiddenField that's outside the gridview:</span>
     <span style="color: #0000ff;">var</span> outerHidden = document.getElementById(strippedSourceId);
     <span style="color: #0000ff;">if</span>(outerHidden)
          outerHidden.value = eventArgs.get_value();
}</pre>
</div>
<p>The nice thing about this is, you add it to your shared javascript function, and it doesn&#8217;t interfere with anything.  I was using the function on 3 other pages before I added this, and it&#8217;s inconsequential if you&#8217;re not trying to use it.</p>
<p>So what we&#8217;ve just done there is set a root-level control (hiddenField)&#8217;s value to our Id that we want to filter on.  Now we can reference the hiddenField in our EntityDataSource and be good to go!  For this trip gridview, our dataSource will look like so:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: blue;">&lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: red;">ID</span><span style="color: blue;">="entTripSource"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"</span>
<span style="color: red;"> ConnectionString</span><span style="color: blue;">="name=BusLineEntities"</span>
<span style="color: red;"> DefaultContainerName</span><span style="color: blue;">="BusLineEntities"</span>
<span style="color: red;"> EntitySetName</span><span style="color: blue;">="ExecutiveTrips"</span>
<span style="background: yellow none repeat scroll 0% 0%; color: red;"> AutoGenerateWhereClause</span><span style="background: yellow none repeat scroll 0% 0%; color: blue;">="true"</span><span style="color: blue;">&gt;</span>
<span style="color: blue;"> &lt;</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span><span class="yellowBack">
<span style="color: blue;"> &lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">ControlParameter</span> <span style="color: red;">ControlID</span><span style="color: blue;">="txtTripSearch_hidden"</span>
<span style="color: red;">Type</span><span style="color: blue;">="Int32"</span> <span style="color: red;">Name</span><span style="color: blue;">="Id"</span> <span style="color: blue;">/&gt;</span></span>
<span style="color: blue;"> &lt;/</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: blue;">&gt;</span></pre>
</div>
<p>You might notice it&#8217;s not posting back like you&#8217;d expect.  You need to cause the postback at some point, which I&#8217;ve done by using the TextChanged event of our input textbox (this makes it immediate when the user selects an item from the autoComplete dropdown, no &#8220;apply filter&#8221; button is needed).</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #993300;">asp</span><span style="color: #0000ff;">:</span><span style="color: #993300;">TextBox</span> <span class="yellowBack"><span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtPassenger_input"</span></span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span>
        <span style="color: #ff0000;">AutoPostBack</span><span style="color: #0000ff;">="true"</span> <span style="color: #ff0000;">OnTextChanged</span><span style="color: #0000ff;">="someMethod"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="200px" /&gt;</span></pre>
</div>
<p>That works very well for when you only want to search.  In some cases, though, you want even more functionality, like filtering on a range of values, that&#8217;s where method 2 comes in.</p>
<blockquote><p>Method 2:</p></blockquote>
<p>Coming soon&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=244</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deep-text search multiple fields with your AJAX AutoCompleteExtender web service</title>
		<link>http://allenringgold.com/?p=227</link>
		<comments>http://allenringgold.com/?p=227#comments</comments>
		<pubDate>Sat, 12 Sep 2009 02:12:20 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Other Tech]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asmx]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[autoCompleteExtender]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=227</guid>
		<description><![CDATA[This post will outline how to write a webservice for an AJAX AutoCompleteExtender that searches multiple database fields/entity scalar properties.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve used the AjaxControlToolkit&#8217;s AutoCompleteExtender, you&#8217;re no doubt pleased by the fact you can now replace cumbersome dropdowns with slick, user-friendly auto-complete textboxes.  To take it one step further, you can actually replace multiple search fields with a single auto-complete textbox, with only a little extra code.</p>
<p>In this example, we&#8217;re going to be searching our Bus Line database for executive travel.  Our imaginary sub-system tracks business trips made by the CEO, CIO, CTO, and CFO.</p>
<p>If we have a gridview with all the business trips for a given date range, there could be many pages to look through to find the trip you&#8217;re looking for.  It&#8217;s only nice to allow our users to search the gridview based on any number of criteria.</p>
<p>For example, with a single textbox, we can allow the user to search by trip name, the person&#8217;s name, the person&#8217;s title, trip start date, trip destination, airline, etc.</p>
<p>Let&#8217;s look first at our sample executive travel entity and a gridview to display our trips (obviously this is not normalized, etc&#8230;):</p>
<div id="attachment_240" class="wp-caption alignnone" style="width: 152px"><img class="size-full wp-image-240" title="TripEntitySample" src="http://allenringgold.com/wp-content/uploads/2009/09/TripEntitySample.JPG" alt="An example of our Trip entity" width="142" height="231" /><p class="wp-caption-text">An example of our Trip entity</p></div>
<div id="attachment_228" class="wp-caption alignnone" style="width: 584px"><img class="size-full wp-image-228" title="TripGridviewInitial" src="http://allenringgold.com/wp-content/uploads/2009/09/TripGridviewInitial.JPG" alt="Our trips span 3 pages" width="574" height="341" /><p class="wp-caption-text">Our trips span 3 pages</p></div>
<p>So you know what the code is doing when you get to that point, I&#8217;ll go ahead and post all of the AutoCompleteExtender dropdown screenshots now.  Keep in mind this is one textbox wired to one AutoCompleteExtender calling one method of one webservice.  There are no tricky code-arounds or messy overloads.</p>
<p>The idea is that the user can type in anything they know about a trip&#8230;who&#8217;s on it, when it is, where it&#8217;s going, etc, and then choose the desired trip from the dropdown.</p>
<div id="attachment_237" class="wp-caption alignnone" style="width: 366px"><img class="size-full wp-image-237" title="FilteredOnTripNumber" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnTripNumber.JPG" alt="Here we're searching on trip number" width="356" height="178" /><p class="wp-caption-text">Here we&#39;re searching on trip number</p></div>
<div id="attachment_235" class="wp-caption alignnone" style="width: 360px"><img class="size-full wp-image-235" title="FilteredOnPersonTitle" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnPersonTitle.JPG" alt="Searching by person title (returning all where title == CEO)" width="350" height="143" /><p class="wp-caption-text">Searching by person title (returning all where title == CEO)</p></div>
<div id="attachment_231" class="wp-caption alignnone" style="width: 356px"><img class="size-full wp-image-231" title="FilteredOnAirline" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnAirline.JPG" alt="Searching where Airline like(&quot;Airtran Airways&quot;)" width="346" height="77" /><p class="wp-caption-text">Searching where Airline like(&quot;Airtran Airways&quot;)</p></div>
<div id="attachment_236" class="wp-caption alignnone" style="width: 352px"><img class="size-full wp-image-236" title="FilteredOnState" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnState.JPG" alt="FilteredOnState" width="342" height="131" /><p class="wp-caption-text">Where State like(&quot;Nevad%&quot;)</p></div>
<div id="attachment_233" class="wp-caption alignnone" style="width: 358px"><img class="size-full wp-image-233" title="FilteredOnPersonNameFirst" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnPersonNameFirst.JPG" alt="In this one, our CEO is named Steve Jobs, and our CIO is named Steve Wozniak" width="348" height="179" /><p class="wp-caption-text">In this one, our CEO is named Steve Jobs, and our CIO is named Steve Wozniak</p></div>
<div id="attachment_232" class="wp-caption alignnone" style="width: 363px"><img class="size-full wp-image-232" title="FilteredOnDate" src="http://allenringgold.com/wp-content/uploads/2009/09/FilteredOnDate.JPG" alt="Looking for all trips in October (remember the date thing, we'll come back to that later)" width="353" height="131" /><p class="wp-caption-text">Looking for all trips in October (remember the date thing, we&#39;ll come back to that later)</p></div>
<p>&#8230;and so on.  I took more screenshots, but by now it should be obvious that we can search an entitySet on any number of fields (essentially a deep-text search), and return the corresponding Id (PK) of the entity.  If you recall my <a title="Key/Value pairs from an AutoCompleteExtender's web service" href="http://allenringgold.com/?p=156" target="_blank">previous post</a> on returning key/value pairs from an AutoCompleteItem, we can gloss over that part of it and just strip the web service down to the functionality this article addresses:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre>[System.Web.Services.<span style="color: #87ceeb;">WebMethod</span>]
[System.Web.Script.Services.<span style="color: #87ceeb;">ScriptMethod</span>]
<span style="color: #0000ff;">public string</span>[] GetTripList(<span style="color: #0000ff;">string</span> prefixText, <span style="color: #0000ff;">int</span> count)
{
<span style="color: #008000;">/**********************************************************
* Takes a prefix string from a textbox and retrieves
* trips from the entitySet. Used by the AJAX Toolkit
* AutoCompleteExtender.
***********************************************************/</span>

    <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt; TripList = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt;();

    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">IQueryable</span>&lt;<span style="color: #87ceeb;">ExecutiveTrip</span>&gt; tripsQuery =
              <span style="color: #0000ff;">from</span> t <span style="color: #0000ff;">in</span> myEnts.ExecutiveTrips
                    <span class="yellowBack"><span style="color: #0000ff;">where</span> t.TripName.Contains(prefixText)
                              || t.PersonName.Contains(prefixText)
                              || t.PersonTitle.Contains(prefixText)
                              || t.DestinationCity.Contains(prefixText)
                              || t.DestinationState.Contains(prefixText)
                              || t.Airline.Contains(prefixText)</span>
                    <span style="color: #0000ff;">orderby</span> t.TripName, t.StartDate
                    <span style="color: #0000ff;">select</span> t;

      <span style="color: #008000;">//add the values to the list and include id</span>
      <span style="color: #0000ff;">foreach</span> (<span style="color: #87ceeb;">ExecutiveTrip</span> t <span style="color: #0000ff;">in</span> tripsQuery) {
              TripList.Add(AjaxControlToolkit.<span style="color: #87ceeb;">AutoCompleteExtender</span>. _
                CreateAutoCompleteItem(t.TripName, t.Id.ToString()));
         }
   }
<span style="color: #0000ff;">return</span> TripList.ToArray();
}<span style="color: #008000;">//end GetTripList</span></pre>
</div>
<p>As you can see above, all we really need to do is chain together as many logical OR || operators and statements.  That should be pretty plain, so I&#8217;ll skip ahead to a situation that can be slightly confusing&#8230;</p>
<p>Recall above we wanted to search by date in our web service.  In this particular case, the date is in our trip name, but you might not always be so lucky.  Assuming your date field is a System.DateTime datatype, it seems logical to try something along the lines of:</p>
<p>where Convert.ToDateTime(t.StartDate).ToLongDateString().Contains(prefixText)</p>
<p>Unfortunately, that won&#8217;t work.  You&#8217;re not able to do the conversion inside of your query.  While there are probably better ways to do this, my approach was fairly simple and can be used on any datatype.  Where required, I make a SQL view that has all of the fields I&#8217;m going to want to search on, and I make all but the primary key varchar fields (either by cast or convert).  This allows us to do a string search on DateTimes, Int32s, etc.  In case you need the conversion, a SQL DateTime or SmallDateTime value can be converted to a string in your view&#8217;s SELECT statement like so:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #ff00ff;">convert</span>(<span style="color: #0000ff;">varchar</span>, <span style="color: #ff00ff;">getdate</span><span style="color: #808080;">()</span>, 100)
       output: Sep 11 2009 9:56PM

<span style="color: #ff00ff;">convert</span>(<span style="color: #0000ff;">varchar</span>, <span style="color: #ff00ff;">getdate</span><span style="color: #808080;">()</span>, 101)
       output: 9/11/2009

<span style="color: #ff00ff;">convert</span>(<span style="color: #0000ff;">varchar</span>, <span style="color: #ff00ff;">getdate</span><span style="color: #808080;">()</span>, 102)
       output: 2009.09.11

<span style="color: #ff00ff;">convert</span>(<span style="color: #0000ff;">varchar</span>, <span style="color: #ff00ff;">getdate</span><span style="color: #808080;">()</span>, 106)
       output: 11 Sep 2009

<span style="color: #ff00ff;">convert</span>(<span style="color: #0000ff;">varchar</span>, <span style="color: #ff00ff;">getdate</span><span style="color: #808080;">()</span>, 107)
       output: Sep 11, 2009
</pre>
</div>
<p>Which one you choose really just depends on how you want it to be searched (I&#8217;d go with 107, personally).</p>
<p>That&#8217;s all for now, my next post will explain the two different ways you can use the return value to databind your EntityDataSource on the trip Id.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=227</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display HTML in an AJAX autoCompleteExtender dropdown</title>
		<link>http://allenringgold.com/?p=211</link>
		<comments>http://allenringgold.com/?p=211#comments</comments>
		<pubDate>Thu, 10 Sep 2009 00:57:23 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Other Tech]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[autoCompleteExtender]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[markup]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=211</guid>
		<description><![CDATA[This post explains how to modify the AjaxControlToolkit to allow arbitrary HTML to be displayed in an autoCompleteExtender dropdown.  Can be used to show images, formatted text, etc, while retaining the key/value pair of an AutoCompleteItem.]]></description>
			<content:encoded><![CDATA[<p>(Originally coded in VS2008 using c#, .net framework 3.5, compatibility with VS2005/.net 2.0 unknown)</p>
<p>In this post, we&#8217;re going to look at modifying the AjaxControlToolkit to allow you to format the displayed items in an AutoCompleteExtender dropdown with html.</p>
<p>I first found out about this from Rob White, aka ILIVEWITHIAN on <a title="The Old Sewing Factory" href="http://theoldsewingfactory.wordpress.com/2008/12/11/arbitrary-html-in-the-autocompleteextender/" target="_blank">The Old Sewing Factory</a>.  Credit goes to him, this is just an example of how I did it in C#.</p>
<p>As discussed, you need to get the source for the toolkit <a title="AjaxControlToolkit" href="http://www.asp.net/ajax/downloads/" target="_blank">here</a>.  Make sure you get the source and not just the precompiled dll, we&#8217;re going to be extending it.</p>
<p>The first change will be adding the following method overload to your AutoCompleteExtender.cs file:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">public static string</span> CreateAutoCompleteItem _
                        (<span style="color: #0000ff;">string</span> text, <span style="color: #0000ff;">string</span> value, <span style="color: #0000ff;">string</span> markup)
{
      <span style="color: #0000ff;">return new</span> JavaScriptSerializer().Serialize _
                                    (<span style="color: #0000ff;">new</span> Pair(text, <span style="color: #0000ff;">new</span> Pair(value, markup)));
}</pre>
</div>
<p>For the sake of keeping this simple, I won&#8217;t post the whole function that goes in AutoCompleteBehavior.js, but you can read about it at Rob&#8217;s blog, or <a title="AutoCompleteBehavior.js" href="http://www.mediafire.com/?5idmqczandm" target="_blank">go here</a> to download the file (link taken from Rob White at <a title="The Old Sewing Factory" href="http://theoldsewingfactory.wordpress.com/" target="_blank">The Old Sewing Factory</a>).</p>
<p>Once you&#8217;ve got the toolkit extended, the fun begins.  In this example, our bus line company wants to add/edit passengers.  Because employees of the bus line are already in our personnel database, we don&#8217;t want to duplicate information in our passengers table.  All the same, we don&#8217;t want just anyone to be able to edit employee personnel records.  At first, it makes sense to just exclude employees in our web service, but then how would a user know if the person they&#8217;re typing in already exists?  The answer: format employees in the dropdown!  We&#8217;ll be showing all employees in red, and regular passengers/customers in black.</p>
<p>We&#8217;ll start by querying our entity set just like in the previous post on this topic:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre>[System.Web.Services.<span style="color: #87ceeb;">WebMethod</span>]
[System.Web.Script.Services.<span style="color: #87ceeb;">ScriptMethod</span>]
<span style="color: #0000ff;">public string</span>[] GetPassengerList(<span style="color: #0000ff;">string</span> prefixText, <span style="color: #0000ff;">int</span> count)
{
<span style="color: #008000;">/**********************************************************
* Takes a prefix string from a textbox and retrieves
* passengers from the entitySet. Used by the AJAX Toolkit
* AutoCompleteExtender.
***********************************************************/</span>

    <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt; Passengers = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt;();
    <span style="color: #008000;">//query the passenger entity:</span>
    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">IQueryable</span>&lt;<span style="color: #87ceeb;">Passenger</span>&gt; passengersQuery =
              <span style="color: #0000ff;">from</span> p <span style="color: #0000ff;">in</span> myEnts.Passengers
                    <span style="color: #0000ff;">where</span> p.DisplayName.Contains(prefixText)
                    <span style="color: #0000ff;">orderby</span> p.LastName
                    <span style="color: #0000ff;">select</span> p;
      <span style="color: #0000ff;">string</span> theHTML; <span style="color: #008000;">//this will be what the user sees</span>

      <span style="color: #008000;">//add the values to the list and include id</span>
      <span style="color: #0000ff;">foreach</span> (<span style="color: #87ceeb;">Passenger</span> p <span style="color: #0000ff;">in</span> passengersQuery) {
         <span class="yellowBack"><span style="color: #0000ff;">if</span>(p.IsEmployee == <span style="color: #0000ff;">false</span>)</span> { <span style="color: #008000;">//person is an external customer</span>
              Passengers.Add(AjaxControlToolkit.<span style="color: #87ceeb;">AutoCompleteExtender</span>. _
                CreateAutoCompleteItem(p.DisplayName, p.Id.ToString()));
                 }
         <span style="color: #0000ff;">else</span> { <span style="color: #008000;">//person is an employee, display in red</span>
             <span class="yellowBack">theHTML = <span style="color: #993300;">"&lt;span style='color: red;'&gt;"</span> + p.DisplayName + <span style="color: #993300;">"&lt;/span&gt;"</span>;</span>
             Passengers.Add(AjaxControlToolkit.<span style="color: #87ceeb;">AutoCompleteExtender</span>. _
               CreateAutoCompleteItem(<span class="yellowBack">p.DisplayName, p.Id.ToString(), theHTML</span>));
                }
         }
   }
<span style="color: #0000ff;">return</span> Passengers.ToArray();
}<span style="color: #008000;">//end GetPassengerList</span></pre>
</div>
<p>When it&#8217;s all done, you&#8217;ll get output like this:<br />
<img class="alignnone size-full wp-image-215" title="personDropdown" src="http://allenringgold.com/wp-content/uploads/2009/09/personDropdown.jpg" alt="personDropdown" width="295" height="198" /></p>
<p>If you really want to get fancy, you can do something like Rob did and display the employee&#8217;s photo, an icon, etc., the possibilities are endless.  I found this simple span to display employees in red was exactly what my client needed to see if the person they were typing in existed in *one* of their tables, while being able to distinguish between external and internal customers.</p>
<p>Next up, if the name they typed in wasn&#8217;t in the list, we&#8217;ll give them a one-click insert. Also, allowing user to select an employee (red) to view their info, but disable edit, is a trivial bit of code on top of this.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=211</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting a stored procedure return value from an entity insert</title>
		<link>http://allenringgold.com/?p=188</link>
		<comments>http://allenringgold.com/?p=188#comments</comments>
		<pubDate>Wed, 02 Sep 2009 21:37:55 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[.net 3.5]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[EntityDataSource]]></category>
		<category><![CDATA[SQl Server 2005]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=188</guid>
		<description><![CDATA[This post shows how to use a stored proc to insert an entity into your database and return the new identity.]]></description>
			<content:encoded><![CDATA[<p>(Originally coded in VS2008 using c#, .net framework 3.5, SQL Server 2005 back-end.  Compatibility with VS2005/.net 2.0 unknown)</p>
<p>In this article, we&#8217;re going to be using an entityDataSource tied to a detailsView to insert a new &#8220;person&#8221; entity into our database.  When the new entity is inserted, we want to use the new server-generated PK (identity) on our form, so we&#8217;ll be sending it back from the stored proc.</p>
<p>First things first, let&#8217;s look at our sample entity. This is a person in our Bus Line&#8217;s passenger database:</p>
<div id="attachment_190" class="wp-caption alignnone" style="width: 256px"><img class="size-full wp-image-190" title="BarePersonEntity" src="http://allenringgold.com/wp-content/uploads/2009/09/BarePersonEntity.JPG" alt="Simple person entity" width="246" height="356" /><p class="wp-caption-text">Simple person entity</p></div>
<p>To insert a new entity from our detailsView, we want to map an Insert stored proc, which looks like this:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">CREATE PROCEDURE</span> SP_BUSLINE_PERSON_INSERT
  @LAST_NAME <span style="color: #0000ff;">varchar</span>(25),
  @FIRST_NAME <span style="color: #0000ff;">varchar</span>(25),
  @DISPLAY_NAME <span style="color: #0000ff;">varchar</span>(65)
<span style="color: #0000ff;">AS
BEGIN
  DECLARE</span> @NEW_IDENTITY <span style="color: #0000ff;">int</span>;

  <span style="color: #0000ff;">INSERT INTO</span> BUSLINE_PERSONNEL(LAST_NAME, FIRST_NAME, DISPLAY_NAME)
  <span style="color: #0000ff;">VALUES</span> (@LAST_NAME, @FIRST_NAME, @DISPLAY_NAME)

  <span style="color: #0000ff;">SET</span> @NEW_IDENTITY = <span style="color: #ff00ff;">@@identity</span>;
  <span style="color: #0000ff;">SELECT</span> @NEW_IDENTITY "PERSON_ID"
<span style="color: #0000ff;">END</span></pre>
</div>
<p>Note that we&#8217;re not setting an &#8220;OUTPUT&#8221; parameter, but rather SELECTing a value.</p>
<p>Now that our SP is created, we need to map the insert function on our entity:</p>
<div id="attachment_189" class="wp-caption alignnone" style="width: 540px"><img class="size-full wp-image-189" title="PersonEntityInsertFunctionMapping" src="http://allenringgold.com/wp-content/uploads/2009/09/PersonEntityInsertFunctionMapping.JPG" alt="Insert Function Mapping for our Person entity" width="530" height="201" /><p class="wp-caption-text">Insert Function Mapping for our Person entity</p></div>
<p>So, now that our insert is all set up (we&#8217;re going to ignore the fact that you&#8217;re currently required to map stored procs for all functions if you map for one), let&#8217;s take a look at the entity data source that feeds our detailsView:</p>
<div class="codeDiv" style="font-size: 9pt; font-family: courier new;">
<pre><span style="color: blue;">

<span style="color: blue;">&lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: red;">ID</span><span style="color: blue;">="entPersonSource"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"</span>
<span style="color: red;"> ConnectionString</span><span style="color: blue;">="name=BusLineEntities"</span>
<span style="color: red;"> DefaultContainerName</span><span style="color: blue;">="BusLineEntities"</span>
<span style="color: red;"> EntitySetName</span><span style="color: blue;">="People" <span style="color: #ff0000;">EnableInsert</span>="True"</span>
<span style="color: blue;"><span style="color: #ff0000; background-color: yellow;">OnInserted</span>="eps_Inserted"</span>
<span style="color: red;"> Where</span><span style="color: blue;">="it.Id = @PERSON_ID)"</span><span style="color: blue;">&gt;</span>
<span style="color: blue;"> &lt;</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span>
<span style="color: blue;"> &lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">ControlParameter</span> <span style="color: red;">ControlID</span><span style="color: blue;">="txtPerson_hidden"</span>
<span style="color: red;">Type</span><span style="color: blue;">="Int32"</span> <span style="color: red;">Name</span><span style="; color: blue;">="PERSON_ID"</span> <span style="color: blue;">/&gt;</span>
<span style="color: blue;"> &lt;/</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: blue;">&gt;</span>

</span></pre>
</div>
<p>(Note you could have also set AutoGenerateWhereClause=&#8221;True&#8221; and renamed your whereParameter to &#8220;Id&#8221;)</p>
<p>For the record, we&#8217;re using a hiddenField as our ParameterSource because the user is typing into an autoComplete textbox that searches our People entitySet.  If the name is found, the user selects it and can edit the details.  If the name isn&#8217;t found, the name is put on our Details View for the user and they can choose to insert the new record.  Obviously, there won&#8217;t be a person_id for the datasource to bind to before the entity is inserted, hence our needing the return value to databind our detailsView after insert.</p>
<p>So now let&#8217;s have a look at this detailsView (simplified):</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #993300;"><span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>DetailsView <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="dvPerson"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span>
 DataSourceID<span style="color: #0000ff;">="entPersonSource"</span> <span style="color: #ff0000;">DataKeyNames</span><span style="color: #0000ff;">="Id"&gt;</span>
<span style="color: #0000ff;">&lt;</span>Fields<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TemplateField <span style="color: #ff0000;">HeaderText</span><span style="color: #0000ff;">="Display Name: "&gt;</span>
   <span style="color: #0000ff;">&lt;</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
     <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TextBox <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtDName"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Bind("DisplayName") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">' /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
   <span style="color: #0000ff;">&lt;</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
     <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>Label <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="lblDName"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Eval("DisplayName") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">' /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span>asp<span style="color: #0000ff;">:</span>TemplateField<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TemplateField <span style="color: #ff0000;">HeaderText</span><span style="color: #0000ff;">="Last Name: "&gt;</span>
   <span style="color: #0000ff;">&lt;</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
       <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TextBox <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtLName"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Bind("LastName") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">' /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
   <span style="color: #0000ff;">&lt;</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
       <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>Label <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="lblLName"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Eval("LastName") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">' /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span>asp<span style="color: #0000ff;">:</span>TemplateField<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TemplateField <span style="color: #ff0000;">HeaderText</span><span style="color: #0000ff;">=</span><span style="color: #0000ff;">"First Name: "&gt;</span>
   <span style="color: #0000ff;">&lt;</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
     <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TextBox <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtFName"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Bind("FirstName") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">' /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
   <span style="color: #0000ff;">&lt;</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
     <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>Label <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="lblFName"</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Text</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Eval("FirstName") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">' /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span>asp<span style="color: #0000ff;">:</span>TemplateField<span style="color: #0000ff;">&gt;</span>
   <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>TemplateField <span style="color: #ff0000;">ShowHeader</span><span style="color: #0000ff;">="False"&gt;</span>
   <span style="color: #0000ff;">&lt;</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
     <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>ImageButton <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="btnInsert"</span> <span class="yellowBack"><span style="color: #ff0000;">CommandName</span><span style="color: #0000ff;">="Insert"</span></span>
        <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">ImageUrl</span><span style="color: #0000ff;">="add.png" /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>InsertItemTemplate<span style="color: #0000ff;">&gt;</span>
   <span style="color: #0000ff;">&lt;</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
      <span style="color: #0000ff;">&lt;</span>asp<span style="color: #0000ff;">:</span>ImageButton <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="btnEdit"</span> <span style="color: #ff0000;">CommandName</span><span style="color: #0000ff;">="Edit"</span>
        <span style="color: #ff0000;">CommandArgument</span><span style="color: #0000ff;">='</span><span style="color: #000000;"><span class="yellowBack">&lt;%</span># Eval("Id") <span class="yellowBack">%&gt;</span></span><span style="color: #0000ff;">'</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">ImageUrl</span><span style="color: #0000ff;">="edit.png" /&gt;</span>
   <span style="color: #0000ff;">&lt;/</span>ItemTemplate<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span>asp<span style="color: #0000ff;">:</span>TemplateField<span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span>Fields<span style="color: #0000ff;">&gt;</span>
&lt;/asp<span style="color: #0000ff;">:</span>DetailsView<span style="color: #0000ff;">&gt;</span> </span></pre>
</div>
<p>Now, since we&#8217;re supposed to be binding the detailsView to the entityDataSource where person_id = the person_id in the hidden field, the details view will disappear after an insert, which is why we put the following in our code-behind:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">protected void</span> eps_Inserted(<span style="color: #0000ff;">object</span> sender, <span style="color: #87ceeb;">EntityDataSourceChangedEventArgs</span> e)
        {
            <span style="color: #87ceeb;">Person</span> p = (<span style="color: #87ceeb;">Person</span>)e.Entity;
            txtPerson_hidden.Value = p.Id.ToString();
        }</pre>
</div>
<p>You can now insert an entity using a stored proc and immediately use the return value.  And notice that we didn&#8217;t have to go to the code-behind for our insert command on the detailsView (the same is true for update and delete, you just have to set your databindings correctly in the markup).</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=188</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a web service for the AJAX Control Toolkit&#8217;s AutoCompleteExtender, using the Entity Framework (C#)</title>
		<link>http://allenringgold.com/?p=172</link>
		<comments>http://allenringgold.com/?p=172#comments</comments>
		<pubDate>Wed, 02 Sep 2009 02:10:12 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Other Tech]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asmx]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[autoCompleteExtender]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[VS2008]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=172</guid>
		<description><![CDATA[This post will show you how to write an .asmx web service to return a list of values for the AJAX Control Toolkit's AutoCompleteExtender, querying an ado.net EF entity set.]]></description>
			<content:encoded><![CDATA[<p>(Originally coded in VS2008 using c#, .net framework 3.5, compatibility with VS2005/.net 2.0 unknown)</p>
<p>In <a title="Saving autoCompleteExtender value to a hiddenField" href="http://allenringgold.com/?p=156" target="_blank">my last post</a>, I showed you how to use javascript and asp hiddenField controls to save values selected in an AJAX AutoCompleteExtender.</p>
<p>In this article, we&#8217;ll take a closer look at the actual web service being called.  The &#8220;GetPassengerList&#8221; method queries our Passengers entity set (ado.net Entity Framework) to return autocomplete options as a user types a passenger name into the textbox.</p>
<p>The code is pretty straightforward, and presented here:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre>[System.Web.Services.<span style="color: #87ceeb;">WebMethod</span>]
[System.Web.Script.Services.<span style="color: #87ceeb;">ScriptMethod</span>]
<span style="color: #0000ff;">public string</span>[] GetPassengerList(<span style="color: #0000ff;">string</span> prefixText, <span style="color: #0000ff;">int</span> count)
{
<span style="color: #008000;">/**********************************************************
* Takes a prefix string from a textbox and retrieves
* passengers from the entitySet. Used by the AJAX Toolkit
* AutoCompleteExtender.
***********************************************************/</span>

    <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt; Passengers = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt;();
    <span style="color: #008000;">//query the passenger entity:</span>
    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">IQueryable</span>&lt;<span style="color: #87ceeb;">Passenger</span>&gt; passengersQuery =
              <span style="color: #0000ff;">from</span> p <span style="color: #0000ff;">in</span> myEnts.Passengers
                    <span style="color: #0000ff;">where</span> p.DisplayName.Contains(prefixText)
                    <span style="color: #0000ff;">orderby</span> p.LastName
                    <span style="color: #0000ff;">select</span> p;
      <span style="color: #008000;">//add the values to the list and include id</span>
      <span style="color: #0000ff;">foreach</span> (<span style="color: #87ceeb;">Passenger</span> p <span style="color: #0000ff;">in</span> passengersQuery)
         Passengers.Add(AjaxControlToolkit.<span style="color: #87ceeb;">AutoCompleteExtender</span>. _
                     CreateAutoCompleteItem(p.DisplayName, p.Id.ToString()));
     }
      <span style="color: #0000ff;">return</span> Passengers.ToArray();
}<span style="color: #008000;">//end GetPassengerList</span></pre>
</div>
<p>Note that we&#8217;re creating objects of type &#8220;AutoCompleteItem,&#8221; which enables us to have the dataText and an associated dataValue, much like we were using a databound dropDownList.</p>
<p>Now, if we had information we wanted to display in our autocomplete in addition to the person&#8217;s DisplayName, we could simply include the navigation path in our query and then append the desired value to our string:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre>[System.Web.Services.<span style="color: #87ceeb;">WebMethod</span>]
[System.Web.Script.Services.<span style="color: #87ceeb;">ScriptMethod</span>]
<span style="color: #0000ff;">public string</span>[] GetPassengerList(<span style="color: #0000ff;">string</span> prefixText, <span style="color: #0000ff;">int</span> count)
{
<span style="color: #008000;">/**********************************************************
* Takes a prefix string from a textbox and retrieves
* passengers from the entitySet. Used by the AJAX Toolkit
* AutoCompleteExtender.
***********************************************************/</span>

    <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt; Passengers = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt;();
    <span style="color: #008000;">//query the passenger entity:</span>
    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">IQueryable</span>&lt;<span style="color: #87ceeb;">Passenger</span>&gt; passengersQuery =
              <span style="color: #0000ff;">from</span> p <span style="color: #0000ff;">in</span> myEnts.Passengers<span class="yellowBack">.Include(<span style="color: #993300;">"States"</span>)</span>
                    <span style="color: #0000ff;">where</span> p.DisplayName.Contains(prefixText)
                    <span style="color: #0000ff;">orderby</span> p.LastName
                    <span style="color: #0000ff;">select</span> p;
      <span style="color: #008000;">//add the values to the list and include id</span>
      <span style="color: #0000ff;">foreach</span> (<span style="color: #87ceeb;">Passenger</span> p <span style="color: #0000ff;">in</span> passengersQuery)
         Passengers.Add(AjaxControlToolkit.<span style="color: #87ceeb;">AutoCompleteExtender</span>. _
                      <span class="yellowBack">CreateAutoCompleteItem(p.DisplayName + </span>_
                               <span class="yellowBack"><span style="color: #993300;">" ("</span> + p.States.Abbrev + <span style="color: #993300;">")"</span>, p.Id.ToString()));</span>
     }
      <span style="color: #0000ff;">return</span> Passengers.ToArray();
}<span style="color: #008000;">//end GetPassengerList</span></pre>
</div>
<p>And that&#8217;s it.  The last sample assumes a NavigationPath of &#8220;States&#8221; leads to a State entity, and we want to display the user&#8217;s home state abbreviation next to their name in our autoCompleteExtender.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=172</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ajax Control Toolkit AutoCompleteExtender, saving key/value to a hidden field</title>
		<link>http://allenringgold.com/?p=156</link>
		<comments>http://allenringgold.com/?p=156#comments</comments>
		<pubDate>Wed, 02 Sep 2009 01:15:34 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Other Tech]]></category>
		<category><![CDATA[3.5]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asmx]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[autoCompleteExtender]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[key/value]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=156</guid>
		<description><![CDATA[This post will show you how to use the AJAX Control Toolkit's AutoCompleteExtender and a very simple javascript call to save a key/value to an asp.net hidden field.]]></description>
			<content:encoded><![CDATA[<p>(Originally coded in VS2008 using c#, .net framework 3.5, compatibility with VS2005/.net 2.0 unknown)</p>
<p>In this post, we&#8217;ll look at using the <a title="Ajax Control Toolkit samples" href="http://www.asp.net/ajax/AjaxControlToolkit/Samples/" target="_blank">AJAX Control Toolkit&#8217;s</a> AutoCompleteExtender, coupled with a very simple javacsript call, to save keys/values to a hidden field on your form.  The best part of this is that the javascript function can be used for any type of value (for example, person_id, course_id, etc) you&#8217;ll need to refer to during data manipulation.</p>
<p>To start, let&#8217;s look at the webservice we&#8217;ll be using for our AutoCompleteExtender.  In this example, we want a textbox that will allow the user to start typing and it will auto-complete with passengers that have ridden our imaginary bus line.  The service, PassengerLookup.asmx, will look like this:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre>[System.Web.Services.<span style="color: #87ceeb;">WebMethod</span>]
[System.Web.Script.Services.<span style="color: #87ceeb;">ScriptMethod</span>]
<span style="color: #0000ff;">public string</span>[] GetPassengerList(<span style="color: #0000ff;">string</span> prefixText, <span style="color: #0000ff;">int</span> count)
{
<span style="color: #008000;">/**********************************************************
* Takes a prefix string from a textbox and retrieves
* passengers from the entitySet. Used by the AJAX Toolkit
* AutoCompleteExtender.
***********************************************************/</span>

    <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt; Passengers = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">List</span>&lt;<span style="color: #0000ff;">string</span>&gt;();
    <span style="color: #008000;">//query the passenger entity:</span>
    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">IQueryable</span>&lt;<span style="color: #87ceeb;">Passenger</span>&gt; passengersQuery =
              <span style="color: #0000ff;">from</span> p <span style="color: #0000ff;">in</span> myEnts.Passengers
                    <span style="color: #0000ff;">where</span> p.DisplayName.Contains(prefixText)
                    <span style="color: #0000ff;">orderby</span> p.LastName
                    <span style="color: #0000ff;">select</span> p;
      <span style="color: #008000;">//add the values to the list and include id</span>
      <span style="color: #0000ff;">foreach</span> (<span style="color: #87ceeb;">Passenger</span> p <span style="color: #0000ff;">in</span> passengersQuery)
         Passengers.Add(AjaxControlToolkit.<span style="color: #87ceeb;">AutoCompleteExtender</span>. _
                     CreateAutoCompleteItem(p.DisplayName, p.Id.ToString()));
     }
      <span style="color: #0000ff;">return</span> Passengers.ToArray();
}<span style="color: #008000;">//end GetPassengerList</span></pre>
</div>
<p>Notice that we&#8217;re returning an AutoCompleteItem here, and not just an array of strings&#8230;that&#8217;s how we&#8217;ll access our value (in this case, the person.Id).  I&#8217;ll write a <a title="Writing a web service to query the EF for an autoCompleteExtender" href="http://allenringgold.com/?p=172" target="_blank">more in-depth article on that</a> soon.</p>
<p>Okay, so now that our webservice is good to go, let&#8217;s have a look at the aspx controls no our page.  For each extended textbox, we&#8217;ll want to have three controls (plus our watermark, optionally), and pay close attention to the naming conventions:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #993300;">asp</span><span style="color: #0000ff;">:</span><span style="color: #993300;">TextBox</span> <span class="yellowBack"><span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtPassenger_input"</span></span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">Width</span><span style="color: #0000ff;">="200px" /&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #993300;">cc2</span><span style="color: #0000ff;">:</span><span style="color: #993300;">TextBoxWatermarkExtender</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">=</span><span style="color: #0000ff;">"txtPassenger_TextBoxWatermarkExtender"</span>
 <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">TargetControlID</span><span style="color: #0000ff;">="txtPassenger_input"</span>
 <span style="color: #ff0000;">WatermarkCssClass</span><span style="color: #0000ff;">="watermarkTextBox"</span> <span style="color: #ff0000;">WatermarkText</span><span style="color: #0000ff;">="Last, First M."&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #993300;">cc2</span><span style="color: #0000ff;">:</span><span style="color: #993300;">TextBoxWatermarkExtender</span><span style="color: #0000ff;">&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #993300;">cc2</span><span style="color: #0000ff;">:</span><span style="color: #993300;">AutoCompleteExtender</span> <span class="yellowBack"><span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtPassenger"</span></span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span>
 <span style="color: #ff0000;">TargetControlID</span><span style="color: #0000ff;">="txtPassenger_input"</span> <span style="color: #ff0000;">ServiceMethod</span><span style="color: #0000ff;">="GetPassengerList"</span>
 <span style="color: #ff0000;">ServicePath</span><span style="color: #0000ff;">="~/Services/PassengerLookup.asmx"</span>
 <span style="color: #ff0000;">MinimumPrefixLength</span><span style="color: #0000ff;">="3"</span> <span style="color: #ff0000;">CompletionSetCount</span><span style="color: #0000ff;">="20"</span>
 <span style="color: #ff0000;">CompletionInterval</span><span style="color: #0000ff;">="100"</span> <span style="color: #ff0000;">CompletionListCssClass</span><span style="color: #0000ff;">="PassengerAutoCompleteFlyout"</span>
 <span style="color: #ff0000;">CompletionListItemCssClass</span><span style="color: #0000ff;">="PassengerAutoCompleteItem"</span>
 <span style="color: #ff0000;">CompletionListHighlightedItemCssClass</span><span style="color: #0000ff;">="PassengerAutoCompleteHighlightItem"</span>
 <span class="yellowBack"><span style="color: #ff0000;">OnClientItemSelected</span><span style="color: #0000ff;">="autoCompleteItemSelected"&gt;</span></span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #993300;">cc2</span><span style="color: #0000ff;">:</span><span style="color: #993300;">AutoCompleteExtender</span><span style="color: #0000ff;">&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #993300;">asp</span><span style="color: #0000ff;">:</span><span style="color: #993300;">HiddenField</span> <span class="yellowBack"><span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txtPassenger_hidden"</span></span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server" /&gt;</span></pre>
</div>
<p>Note that the IDs of our controls are all based on the ID of the AutoCompleteExtender.  The reason for that will become apparent when you look at the javascript call:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">function</span> autoCompleteItemSelected(source, eventArgs) {
 <span style="color: #008000;">/******************************************************************
 * this function will save the value (personID, etc) of the item
 *    selected in an autcomplete to an associated hidden field
 *******************************************************************/

 /*  Naming conventions: (required for this to work)
 the textBox being extended:     ID="txtVar_input"
 the autocompleteExtender:       ID="txtVar"
 the hidden field to save to:    ID="txtVar_hidden"

 //get the hidden field associated with this autoCompleteExtender:*/</span>
 <span style="color: #0000ff;">var</span> assocHiddenField = document.getElementById(source.get_id() + <span style="color: #993300;">'_hidden'</span>);

 <span style="color: #008000;">//set the value of the hidden field to the pair's value (personID, etc)</span>:
 assocHiddenField.value = eventArgs.get_value();

} <span style="color: #008000;">//end function autoCompleteItemSelected()</span></pre>
</div>
<p>And that&#8217;s all there is to it!  The javascript function can be put in the head of your master page and re-used anywhere you want to retrieve values.  To make it even more functional, we can include our extended textbox controls in a data control (gridview, repeater, etc), and bind with the following code:</p>
<div class="codeDiv" style="font-size: 9pt">
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #993300;">asp</span><span style="color: #0000ff;">:</span><span style="color: #993300;">HiddenField</span> <span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span> <span style="color: #ff0000;">ID</span><span style="color: #0000ff;">="txt_hidden"</span> <span style="color: #ff0000;">Value</span><span style="color: #0000ff;">='</span><span class="yellowBack">&lt;%</span># Bind("Person.Id") <span class="yellowBack">%&gt;</span><span style="color: #0000ff;">' /&gt;</span></pre>
</div>
<p>Now the gridview update will work on your entityDataSource with no additional code!</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=156</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inserting and Deleting a many-to-many relationship with the Entity Framework (C#)</title>
		<link>http://allenringgold.com/?p=132</link>
		<comments>http://allenringgold.com/?p=132#comments</comments>
		<pubDate>Tue, 01 Sep 2009 01:14:24 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Headline]]></category>
		<category><![CDATA[3.5]]></category>
		<category><![CDATA[ado]]></category>
		<category><![CDATA[ado.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[many to many]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=132</guid>
		<description><![CDATA[This post will show you how to insert and delete a many-to-many relationship (as with bridge/link tables) using the EF.]]></description>
			<content:encoded><![CDATA[<p>(Originally coded in VS2008 using c#, .net framework 3.5, compatibility with VS2005/.net 2.0 unknown)</p>
<p>In <a title="Querying many-to-may relationships with EF" href="http://allenringgold.com/?p=115" target="_blank">my last post</a>, I showed you how to use an entity datasource to query many-to-many relationships and display them on a gridview.</p>
<p>Now, we&#8217;ll go over the same scenario, but we&#8217;ll be using our gridviews to insert/delete records in our bridge table.</p>
<p>For review, we&#8217;re dealing with a database structure that looks like this:</p>
<div id="attachment_127" class="wp-caption alignnone" style="width: 423px"><img class="size-full wp-image-127" title="notamanytomany1" src="http://allenringgold.com/wp-content/uploads/2009/08/notamanytomany1.jpg" alt="This is how a many to many looks in your database, but not in your edmx" width="413" height="380" /><p class="wp-caption-text">This is how a many to many looks in your database, but not in your edmx</p></div>
<p>Conceptually, it looks like the below (the bridge table is represented as a many-to-many ( *:* ) relationship between our two entities:</p>
<div id="attachment_117" class="wp-caption alignnone" style="width: 424px"><img class="size-full wp-image-117" title="manytomany" src="http://allenringgold.com/wp-content/uploads/2009/08/manytomany.jpg" alt="This is how a many-to-many relationship looks in your data model" width="414" height="212" /><p class="wp-caption-text">This is how a many-to-many relationship looks in your data model</p></div>
<p>And once we&#8217;ve bound our gridviews (as in<a title="Querying a many-to-many using EF" href="http://allenringgold.com/?p=115" target="_blank"> the last article</a>), we should be looking at something like this:</p>
<div id="attachment_130" class="wp-caption alignnone" style="width: 381px"><img class="size-full wp-image-130" title="pantryscreen" src="http://allenringgold.com/wp-content/uploads/2009/08/pantryscreen.jpg" alt="the menu and the pantry" width="371" height="278" /><p class="wp-caption-text">the menu and the pantry</p></div>
<p>I&#8217;m going to assume a general understanding of gridview rowcommand events, but if anyone needs further explanation, feel free to ask.</p>
<p>The first action we&#8217;ll look at is the insert.  As you can see in the screenshot above, the user can click the arrow on the pantry to add a given item to the current menu.  The code will look like this for an insert:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">protected void</span> gvPantry_RowCommand(<span style="color: #0000ff;">object</span> sender, <span style="color: #87ceeb;">GridViewCommandEventArgs</span> e)
  {
  <span style="color: #008000;">/**************************************************************************
  * this function handles the row commands on the pantry (adds item to menu)
  * ************************************************************************/

  //"if" required to avoid raising this on ddlPantyFilter postback</span>
  <span style="color: #0000ff;">if</span> (e.CommandName.ToString() == <span style="color: #993300;">"AddItemToMenu"</span>)
  {
    <span style="color: #87ceeb;">Int32</span> mealID = Convert.ToInt32(((<span style="color: #87ceeb;">ImageButton</span>)gvMeals.SelectedRow. _
                             FindControl(<span style="color: #993300;">"btnSelectMeal"</span>)).CommandArgument);
    <span style="color: #87ceeb;">Int32</span> pantryItemID = <span style="color: #87ceeb;">Convert</span>.ToInt32(e.CommandArgument);
    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">Meal</span> thisMeal = myEnts.Meals.FirstOrDefault(m =&gt; m.Id == mealID);
      <span style="color: #87ceeb;">PantryItem</span> thisPI = myEnts.PantryItems. _
                            FirstOrDefault(p =&gt; p.Id == pantryItemID);

      thisMeal.PantryItems.Add(thisPI);
      myEnts.SaveChanges();
    }
    gvFoodOnMenu.DataBind();
    gvPantry.DataBind();
  }
  } <span style="color: #008000;">//end void gvPantry_RowCommand()</span></pre>
</div>
<p>Fairly straightforward, but now let&#8217;s say you need to do a delete.  It&#8217;s not much different, really, and the code will look something like this:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre><span style="color: #0000ff;">protected void</span> gvFoodOnMenu_RowCommand(<span style="color: #0000ff;">object</span> sender, <span style="color: #87ceeb;">GridViewCommandEventArgs</span> e)
  {
  <span style="color: #008000;">/****************************************************************************
  * this function handles the row commands on the menu (removes item from menu)
  * **************************************************************************/
</span>
    <span style="color: #87ceeb;">Int32</span> mealID = Convert.ToInt32(((<span style="color: #87ceeb;">ImageButton</span>)gvMeals.SelectedRow. _
                             FindControl(<span style="color: #993300;">"btnSelectMeal"</span>)).CommandArgument);
    <span style="color: #87ceeb;">Int32</span> pantryItemID = <span style="color: #87ceeb;">Convert</span>.ToInt32(e.CommandArgument);
    <span style="color: #0000ff;">using</span> (<span style="color: #87ceeb;">BusLineEntities</span> myEnts = <span style="color: #0000ff;">new</span> <span style="color: #87ceeb;">BusLineEntities</span>())
    {
      <span style="color: #87ceeb;">Meal</span> thisMeal = myEnts.Meals.FirstOrDefault(m =&gt; m.Id == mealID);
      <span style="color: #87ceeb;">PantryItem</span> thisPI = myEnts.PantryItems. _
                            FirstOrDefault(p =&gt; p.Id == pantryItemID);

      <span style="background-color: yellow;">thisMeal.PantryItems.Attach(thisPI);</span>
      <span style="background-color: yellow;">thisMeal.PantryItems.Remove(thisPI);</span>
      myEnts.SaveChanges();
    }
    gvFoodOnMenu.DataBind();
    gvPantry.DataBind();
  } <span style="color: #008000;">//end void gvPantry_RowCommand()</span></pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=132</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Querying a many-to-many relationship with EntityDataSource</title>
		<link>http://allenringgold.com/?p=115</link>
		<comments>http://allenringgold.com/?p=115#comments</comments>
		<pubDate>Mon, 31 Aug 2009 00:41:50 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[ado]]></category>
		<category><![CDATA[ado.net]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[EntityDataSource]]></category>
		<category><![CDATA[many to many]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=115</guid>
		<description><![CDATA[Shows you how to use an EntityDataSource to query many-to-many relationships in your entity data model for display on a gridview.]]></description>
			<content:encoded><![CDATA[<p>For all of the great features of the entity framework 3.5 used with Visual Studio 2008, it has some seriously annoying setbacks.  Well, maybe not so annoying once you figure them out, but it sure can slow down development when you&#8217;re racking your brains (and scouring the <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads" target="_blank">EF forums</a>!) trying to figure out something that was so simple to do &#8220;the old-fashioned way.&#8221;</p>
<p>With that, in this post, I&#8217;ll show you how to use an EntityDataSource to query a many-to-many relationship (sometimes called a swing table or bridge table), for display on a gridview.</p>
<p>First thing&#8217;s first, you probably already noticed when importing your database to the entity model that your swing table (consisting of two foreign keys) didn&#8217;t show up.  Well, it did, but not like you thought it would.  Just in case you haven&#8217;t gotten that far, we&#8217;ll do a quick review&#8230;</p>
<p>Using an example of a system you&#8217;re creating for a bus line, let&#8217;s say you have two tables&#8230;a T_PANTRY and T_MEALS.  T_PANTRY has all of you pantry items (food items that can be served to passengers), and T_MEALS has all of the meals served on your bus line.  You have a third table, T_MENUS, that consists of the MEAL_ID (pk from T_MEALS), and PANTRY_ITEM_ID (pk from T_PANTRY).  You might expect to see something like the following in your entity data model (edmx):</p>
<div id="attachment_116" class="wp-caption alignnone" style="width: 424px"><img class="size-full wp-image-116 " title="notamanytomany" src="http://allenringgold.com/wp-content/uploads/2009/08/notamanytomany1.jpg" alt="This is a many-to-many in your database, but not in your edmx" width="414" height="378" /><p class="wp-caption-text">This is a many-to-many in your database, but not in your edmx</p></div>
<p>What you&#8217;ll actually see in your edmx can be a bit confusing at first, and looks like this:</p>
<div id="attachment_117" class="wp-caption alignnone" style="width: 424px"><img class="size-full wp-image-117" title="manytomany" src="http://allenringgold.com/wp-content/uploads/2009/08/manytomany.jpg" alt="This is how a many-to-many relationship looks in your data model" width="414" height="212" /><p class="wp-caption-text">This is how a many-to-many relationship looks in your data model</p></div>
<p>So, you&#8217;ve got a gridview that shows you all of the meals on a given trip, and you want to give the user the ability to select a meal.  When they select a meal, they can see two smaller gridviews, one with all pantry items currently on the meal (T_MENUS), and one with all pantry items NOT currently on the meal.  Your entity datasource would look something like this for  the menu:</p>
<div class="codeDiv" style="font-size: 9pt; font-family: courier new;"><span style="color: blue;"></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;">&lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: red;">ID</span><span style="color: blue;">=&#8221;MenuDataSource&#8221;</span> <span style="color: red;">runat</span><span style="color: blue;">=&#8221;server&#8221;</span><br />
<span style="color: red;"><span> </span>ConnectionString</span><span style="color: blue;">=&#8221;name=BusLineEntities&#8221;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: red;"> DefaultContainerName</span><span style="color: blue;">=&#8221;BusLineEntities&#8221;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: red;"> EntitySetName</span> <span style="color: blue;">=&#8221;PantryItems&#8221;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="background: yellow none repeat scroll 0% 0%; color: red;"> Where</span><span style="background: yellow none repeat scroll 0% 0%; color: blue;">=&#8221;EXISTS(SELECT VALUE m FROM it.Meals AS m WHERE m.ID = @MealId)&#8221;</span><span style="color: blue;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"> &lt;</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"> &lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">ControlParameter</span> <span style="color: red;">ControlID</span><span style="color: blue;">=&#8221;MealsOnTripGridView&#8221;</span><br />
<span style="color: red;">Type</span><span style="color: blue;">=&#8221;Int32&#8243;</span> <span style="background: yellow none repeat scroll 0% 0%; color: red;">Name</span><span style="background: yellow none repeat scroll 0% 0%; color: blue;">=&#8221;MealId&#8221;</span> <span style="color: blue;">/&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"> &lt;/</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: blue;">&gt;</span></p>
<p></span></div>
<p>(note that you need to have dataKeyNames set up properly on your &#8220;MealsOnTrip&#8221; gridview for the where parameter to work when a meal is selected.)</p>
<p>Now, for your pantry gridview, you only want to show pantry items that aren&#8217;t currently on the menu.  To make things even smoother, we&#8217;ve added a dropdownlist to filter the pantry by mealtype.  The entitydatasource looks like so:</p>
<div class="codeDiv" style="font-size: 9pt;">
<pre>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;">&lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: red;">ID</span><span style="color: blue;">="PantryDataSource"</span> <span style="color: red;">runat</span><span style="color: blue;">="server"</span>
<span style="color: red;"><span> </span>ConnectionString</span><span style="color: blue;">="name=BusLineEntities"</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: red;"> DefaultContainerName</span><span style="color: blue;">="BusLineEntities"</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: red;"> EntitySetName</span> <span style="color: blue;">="PantryItems"</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="background: yellow none repeat scroll 0% 0%; color: red;"> Where</span><span style="background: yellow none repeat scroll 0% 0%; color: blue;">="NOT EXISTS(SELECT VALUE m FROM it.Meals AS m WHERE m.ID = @MealId)
        AND it.MealType LIKE(@MealType)"</span><span style="color: blue;">&gt;</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"> &lt;</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"> &lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">ControlParameter</span> <span style="color: red;">ControlID</span><span style="color: blue;">="MealsOnTripGridView"</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: red;">Type</span><span style="color: blue;">="Int32"</span> <span style="background: yellow none repeat scroll 0% 0%; color: red;">Name</span><span style="background: yellow none repeat scroll 0% 0%; color: blue;">="MealId"</span> <span style="color: blue;">/&gt;</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">ControlParameter</span> <span style="color: red;">ControlID</span><span style="color: blue;">="ddlPantryFilter"</span>
<span style="color: red;">Type</span><span style="color: blue;">="String"</span> <span style="background: yellow none repeat scroll 0% 0%; color: red;">Name</span><span style="background: yellow none repeat scroll 0% 0%; color: blue;">="MealType" Property="SelectedValue"</span> <span style="color: blue;">/&gt;</span></span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;"> &lt;/</span><span style="color: #a31515;">WhereParameters</span><span style="color: blue;">&gt;</span>
<p class="MsoNormal" style="margin: 0in 0in 0pt;" align="left"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">asp</span><span style="color: blue;">:</span><span style="color: #a31515;">EntityDataSource</span><span style="color: blue;">&gt;</span>
</pre>
</div>
<p>So you have an idea what we&#8217;re talking about, here&#8230;when a meal is selected, the menu and pantry are visible, something like this:</p>
<div id="attachment_130" class="wp-caption alignnone" style="width: 381px"><img class="size-full wp-image-130" title="pantryscreen" src="http://allenringgold.com/wp-content/uploads/2009/08/pantryscreen.jpg" alt="the menu and the pantry" width="371" height="278" /><p class="wp-caption-text">the menu and the pantry</p></div>
<p>Okay, so now you know how to query a many-to-many relationship using an entityDataSource for display on a gridview.  The <a title="Insert and Delete many-to-many relationships in EF" href="http://allenringgold.com/?p=132" target="_blank">next article will show you how to insert/delete</a>, using the same example we&#8217;re working with here.</p>
<p>If you have any questions or suggestions, feel free to leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=115</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Copying DLLs from the GAC (directory access in Windows)</title>
		<link>http://allenringgold.com/?p=81</link>
		<comments>http://allenringgold.com/?p=81#comments</comments>
		<pubDate>Mon, 27 Apr 2009 14:22:46 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Other Tech]]></category>
		<category><![CDATA[cmd]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[copy dll]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[GAC]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[shfusion]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=81</guid>
		<description><![CDATA[This post shows you how to access the GAC like any normal directory so you can simply right-click and copy DLLs.]]></description>
			<content:encoded><![CDATA[<p>If you need to reference a DLL that’s in the GAC (Global Assembly Cache), but you don’t have the actual DLL file, you can easily copy it from the GAC and then install in elsewhere (if, for instance, there is a DLL missing on one server but it’s in the GAC on another).</p>
<div id="attachment_87" class="wp-caption alignnone" style="width: 556px"><img class="size-full wp-image-87" title="gac01" src="http://allenringgold.com/wp-content/uploads/2009/04/gac01.jpg" alt="Normal GAC View" width="546" height="532" /><p class="wp-caption-text">Normal GAC View</p></div>
<p>On the machine where the desired assembly is located, you can change the windows assembly to directory view by running the following command from command line:</p>
<div class="cmdPrompt" style="font-size: 9pt">regsvr32 /u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll</div>
<p>&nbsp; </p>
<div id="attachment_88" class="wp-caption alignnone" style="width: 589px"><img class="size-full wp-image-88" title="gac02" src="http://allenringgold.com/wp-content/uploads/2009/04/gac02.jpg" alt="Unregister SHFusion.dll" width="579" height="285" /><p class="wp-caption-text">Unregister SHFusion.dll</p></div>
<div id="attachment_89" class="wp-caption alignnone" style="width: 556px"><img class="size-full wp-image-89" title="gac03" src="http://allenringgold.com/wp-content/uploads/2009/04/gac03.jpg" alt="GAC in Directory View" width="546" height="532" /><p class="wp-caption-text">GAC in Directory View</p></div>
<p>You can now view the assembly folder like any other directory, just copy the desired DLL to another location and then re-set the view by running the following command:</p>
<div class="cmdPrompt" style="font-size: 9pt">regsvr32 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll</div>
<p>&nbsp; </p>
<div id="attachment_90" class="wp-caption alignnone" style="width: 589px"><img class="size-full wp-image-90" title="gac04" src="http://allenringgold.com/wp-content/uploads/2009/04/gac04.jpg" alt="Re-registering SHFusion.dll" width="579" height="285" /><p class="wp-caption-text">Re-registering SHFusion.dll</p></div>
<p>Now, to install a DLL in the GAC on another machine, locate the gacutil.exe (should be in the location below, if not, change the command as needed) and run the following commands:</p>
<div class="cmdPrompt" style="font-size: 9pt">cd c:\windows\microsoft.net\framework\v1.1.4322</p>
<p>gacutil.exe /if &lt;location&gt;\&lt;filename.dll&gt;</p></div>
<p>for example, <span class="cmdPrompt" style="font-size: 9pt">gacutil.exe /if c:\dllCopyDir\testDLL.dll</span></p>
<p>the “/if” switch overwrites an existing DLL (if you wish, you can also use “/i” to only install if not existing)</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=81</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Site Under Construction</title>
		<link>http://allenringgold.com/?p=11</link>
		<comments>http://allenringgold.com/?p=11#comments</comments>
		<pubDate>Sun, 12 Apr 2009 01:32:03 +0000</pubDate>
		<dc:creator>Allen Ringgold</dc:creator>
				<category><![CDATA[Headline]]></category>

		<guid isPermaLink="false">http://allenringgold.com/?p=11</guid>
		<description><![CDATA[Sorry about the random AdSense ads.]]></description>
			<content:encoded><![CDATA[<p>Allen Ringgold&#8217;s new tech blog will focus on .net tips &#038; tricks, as well as whatever else I run into that warrants repeating.  Bear with me as I work on getting up enough content to make the site useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenringgold.com/?feed=rss2&amp;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
