<?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>TechnoBlogy &#187; ADO.NET</title>
	<atom:link href="http://technoblogy.net/category/net/ado-net/feed" rel="self" type="application/rss+xml" />
	<link>http://technoblogy.net</link>
	<description>Technology with a Big difference</description>
	<lastBuildDate>Mon, 30 Aug 2010 09:10:56 +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>Return the value for the specific attribute using XPathNodeIterator</title>
		<link>http://technoblogy.net/return-the-value-for-the-specific-attribute-using-xpathnodeiterator/</link>
		<comments>http://technoblogy.net/return-the-value-for-the-specific-attribute-using-xpathnodeiterator/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 15:14:17 +0000</pubDate>
		<dc:creator>Nauman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://technoblogy.net/return-the-value-for-the-specific-attribute-using-xpathnodeiterator/</guid>
		<description><![CDATA[I will use the following XML document in the example below. &#60;? xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34; ?&#62; &#60; users &#62; &#60; user FirstName =&#34;Tom&#34; LastName =&#34;Adams&#34; Age =&#34;23&#34; /&#62; &#60; user FirstName =&#34;Jhon&#34; LastName =&#34;Brams&#34; Age =&#34;17&#34; /&#62; &#60; user FirstName =&#34;Bill&#34; LastName =&#34;Smith&#34; Age =&#34;33&#34; /&#62; &#60;/ users &#62; The code prints out all the [...]]]></description>
			<content:encoded><![CDATA[<p>I will use the following XML document in the example below.</p>
<blockquote><p>&lt;? xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;      <br />&lt; users &gt;      <br />&lt; user FirstName =&quot;Tom&quot; LastName =&quot;Adams&quot; Age =&quot;23&quot; /&gt;      <br />&lt; user FirstName =&quot;Jhon&quot; LastName =&quot;Brams&quot; Age =&quot;17&quot; /&gt;      <br />&lt; user FirstName =&quot;Bill&quot; LastName =&quot;Smith&quot; Age =&quot;33&quot; /&gt;       <br />&lt;/ users &gt;</p>
</blockquote>
<p>The code prints out all the user nodes of the users node that have an age with a value greater than 17</p>
<blockquote><p>// Read the xml from resource file</p>
<p>string xml = Resources.Users;      <br />using (StringReader sr = new StringReader(xml))       <br />{      <br />XPathDocument document = new XPathDocument(sr);       <br />XPathNavigator navigator = document.CreateNavigator();       <br />XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);</p>
<p>//Selects all the user nodes of the users node that     <br />// have an age with a value greater than 17</p>
<p>XPathNodeIterator it = navigator.Select(&quot;/users/user[@Age&gt;17]&quot;);     <br />while (it.MoveNext())      <br />{       <br />// access the atributes      <br />string firstName = it.Current.GetAttribute(&quot;FirstName&quot;, ns.DefaultNamespace);      <br />string lastName = it.Current.GetAttribute(&quot;LastName&quot;, ns.DefaultNamespace);      <br />// Print out      <br />Console.WriteLine( &quot;{0} {1}&quot;, firstName, lastName );       <br />}       <br />}</p>
</blockquote>
<script type="text/javascript" class="owbutton" src="http://onlywire.com/btn/button_1305" title="Return the value for the specific attribute using XPathNodeIterator" url="http://technoblogy.net/return-the-value-for-the-specific-attribute-using-xpathnodeiterator/"></script>]]></content:encoded>
			<wfw:commentRss>http://technoblogy.net/return-the-value-for-the-specific-attribute-using-xpathnodeiterator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Row already belongs to another table</title>
		<link>http://technoblogy.net/row-already-belongs-to-another-table/</link>
		<comments>http://technoblogy.net/row-already-belongs-to-another-table/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 15:12:51 +0000</pubDate>
		<dc:creator>Nauman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[datarow]]></category>
		<category><![CDATA[datatable]]></category>
		<category><![CDATA[rows]]></category>

		<guid isPermaLink="false">http://technoblogy.net/row-already-belongs-to-another-table/</guid>
		<description><![CDATA[When copying a row from one datatable to another you might get the error &#34;this row already belongs to another table&#34;. Typically you might write code like this: string sConnString = ConfigurationManager.ConnectionStrings[&#34;NorthwindConn&#34;].ConnectionString; DataSet ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(sConnString,CommandType.Text, &#34;select * from suppliers order by supplierid&#34;); int nRowCount = 0; DataTable dt = ds.Tables[0];DataTable dt2 = dt.Clone(); dt2.Clear(); [...]]]></description>
			<content:encoded><![CDATA[<p>When copying a row from one datatable to another you might get the error &quot;this row already belongs to another table&quot;.    <br />Typically you might write code like this:</p>
<blockquote><pre>string sConnString = ConfigurationManager.ConnectionStrings[&quot;NorthwindConn&quot;].ConnectionString; </pre>
<pre>DataSet ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(sConnString,CommandType.Text, </pre>
<pre>&quot;select * from suppliers order by supplierid&quot;); </pre>
<pre>int nRowCount = 0;</pre>
<pre>DataTable dt = ds.Tables[0];DataTable dt2 = dt.Clone();</pre>
<pre>dt2.Clear();</pre>
<pre>foreach (DataRow row in dt.Rows)</pre>
<pre>{ </pre>
<pre>dt2.Rows.Add(row);</pre>
<pre>nRowCount++;</pre>
<pre>if (nRowCount == 10)</pre>
<pre>{</pre>
<pre>DataRow rowNew = dt2.NewRow();</pre>
<pre>rowNew[&quot;supplierid&quot;] = -12345;</pre>
<pre>dt2.Rows.Add(rowNew);</pre>
<pre>nRowCount = 0;</pre>
<pre>}</pre>
<pre>} </pre>
</blockquote>
<p>The correct way is instead to &quot;import&quot; the row using ImportRow</p>
<blockquote>
<pre>string sConnString = ConfigurationManager.ConnectionStrings[&quot;NorthwindConn&quot;].ConnectionString;</pre>
<pre>DataSet ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(sConnString,CommandType.Text,</pre>
<pre>&quot;select * from suppliers order by supplierid&quot;); </pre>
<pre>int nRowCount = 0; </pre>
<pre>DataTable dt = ds.Tables[0]; </pre>
<pre>DataTable dt2 = dt.Clone(); </pre>
<pre>dt2.Clear(); </pre>
<pre>foreach (DataRow row in dt.Rows) </pre>
<pre>{ </pre>
<pre>dt2.ImportRow(row); </pre>
<pre>nRowCount++; </pre>
<pre>if (nRowCount == 10) </pre>
<pre>{ </pre>
<pre>DataRow rowNew = dt2.NewRow(); </pre>
<pre>rowNew[&quot;supplierid&quot;] = -12345; </pre>
<pre>dt2.Rows.Add(rowNew); </pre>
<pre>nRowCount = 0; </pre>
<pre>} </pre>
<p>}</p>
</blockquote>
<script type="text/javascript" class="owbutton" src="http://onlywire.com/btn/button_1305" title="Row already belongs to another table" url="http://technoblogy.net/row-already-belongs-to-another-table/"></script>]]></content:encoded>
			<wfw:commentRss>http://technoblogy.net/row-already-belongs-to-another-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Stored Procedures in SQL Serrver 2005</title>
		<link>http://technoblogy.net/debugging-stored-procedures-in-sql-serrver-2005/</link>
		<comments>http://technoblogy.net/debugging-stored-procedures-in-sql-serrver-2005/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 13:32:45 +0000</pubDate>
		<dc:creator>Nauman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[ado]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://technoblogy.net/debugging-stored-procedures-in-sql-serrver-2005/</guid>
		<description><![CDATA[Pre-requisites 1. Find the .exe file under the directory, C:\Program Files\Microsoft SQL Server\90\Shared\1033\rdbgsetup.exe or similar path where the SQL Server was installed. The file rdbgsetup.exe stands for &#8216;RemoteDeBuGsetup&#8217;. Look for the emphasis on letters. The filename reads rdbgsetup because, we are going to debug a stored procedures of a database available in some remote physical [...]]]></description>
			<content:encoded><![CDATA[<h5>Pre-requisites </h5>
<p>1. Find the .exe file under the directory, C:\Program Files\Microsoft SQL Server\90\Shared\1033\rdbgsetup.exe or similar path where the SQL Server was installed. The file rdbgsetup.exe stands for &#8216;RemoteDeBuGsetup&#8217;. Look for the emphasis on letters. The filename reads rdbgsetup because, we are going to debug a stored procedures of a database available in some remote physical server. Either we need to sit at the server and debug them or we should be in a position to debug the stored procedure remotely. This action should be performed on the physical server where SQL Server 2005 was installed.    <br />2. The user who debugs the stored procedure should be a member of SQL Server&#8217;s fixed Server role, SysAdmin.     <br />As a DBA, I may need to grant this privilege to the user who is in need of debugging a stored procedure. When I do that, I should trust the user so that he/she will not mess-up anything on the Server. Ok, I believe my users are trust worthy and I issue the following T-SQL command to assigning a fixer server role, SysAdmin.     <br />The command to do so is as follows     <br />EXEC master..sp_addsrvrolemember @loginame = N&#8217;&amp;amp;amp;amp;amp;lt;YourLoginName either SQL Server Authentication or Windows Authentication&amp;amp;amp;amp;amp;gt;&#8217;, @rolename = N&#8217;sysadmin&#8217;     <br />This can however be accomplished with SQL Server Management Studio&#8217;s IDE. Perhaps, I prefer using T-SQL commands.     <br />Note: The parameters to the stored procedure, sp_addsrvrolemember are of type, Nvarchar and it stands for (National Variable Character), which usually holds Unicode characters.     <br />Now, we are all set for debugging stored procedures.     <br />Process to Debug a stored procedure     <br />1. Open the Microsoft Visual Studio 2005 IDE.     <br />2. Select Server Explorer option from the View Menu as follows:</p>
<p><a href="http://technoblogy.net/wp-content/uploads/2009/11/123.jpg"><img title="123" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="165" alt="123" src="http://technoblogy.net/wp-content/uploads/2009/11/123_thumb.jpg" width="236" align="left" border="0" /></a> </p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>3. From the Data Connections node, right click and select, &#8216;Add connection&#8217;. Fill in the details to connect to the intended SQL Server and the database using the login who has a fixed server role, SysAdmin. Click on Test connection. Once the connection succeeds, the overall screen should look like the following.</p>
<p><a href="http://technoblogy.net/wp-content/uploads/2009/11/234.jpg"><img title="234" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="440" alt="234" src="http://technoblogy.net/wp-content/uploads/2009/11/234_thumb.jpg" width="556" border="0" /></a> </p>
</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p></p>
<p>&#160;</p>
<p>&#160;</p>
<p>4. Expand the data connection just added, and navigate to the Stored Procedures node.    <br />5. Expand the Stored Procedures node and select the intended SP to be debugged.     <br />6. Right click and select open to view the source code of the selected stored procedure.     <br />7. Place a break point on the intended line of code where debugging needs to be started its usually the way .NET Developers perform.     <br />8. After performing the above steps the screen shot should look like the following.     <br />9. After performing the above steps the screen shot should like the following:</p>
<p> <a href="http://technoblogy.net/wp-content/uploads/2009/11/345.jpg"><img title="345" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="312" alt="345" src="http://technoblogy.net/wp-content/uploads/2009/11/345_thumb.jpg" width="503" align="left" border="0" /></a>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>10. Right click on the stored procedure from the &#8216;Server Explorer&#8217; and select &#8216;Step-Into Stored Procedure&#8217; as shown below.</p>
<p><a href="http://technoblogy.net/wp-content/uploads/2009/11/456.jpg"><img title="456" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="260" alt="456" src="http://technoblogy.net/wp-content/uploads/2009/11/456_thumb.jpg" width="243" align="left" border="0" /></a> </p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>11. This action brings up the following screen shot.</p>
<p><a href="http://technoblogy.net/wp-content/uploads/2009/11/567.jpg"><img title="567" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="155" alt="567" src="http://technoblogy.net/wp-content/uploads/2009/11/567_thumb.jpg" width="240" align="left" border="0" /></a> </p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>12. Enter the needful values and click Ok. The next shot will be the following.</p>
<p><a href="http://technoblogy.net/wp-content/uploads/2009/11/678.jpg"><img title="678" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="260" alt="678" src="http://technoblogy.net/wp-content/uploads/2009/11/678_thumb.jpg" width="232" align="left" border="0" /></a> </p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>13. From here on, its usual .NET debugging stuff. Use Step-Into and Step-Over and Step-out from the shortcuts menu or pressing F11,F10, Shift+F11</p>
<script type="text/javascript" class="owbutton" src="http://onlywire.com/btn/button_1305" title="Debugging Stored Procedures in SQL Serrver 2005" url="http://technoblogy.net/debugging-stored-procedures-in-sql-serrver-2005/"></script>]]></content:encoded>
			<wfw:commentRss>http://technoblogy.net/debugging-stored-procedures-in-sql-serrver-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Different operations on Datatable (Add, Edit, delete, Sort etc) and XML</title>
		<link>http://technoblogy.net/different-operations-on-datatable-add-edit-delete-sort-etc-and-xml/</link>
		<comments>http://technoblogy.net/different-operations-on-datatable-add-edit-delete-sort-etc-and-xml/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 13:31:35 +0000</pubDate>
		<dc:creator>Nauman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[datarow]]></category>
		<category><![CDATA[datatable]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[edit]]></category>
		<category><![CDATA[operations]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://technoblogy.net/different-operations-on-datatable-add-edit-delete-sort-etc-and-xml/</guid>
		<description><![CDATA[Introduction DataTable is a central object in the ADO.NET library. If you are working with ADO.NET &#8211; accessing data from database, you can not escape from DataTable. Other objects that use DataTable are DataSet and DataView. In this tutorials, I will explain how to work with DataTable. I have tried to cover most of the [...]]]></description>
			<content:encoded><![CDATA[<h4>Introduction</h4>
<p>DataTable is a central object in the ADO.NET library. If you are working with ADO.NET &#8211; accessing data from database, you can not escape from DataTable. Other objects that use DataTable are DataSet and DataView. In this tutorials, I will explain how to work with DataTable. I have tried to cover most of the frequently used activity in the DataTable, I hope you will like it. </p>
<h4>Creating a DataTable</h4>
<p>To create a DataTable, you need to use System.Data namespace, generally when you create a new class or page, it is included by default by the Visual Studio. Lets write following code to create a DataTable object. Here, I have pased a string as the DataTable name while creating DataTable object.</p>
<blockquote><p><em>//Instantiate DataTable</em>       <br /><em>DataTable dTable = new DataTable(&quot;NewTable&quot;);</em></p>
</blockquote>
<h4><strong><strong>Creating Columns in the DataTable</strong> </strong></h4>
<p>To create column in the DataTable, you need to use DataColumn object. Instantiate the DataColumn object and pass column name and its data type as parameter. Then call add method of DataTable column and pass the DataColumn object as parameter.</p>
<blockquote><pre><em>//Create columns for the datatable</em></pre>
<pre><em>DataColumn dCol = new DataColumn(&quot;RowID&quot;, System.Int32);</em></pre>
<pre><em>dTable.Columns.Add(dCol);</em></pre>
</blockquote>
<h4>Specifying AutoIncrement column in the DataTable</h4>
<p>To specify a column as AutoIncrement (naturally it should be an integer type of field only), you need to set some properties of the column like AutoIncrement, AutoIncrementSeed. See the code below, here I am setting the first column &quot;AutoID&quot; as autoincrement field. Whenever a new row will be added its value will automatically increase by 1 as I am specified AutoIncrementSeed value as 1.</p>
<blockquote>
<pre><em>//Specify previously create column as auto increment.</em></pre>
<pre><em>dCol.AutoIncrement = true;</em></pre>
<pre><em>dCol.AutoIncrementSeed = 1;</em></pre>
<pre><em>dCol.readonly = true;</em></pre>
</blockquote>
<p>If you want a particular column to be a unique column ie. you don&#8217;t want duplicate records into that column, then set its Unique property to true like below.</p>
<blockquote>
<p><em>dCol.Unique = true;</em></p>
</blockquote>
<h4>Specifying Primary Key column in the DataTable</h4>
<p>To set the primary key column in the DataTable, you need to create arrays of column and store column you want as primary key for the DataTable and set its PrimaryKey property to the column arrays. See the code below.</p>
<blockquote>
<pre><em>//Create Primary key on this field.</em></pre>
<pre><em>DataColumn []Prime = new DataColumn[1];</em></pre>
<pre><em>Prime[0] = dCol;</em></pre>
<pre><em>dTabl.Primarykey = Prime;</em></pre>
</blockquote>
<p>We have used only one column as the primary key in the datatable, but you can add more than one column in the Prime array object and that will create a composite primary key in the datatable. Till now we have created the DataTable, now lets populate the DataTable with some data.</p>
<h4>Populating data into DataTable</h4>
<p>There are two ways to populate DataTable.</p>
<h5>Using DataRow object</h5>
<p>Look at the code below, I have created a DataRow object above the loop and I am assiging its value to the dTable.NewRow() inside the loop. After specifying columns value, I am adding that row to the DataTable using dTable.Rows.Add method.</p>
<blockquote>
<pre><em>//Populate the DataTable using the DataRow object.</em></pre>
<pre><em>DataRow dRow ;</em></pre>
<pre><em>For(int i=0 ; i&lt;5; ++i)</em></pre>
<pre><em>{</em></pre>
<blockquote>
<pre>                <em>dRow = dTable.NewRow();</em>                </pre>
<pre>                <em>dRow[&quot;RowId&quot;] = i + 1;</em>       </pre>
<pre>                <em>dTable.Rows.Add(dRow);</em>         </pre>
</blockquote>
<pre><em>}</em></pre>
</blockquote>
<p>Instead of using the column name, you can use ColumnIndex too, however it is not suggested to use magic numbers. Same applies for reading and writing of data.</p>
<h5><strong>Asiging the value of column using Arrays</strong> </h5>
<p>In following code, I have specified the values of every column as the array separated by comma (,) in the Add method of the dTable.Rows.</p>
<blockquote>
<pre><em>//Manually adding rows using array of values.</em></pre>
<pre><em>dTable.Rows.Add(0,&quot;1&quot;);</em></pre>
<pre><em>dTable.Rows.Add(3,&quot;3&quot;);</em></pre>
</blockquote>
<h5>Modifying Row Data</h5>
<p>To edit the data of the row, sets its column value using row index or by specifying the column name. In below example, I am updating the 3rd row of the DataTable as I have specified the row index as 2 (dTable.Rows[2]).</p>
<blockquote>
<pre> <em>//Modify certain value into datatable.</em>
<em>dTable.Rows[2][&quot;RowID&quot;] = 3;</em>
<em>dTable.AcceptChanges();</em></pre>
</blockquote>
<h5>Deleting Row</h5>
<p>To delete a row into DataTable, call the rows.Delete() method followed by AcceptChanges() method. AcceptChanges() method commits all the changes made by you to the DataTable. Here Row[1] is the index of the row, in this case 2nd row will be deleted as in collection (here rows collection) count start from 0.</p>
<blockquote>
<pre><em>//Delete Row.</em></pre>
<pre><em>dTable.Rows[1].Delete();</em></pre>
<pre><em>dTable.AcceptChanges();</em></pre>
</blockquote>
<h5>Filtering data from DataTable </h5>
<p>To filter records from the DataTable, use Select method and pass necessary filter expression. In below code, the 1st line will simply filter all rows whose AutoID value is greater than 5. The 2nd line of the code filters the DataTable whose AutoID value is greater than 5 after sorting it.</p>
<blockquote>
<p><em>DataRow []rows = dTable.Select(filterCondition); </em></p>
<p><em>foreach( DataRow drow in rows ) </em></p>
<p><em>{ </em></p>
<p><em>dTable.Rows.Add(dRow.ItemArray); </em></p>
<p><em>} </em></p>
</blockquote>
<h5>Working with Aggregate functions</h5>
<p>We can use almost all aggregate functions with DataTable, however the syntax is bit different than standard SQL. Suppose we need to get the maximum value of a particular column, we can get it in the following way.</p>
<blockquote>
<p><em>DataRow []dRows = dTable.Select(&quot;RowID = MAX(RowID)&quot;); </em></p>
<p><em>string str = &quot;Max RowID: &quot; + dRows[0][&quot;RowID&quot;].ToString(); </em></p>
</blockquote>
<p>To get the sum of a particular column, we can use Compute method of the DataTable. Compute method of the DataTable takes two argument. The first argument is the expression to compute and second is the filter to limit the rows that evaluate in the expression. If we don&#8217;t want any filteration (if we need only the sum of the AutoID column for all rows), we can leave the second parameter as blank (&quot;&quot;).</p>
<blockquote>
<p><em>Object objSum = dTable.Compute(&quot;Sum(RowID)&quot;, &quot;RowID &gt; 10&quot;); OR Object objSum = dTable.Compute(&quot;Sum(RowID)&quot;,&quot;&quot;); </em></p>
<p><em>string sum = &quot;Sum: &quot; + objSum.ToString(); </em></p>
</blockquote>
<h4>Sorting data of DataTable </h4>
<h5>Using DataView </h5>
<p>See the code below. I have created a DataView object by passing my DataTable as parameter, so my DataView will have all the data of the DataTable. Now, simply call the Sort method of the DataView and pass the sort expression. Your DataView object have sorted records now, You can either directly specify the Source of the Data controls object like GridView, DataList to bind the data or if you need to loop through its data you can use ForEach loop as below.</p>
<blockquote>
<p>//<em>Sorting dataTable</em></p>
<p><em>DataView dv = new DataView(dTable);</em></p>
<p><em>dv.Sort = &quot;RowID DESC&quot;;</em></p>
</blockquote>
<h5>Using DataTable.Select() method</h5>
<p>Yes, you can sort all the rows using Select method too provided you have not specified any filter expression. If you will specify the filter expression, ofcourse your rows will be sorted but filter will also be applied. A small drawback of this way of sorting is that it will return array of DataRows as descibed earlier so if you are planning to bind it to the Data controls like GridView or DataList you will have for form a DataTable by looping through because directly binding arrays of rows to the Data controls will not give desired results.</p>
<blockquote>
<p><em>DataRow [] drow = dTable.Select(&quot;&quot;, &quot;RowID DESC&quot;); </em></p>
</blockquote>
<h5>Writing and Reading XmlSchema of the DataTable</h5>
<p>If you need XmlSchema of the DataTabe, you can use WriteXmlSchema to write and ReadXmlSchema to read it. There are several overloads methods of both methods and you can pass filename, stream, TextReader, XmlReader etc. as the parameter. In this code, the schema will be written to the .xml file and will be read from there.</p>
<blockquote>
<p><em>//Creating schema defination for data Table </em></p>
<p><em>dTable.WriteXMLSchema(Server.MapPath(&quot;~/Schema.xml&quot;)); </em></p>
<p><em>DataTable dSchema = new DataTable(); </em></p>
<p><em>dSchema.ReadXMLSchema(Server.MapPath(&quot;~/Schema.xml&quot;)); </em></p>
</blockquote>
<h5>Reading/Writing from/to Xml</h5>
<p>If you have a scenario, where you need to write the data of the DataTable into xml format, you can use WriteXml method of the DataTable. Note that WriteXml method will not work if you will not specify the name of the DataTable object while creating it. Look at the first code block above, I have passed &quot;Dynamically_Generated&quot; string while creating the instance of the DataTable. If you will not specify the name of the DataTable then you will get error as WriteXml method will not be able to serialize the data without it.</p>
<blockquote>
<p><em>dTable.writeXML(Server.MapPath(&quot;~/Schema.xml&quot;),XmlWriteMode .WriteSchema);</em></p>
</blockquote>
<p>If you are planning to read the xml you have just created into the DataTable sometime later then you need to specify XmlWriteMode.WriteSchema too as the 2nd parameter while calling WriteXml method of the DataTable otherwise normally WriteXml method doesn&#8217;t write schema of the DataTable. In the abscence of the schema, you will get error (DataTable does not support schema inference from Xml) while calling ReadXml method of the DataTable.</p>
<script type="text/javascript" class="owbutton" src="http://onlywire.com/btn/button_1305" title="Different operations on Datatable (Add, Edit, delete, Sort etc) and XML" url="http://technoblogy.net/different-operations-on-datatable-add-edit-delete-sort-etc-and-xml/"></script>]]></content:encoded>
			<wfw:commentRss>http://technoblogy.net/different-operations-on-datatable-add-edit-delete-sort-etc-and-xml/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
