<?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>dotnet thoughts &#187; ASP.Net</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:18:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Webcams in Silverlight 4.0</title>
		<link>http://www.dotnetthoughts.net/2012/01/03/webcams-in-silverlight-4/</link>
		<comments>http://www.dotnetthoughts.net/2012/01/03/webcams-in-silverlight-4/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 17:58:32 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Web cam]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2209</guid>
		<description><![CDATA[Microsoft Silverlight 4.0 supports Webcam and microphone access from Web Applications like Flash. This feature enables a number of scenarios, such as capturing and displaying images, uploading profile pictures to social networking applications etc. Here is code part, which will &#8230; <a href="http://www.dotnetthoughts.net/2012/01/03/webcams-in-silverlight-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Microsoft Silverlight 4.0 supports Webcam and microphone access from Web Applications like Flash. This feature enables a number of scenarios, such as capturing and displaying images, uploading profile pictures to social networking applications etc. Here is code part, which will start the webcam, stop the webcam and captures the image.</p>
<p>Initialize the web cam</p>
<pre class="brush: csharp; title: ; notranslate">
private CaptureSource _captureSource;
private VideoBrush _videoBrush;
private ImageBrush _imageBrush;

_captureSource = new CaptureSource();
_captureSource.CaptureImageCompleted += (o, e) =&gt;
{
    _imageBrush = new ImageBrush();
    _imageBrush.ImageSource = e.Result;
    rectWebCamView.Fill = _imageBrush;
};
_captureSource.CaptureFailed += (o, e) =&gt;
{
    MessageBox.Show(e.ErrorException.Message);
};
</pre>
<p>Start the web cam, the rectWebCamView is a Rectangle.</p>
<pre class="brush: csharp; title: ; notranslate">
if (_captureSource.State != CaptureState.Started)
{
    _captureSource.VideoCaptureDevice =
        CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
    _videoBrush = new VideoBrush();
    _videoBrush.SetSource(_captureSource);
    rectWebCamView.Fill = _videoBrush;

    if (CaptureDeviceConfiguration.RequestDeviceAccess())
    {
        _captureSource.Start();
    }
}
</pre>
<p>The RequestDeviceAccess() method invokes a request dialog for accessing the webcame like this.</p>
<div id="attachment_2211" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-2211" title="Request Access dialog " src="http://www.dotnetthoughts.net/wp-content/uploads/2012/01/request_access_opt-300x115.jpg" alt="Request Access dialog " width="300" height="115" /><p class="wp-caption-text">Request Access dialog</p></div>
<p>Note: The RequestAccess must be called in response to a user initiated event, such as a Button Click event.</p>
<p>Stop the web cam</p>
<pre class="brush: csharp; title: ; notranslate">
_captureSource.Stop();
</pre>
<p>And finally the capture image.</p>
<pre class="brush: csharp; title: ; notranslate">
if (_captureSource.State == CaptureState.Started)
{
    _captureSource.CaptureImageAsync();
}
</pre>
<p>The method will invoke CaptureImageCompleted event, and which will capture the image and display it in the rectangle. The XAML code contains only 3 buttons and one rectangle, so I am not posting the XAML code.</p>
<p>Happy programming <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/09/20/drag-and-drop-in-silverlight-3/" title="Permanent link to Drag and Drop in Silverlight 3">Drag and Drop in Silverlight 3</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/11/10/post-data-using-httpwebrequest-in-c-sharp/" title="Permanent link to Post data using HttpWebRequest in C#">Post data using HttpWebRequest in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/01/06/file-uploader-using-silverlight-and-wcf/" title="Permanent link to File Uploader using Silverlight and WCF">File Uploader using Silverlight and WCF</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/04/how-to-access-sharepoint-list-items-using-silverlight-object-model/" title="Permanent link to How to access SharePoint List Items using Silverlight Object Model">How to access SharePoint List Items using Silverlight Object Model</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/05/hosting-silverlight-application-in-sharepoint-2010/" title="Permanent link to Hosting Silverlight application in Sharepoint 2010">Hosting Silverlight application in Sharepoint 2010</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2012/01/03/webcams-in-silverlight-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>There is a duplicate &#8216;system.web.extensions / scripting / scriptResourceHandler&#8217; section defined</title>
		<link>http://www.dotnetthoughts.net/2011/11/20/there-is-a-duplicate-system-web-extensions-scripting-scriptresourcehandler-section-defined/</link>
		<comments>http://www.dotnetthoughts.net/2011/11/20/there-is-a-duplicate-system-web-extensions-scripting-scriptresourcehandler-section-defined/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 14:44:02 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[IIS 7]]></category>
		<category><![CDATA[Web.Config]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2052</guid>
		<description><![CDATA[After installing Sharepoint Foundation(again its not worked ) on my Windows 7 system I got this while looking into the Authentication feature of IIS. But after verifying the web.config file, I couldn&#8217;t find the duplicate entry. After searching I found &#8230; <a href="http://www.dotnetthoughts.net/2011/11/20/there-is-a-duplicate-system-web-extensions-scripting-scriptresourcehandler-section-defined/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After installing Sharepoint Foundation(again its not worked <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  ) on my Windows 7 system I got this while looking into the Authentication feature of IIS.</p>
<div id="attachment_2055" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/2011/11/20/there-is-a-duplicate-system-web-extensions-scripting-scriptresourcehandler-section-defined/authentication_exception/" rel="attachment wp-att-2055"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/authentication_exception-300x194.jpg" alt="Exception - There is a duplicate &#039;system.web.extensions/scripting/scriptResourceHandler&#039; section defined" title="Exception - There is a duplicate &#039;system.web.extensions/scripting/scriptResourceHandler&#039; section defined" width="300" height="194" class="size-medium wp-image-2055" /></a><p class="wp-caption-text">There is a duplicate &#039;system.web.extensions/ scripting/ scriptResourceHandler&#039; section defined.</p></div>
<p>But after verifying the web.config file, I couldn&#8217;t find the duplicate entry. After searching I found that this issue is with ASP.NET 4.0 / VS 2010. The workaround for the scenario is to delete or comment out all the system.web.extensions configuration section definitions and configuration section group definitions from the application-level <em>web.config</em> file. Because all these settings are available in the top application level <em>web.config</em> file, will be under the configSections element.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/10/07/webmatrix-beta-2/" title="Permanent link to WebMatrix Beta 2">WebMatrix Beta 2</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/04/16/system-invalidoperationexception-the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-property/" title="Permanent link to System.InvalidOperationException &#8211; The length of the string exceeds the value set on the maxJsonLength property.">System.InvalidOperationException &#8211; The length of the string exceeds the value set on the maxJsonLength property.</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/09/22/unrecognized-attribute-targetframework-error/" title="Permanent link to Unrecognized attribute &#8216;targetFramework&#8217; error">Unrecognized attribute &#8216;targetFramework&#8217; error</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/10/09/creating-a-webpart-using-visual-web-developer-express/" title="Permanent link to Creating a WebPart using Visual Web Developer Express">Creating a WebPart using Visual Web Developer Express</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/02/04/encrypting-and-decrypting-configuration-sections/" title="Permanent link to Encrypting and Decrypting Configuration Sections">Encrypting and Decrypting Configuration Sections</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/11/20/there-is-a-duplicate-system-web-extensions-scripting-scriptresourcehandler-section-defined/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to create a RSS feed using ASP.Net</title>
		<link>http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/</link>
		<comments>http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 19:36:18 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[ATOM]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[RSS Feed]]></category>
		<category><![CDATA[Syndication]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2014</guid>
		<description><![CDATA[Another ASP.Net related post, this post helps to create a custom RSS feed using ASP.Net. There are many ways to create feed using ASP.Net, like using Xml Documents, String Builder and simple string concatenation. But none of them designed to &#8230; <a href="http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Another ASP.Net related post, this post helps to create a custom RSS feed using ASP.Net. There are many ways to create feed using ASP.Net, like using Xml Documents, String Builder and simple string concatenation. But none of them designed to create RSS feed, so it will be painful and complex. But Microsoft introduced Syndication namespace in .Net Framework 3.5, which helps to create RSS or ATOM feed easily.</p>
<p>Here is my sample Database design and which contains few of my blog posts.</p>
<div id="attachment_2015" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture.jpg"><img class="size-medium wp-image-2015" title="Database Structure " src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture-300x109.jpg" alt="Database Structure " width="300" height="109" /></a><p class="wp-caption-text">Database Structure</p></div>
<p style="text-align: justify;">And here is the implementation using generic ashx handler, which helps to create RSS feeds. Following snippet will read all the values from the table and creating list of SyndicationItem using SqlDataReader.</p>
<pre class="brush: csharp; title: ; notranslate">
using (SqlCommand command = new SqlCommand(&quot;SELECT * FROM tblPosts&quot;, connection))
{
    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
    while (reader.Read())
    {
        feedList.Add(new SyndicationItem(
            reader[&quot;PostTitle&quot;].ToString(),
            reader[&quot;PostContent&quot;].ToString(),
            new Uri(string.Format(APP_URL,
                reader[&quot;PostId&quot;].ToString())),
            reader[&quot;PostId&quot;].ToString(),
            DateTimeOffset.Parse(reader[&quot;PostDate&quot;].ToString())));
    }
}
</pre>
<p style="text-align: justify;">And next snippet creating a SyndicationFeed which will help to create RSS using SyndicationItems. Based on the querystring, it is generating either ATOM or RSS feed.</p>
<pre class="brush: csharp; title: ; notranslate">
SyndicationFeed feeds =
    new SyndicationFeed(&quot;DOTNETTHOUGHTS&quot;,
        &quot;A DOTNET DEVELOPER'S TECHNICAL BLOG&quot;,
        new Uri(&quot;http://dotnetthoughts.net/feed&quot;), feedList);
feeds.Copyright =
    new TextSyndicationContent(&quot;Copyright (C) 2011 dotnetthoughts.net&quot;);
feeds.Authors.Add(
    new SyndicationPerson(&quot;anuraj@dotnetthoughts.net&quot;, &quot;Anuraj&quot;, &quot;dotnetthoughts.net&quot;));
feeds.Language = &quot;en-us&quot;;
string responseString;
using (var ms = new MemoryStream())
{
    using (XmlWriter xmlWriter = XmlWriter.Create(ms))
    {
        string rss = HttpContext.Current.Request.QueryString[&quot;rss&quot;];
        if (string.IsNullOrWhiteSpace(rss))
        {
            feeds.SaveAsAtom10(xmlWriter);
        }
        else
        {
            feeds.SaveAsRss20(xmlWriter);
        }
        xmlWriter.Flush();
        xmlWriter.Close();
    }
    responseString = System.Text.ASCIIEncoding.UTF8.GetString(ms.ToArray());
}

context.Response.ContentType = &quot;text/xml&quot;;
context.Response.Write(responseString);
context.Response.End();
</pre>
<p>And here is the screenshot of RSS feed in Internet Explorer.</p>
<div id="attachment_2016" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture1.jpg"><img class="size-medium wp-image-2016" title="RSS Feed in Internet Explorer" src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture1-300x175.jpg" alt="RSS Feed in Internet Explorer" width="300" height="175" /></a><p class="wp-caption-text">RSS Feed in Internet Explorer</p></div>
<p style="text-align: justify;">There are more useful tags which you can use to create the RSS feed, such as the category or an unique ID. You found more information on Creating RSS Feed in the <a href="http://feed1.w3.org/docs/rss2.html" target="_blank">RSS 2.0 specification page</a>.</p>
<p style="text-align: justify;">Happy Programming.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/12/17/developing-a-simple-rss-reader-in-c-net/" title="Permanent link to Developing a simple RSS Reader in C#.Net">Developing a simple RSS Reader in C#.Net</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/03/20/how-to-create-restful-wcf-services/" title="Permanent link to How to create RESTful WCF services">How to create RESTful WCF services</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/" title="Permanent link to How to configure a WCF service to use Port Sharing">How to configure a WCF service to use Port Sharing</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/25/raven-db-introduction/" title="Permanent link to Raven DB &#8211; Introduction">Raven DB &#8211; Introduction</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to persist checkbox state in gridview while paging</title>
		<link>http://www.dotnetthoughts.net/2011/11/15/how-to-persist-checkbox-state-in-gridview-while-paging/</link>
		<comments>http://www.dotnetthoughts.net/2011/11/15/how-to-persist-checkbox-state-in-gridview-while-paging/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 17:56:10 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[Paging]]></category>
		<category><![CDATA[State Management]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=1980</guid>
		<description><![CDATA[Again an asp.net post. Today I saw the same question few times in forum, so I thought about creating a post around it. And it helped me to brush up my asp.net skills once again. In this I am using &#8230; <a href="http://www.dotnetthoughts.net/2011/11/15/how-to-persist-checkbox-state-in-gridview-while-paging/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Again an asp.net post. Today I saw the same question few times in forum, so I thought about creating a post around it. And it helped me to brush up my asp.net skills once again. In this I am using ViewState to store the information about paging state, you can also use session instead of View State. As view state is enabled default and its required for Paging also, I thought better to use ViewState than session.</p>
<p style="text-align: justify;">I created a web page with a Gridview with Paging enabled. First column of the grid view is Template Column which contains the Checkbox for selection of rows. And the state information is saved while paging, and retrived on Prerender event of the GridView. And here is the implementation.</p>
<p>ASPX Markup</p>
<pre class="brush: xml; title: ; notranslate">
&lt;asp:GridView runat=&quot;server&quot; ID=&quot;gvSample&quot; AutoGenerateColumns=&quot;false&quot; AllowPaging=&quot;True&quot;
            OnPageIndexChanging=&quot;gvSample_PageIndexChanging&quot; OnPreRender=&quot;gvSample_PreRender&quot;&gt;
            &lt;Columns&gt;
                &lt;asp:TemplateField&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:CheckBox runat=&quot;server&quot; ID=&quot;chkSelected&quot; /&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:BoundField DataField=&quot;Name&quot; HeaderText=&quot;Name&quot; /&gt;
                &lt;asp:BoundField DataField=&quot;Email&quot; HeaderText=&quot;Email&quot; /&gt;
            &lt;/Columns&gt;
            &lt;PagerSettings PageButtonCount=&quot;10&quot; /&gt;
        &lt;/asp:GridView&gt;</pre>
<p>And here is the code-behind for it</p>
<pre class="brush: csharp; title: ; notranslate">
protected void gvSample_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    string pageId = string.Format(&quot;Page{0}&quot;, gvSample.PageIndex);
    bool[] selectedCheckboxes = new bool[gvSample.PageSize];
    for (int i = 0; i &lt; gvSample.Rows.Count; i++)
    {
        TableCell cell = gvSample.Rows[i].Cells[0];
        selectedCheckboxes[i] = (cell.FindControl(&quot;chkSelected&quot;) as CheckBox).Checked;
    }

    ViewState[pageId] = selectedCheckboxes;
    gvSample.PageIndex = e.NewPageIndex;
    //Bind the gridview again
}

protected void gvSample_PreRender(object sender, EventArgs e)
{
    string pageId = string.Format(&quot;Page{0}&quot;, gvSample.PageIndex);
    bool[] selectedCheckboxes = ViewState[pageId] as bool[];
    if (selectedCheckboxes != null)
    {
        for (int i = 0; i &lt; gvSample.Rows.Count; i++)
        {
            TableCell cell = gvSample.Rows[i].Cells[0];
            (cell.FindControl(&quot;chkSelected&quot;) as CheckBox).Checked = selectedCheckboxes[i];
        }
    }
}
</pre>
<p>Welcome for Feedbacks/Suggestions/Comments. Thank you for reading my blog. Happy Programming <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2007/05/14/databinding-in-aspnet-dropdownlist/" title="Permanent link to Databinding in ASP.Net dropdownlist">Databinding in ASP.Net dropdownlist</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/06/21/asp-net-default-button-and-master-pages/" title="Permanent link to ASP.Net Default button and Master Pages">ASP.Net Default button and Master Pages</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/" title="Permanent link to Implementing Custom Paging in DataRepeater using C# and SQL Server">Implementing Custom Paging in DataRepeater using C# and SQL Server</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/04/14/how-to-convert-xmlnodelist-to-datatable/" title="Permanent link to How To Convert XMLNodeList To DataTable">How To Convert XMLNodeList To DataTable</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/11/15/how-to-persist-checkbox-state-in-gridview-while-paging/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ArgumentException &#8211; This row already belongs to another table</title>
		<link>http://www.dotnetthoughts.net/2011/11/03/argumentexception-this-row-already-belongs-to-another-table/</link>
		<comments>http://www.dotnetthoughts.net/2011/11/03/argumentexception-this-row-already-belongs-to-another-table/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 12:44:40 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[ADO.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[DataTable]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=1939</guid>
		<description><![CDATA[Today one of my colleague got this problem while copying datarow from one table to another, he just want to update few values of a same schema dataTable. So he used following method. He was getting an exception like &#8220;This &#8230; <a href="http://www.dotnetthoughts.net/2011/11/03/argumentexception-this-row-already-belongs-to-another-table/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today one of my colleague got this problem while copying datarow from one table to another, he just want to update few values of a same schema dataTable. So he used following method.</p>
<pre class="brush: csharp; title: ; notranslate">
for (var i = 0; i &lt; 10; i++)
{
	var row = dataTable.NewRow();
	row[0] = i.ToString();
	dataTable.Rows.Add(row);
}

for (int i = 0; i &lt; 10; i++)
{
	var dr = dataTable.Rows[i];
	dr[0] = (i + 10).ToString();
	//This line throws exception
	dataTable1.Rows.Add(dr);
}
</pre>
<p>He was getting an exception like &#8220;This row already belongs to another table&#8221;. This exception is coming because of A DataRow can only belong to a single data table.</p>
<div id="attachment_1941" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/exception1.jpg"><img class="size-medium wp-image-1941" title="ArgumentException – This row already belongs to another table  " src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/exception1-300x143.jpg" alt="ArgumentException – This row already belongs to another table  " width="300" height="143" /></a><p class="wp-caption-text">ArgumentException – This row already belongs to another table</p></div>
<p>I got this exception many times (in good(?) old ASP.Net 1.1 days <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) and able to fix, but today I couldn&#8217;t remember the solution. Later we fixed it, and here is the solutions.</p>
<ol>
<li>Using Item array property : Instead of using the row directly from other second table, use the ItemArray property.
<pre class="brush: csharp; title: ; notranslate">
var dr = dataTable.Rows[i].ItemArray;
dr[0] = (i + 10).ToString();
dataTable.Rows.Add(dr);
</pre>
</li>
<li>Using DataTable.Clone() and ImportRow() methods : Instead of using creating a new DataTable with same schema again, clone the DataTable and using ImportRow method.
<pre class="brush: csharp; title: ; notranslate">
DataTable dataTable1 = dataTable.Clone();
var dr = dataTable.Rows[i];
dr[0] = (i + 10).ToString();
dataTable1.ImportRow(dr);
</pre>
</li>
</ol>
<p>Happy coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and Thanks to Sarath</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/04/14/how-to-convert-xmlnodelist-to-datatable/" title="Permanent link to How To Convert XMLNodeList To DataTable">How To Convert XMLNodeList To DataTable</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/" title="Permanent link to Import Data from Excel using C# &#8211; Part 2">Import Data from Excel using C# &#8211; Part 2</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/12/31/text-truncated-to-255-characters-when-reading-from-excel-using-oledb/" title="Permanent link to Text truncated to 255 characters when reading from Excel using OLEDB">Text truncated to 255 characters when reading from Excel using OLEDB</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/" title="Permanent link to How to create a RSS feed using ASP.Net">How to create a RSS feed using ASP.Net</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/11/03/argumentexception-this-row-already-belongs-to-another-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

