<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Pankaj Bodani's Blog</title>
	<atom:link href="http://olserv.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://olserv.wordpress.com</link>
	<description>Life Perception Management .NET</description>
	<lastBuildDate>Thu, 19 Feb 2009 10:33:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='olserv.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Pankaj Bodani's Blog</title>
		<link>http://olserv.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://olserv.wordpress.com/osd.xml" title="Pankaj Bodani&#039;s Blog" />
	<atom:link rel='hub' href='http://olserv.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SelectedValue out of range DropDownList linq !!!</title>
		<link>http://olserv.wordpress.com/2009/02/18/selectedvalue-out-of-range-dropdownlist-linq/</link>
		<comments>http://olserv.wordpress.com/2009/02/18/selectedvalue-out-of-range-dropdownlist-linq/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 15:41:51 +0000</pubDate>
		<dc:creator>olserv</dc:creator>
				<category><![CDATA[.NET stuff]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[argument]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[because]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[datasource]]></category>
		<category><![CDATA[does]]></category>
		<category><![CDATA[dropdownlist]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[exist]]></category>
		<category><![CDATA[has]]></category>
		<category><![CDATA[in]]></category>
		<category><![CDATA[invalid]]></category>
		<category><![CDATA[is]]></category>
		<category><![CDATA[it]]></category>
		<category><![CDATA[items]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linqdatasource]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[nchar]]></category>
		<category><![CDATA[not]]></category>
		<category><![CDATA[of]]></category>
		<category><![CDATA[out of]]></category>
		<category><![CDATA[Parameter]]></category>
		<category><![CDATA[range]]></category>
		<category><![CDATA[selected]]></category>
		<category><![CDATA[SelectedValue]]></category>
		<category><![CDATA[space]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[the]]></category>
		<category><![CDATA[trailing]]></category>
		<category><![CDATA[value]]></category>
		<category><![CDATA[which]]></category>
		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://olserv.wordpress.com/?p=3</guid>
		<description><![CDATA[When assigning to a  SelectedValue property of a dropdownlist the following exception is thrown &#8216;DropdownList&#8217; has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value The problem here is that when the data binding is performed on the dropdownlist the selectedValue for items has extraneous trailing whitespaces. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=olserv.wordpress.com&amp;blog=6634675&amp;post=3&amp;subd=olserv&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When assigning to a  SelectedValue property of a dropdownlist the following exception is thrown</p>
<p>&#8216;DropdownList&#8217; has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value</p>
<p>The problem here is that when the data binding is performed on the dropdownlist the selectedValue for items has extraneous trailing whitespaces.</p>
<p>Many programmers make the mistake of assuming that the trailing whitespace is due to the width of the sql nchar column (Sorry guys reducing the width won&#8217;t do much good)</p>
<p>First letme give you the solution, later at the end of this post ill give you a probable reason that i have discovered so as to to why this anomally occurs.</p>
<p> </p>
<p>Solution.</p>
<p>Trim the selectedValues on the databound event of the dropdownlist as shown in the following code</p>
<p> </p>
<div><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">protected </span></span></span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">void</span></span><span style="font-size:x-small;"> DropDownList1_DataBound(</span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">object</span></span><span style="font-size:x-small;"> sender, </span><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">EventArgs </span></span><span style="font-size:x-small;">e)</span></span></span></span></span><span style="font-size:x-small;color:#0000ff;"> </span></span> </div>
<div><span style="font-size:x-small;"><span style="font-size:x-small;"> </span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">       foreach</span></span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">ListItem</span></span><span style="font-size:x-small;"> li </span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">in</span></span><span style="font-size:x-small;"> DropDownList1.Items) </span></div>
<p><span style="font-size:x-small;">        {</span></p>
<div><span style="font-size:x-small;">                {</span></div>
<div><span style="font-size:x-small;">                li.Value = li.Value.Trim();</span></div>
<div><span style="font-size:x-small;">                }</span></div>
<div><span style="font-size:x-small;">        }</span></div>
<div><span style="font-size:x-small;">}</span></div>
<p><span style="font-size:x-small;">For absolute beginners: Make sure the above funtion is added as an event handler for the databound event of the dropdownlist</p>
<p>and voila!!! problem solved , Party!!!!!  (Atleast in my case he he..)</p>
<p>Hope this helps,</p>
<p>drop me a mail at <a href="mailto:bodani.pankaj@live.com">bodani.pankaj@live.com</a> and send your code if this doesnt work for you.</p>
<p>The actual problem is here: I found this on MSDN</p>
<p>&#8220;In order to translate data between the object model and the database, a <span class="parameter"><em>type mapping</em></span> must be defined. LINQ to SQL uses a type mapping to match each common language runtime (CLR) type with a particular SQL Server type. You can define type mappings and other mapping information, such as database structure and table relationships, inside the object model with attribute-based mapping. Alternatively, you can specify the mapping information outside the object model with an external mapping file&#8221;</p>
<p>this may even happen if you are using other type of data sources such a SQLDatasource</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p></span></p>
<p><span style="font-size:x-small;">{<span style="font-size:x-small;"> </span></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/olserv.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/olserv.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/olserv.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/olserv.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/olserv.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/olserv.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/olserv.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/olserv.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=olserv.wordpress.com&amp;blog=6634675&amp;post=3&amp;subd=olserv&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://olserv.wordpress.com/2009/02/18/selectedvalue-out-of-range-dropdownlist-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c99b5bbb8b28e725d31fd06f054ae259?s=96&#38;d=identicon" medium="image">
			<media:title type="html">olserv</media:title>
		</media:content>
	</item>
	</channel>
</rss>
