<?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>Pushing Pixels &#187; ASP2</title>
	<atom:link href="http://familywhitfield.co.uk/wordpress/category/software-development/asp2/feed/" rel="self" type="application/rss+xml" />
	<link>http://familywhitfield.co.uk/wordpress</link>
	<description>Computing and Digital Imaging</description>
	<lastBuildDate>Thu, 13 May 2010 20:13:34 +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>Tips for returning IDENTITY values from INSERT</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/#comments</comments>
		<pubDate>Sun, 14 Jan 2007 17:04:00 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=16</guid>
		<description><![CDATA[There&#8217;s a good article by Scott Guthrie here that describes the basics. Just scroll down to Tutorial 5 for the INSERT specific bit &#8211; it is pretty straightforward and there is no point my repeating it here. One point to note, however, and why I wrote this particular post. The key step in getting this [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a good article by Scott Guthrie <a href="http://aspalliance.com/914_Building_a_DAL_using_Strongly_Typed_TableAdapters_and_DataTables_in_VS_2005_and_ASPNET_20">here</a> that describes the basics. Just scroll down to Tutorial 5 for the INSERT specific bit &#8211; it is pretty straightforward and there is no point my repeating it here.</p>
<p>One point to note, however, and why I wrote this particular post. The key step in getting this working is changing the query type from NonQuery to Scalar. But this can get reset by Visual Studio if you choose the Configure option on the ObjectDataSource&#8217;s smart tags to modify the query in any way. If you don&#8217;t notice this, then suddenly the identity value stops getting returned and your code breaks.</p>
<p>The second tip is that by default the type returned by the insert query is decimal. This can cause some extra casting in your code if you were expecting it to be int. To fix this, just modify the SQL in Scott&#8217;s original article to be this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> CAST <span style="color: #66cc66;">&#40;</span>SCOPE_IDENTITY<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> int<span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Determining row counts for all tables in a database</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/#comments</comments>
		<pubDate>Thu, 11 Jan 2007 22:02:33 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[unit-testing]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=13</guid>
		<description><![CDATA[Here is a usful snippet if you want a way to get the number of rows in each table in your database. I use this in an admin-only page of the web application to provide some at-a-glance statistics. It is also really useful in unit tests for checking the correctness of business logic that may [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a usful snippet if you want a way to get the number of rows in each table in your database. I use this in an admin-only page of the web application to provide some at-a-glance statistics. It is also really useful in unit tests for checking the correctness of business logic that may create new entries in several tables in one transaction.</p>
<p><span id="more-13"></span></p>
<h3>SQL snippet</h3>
<p>The bit of SQL is quite straightforward, once you delve down into the depths of SQLServer&#8217;s system tables:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
   so<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> TableName<span style="color: #66cc66;">,</span>
   MAX<span style="color: #66cc66;">&#40;</span>si<span style="color: #66cc66;">.</span>rows<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>RowCount<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
   sys<span style="color: #66cc66;">.</span>sysobjects <span style="color: #993333; font-weight: bold;">AS</span> so
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
   sys<span style="color: #66cc66;">.</span>sysindexes <span style="color: #993333; font-weight: bold;">AS</span> si
   <span style="color: #993333; font-weight: bold;">ON</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>so<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> si<span style="color: #66cc66;">.</span>id
<span style="color: #993333; font-weight: bold;">WHERE</span>
   <span style="color: #66cc66;">&#40;</span>so<span style="color: #66cc66;">.</span>xtype <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'U'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> so<span style="color: #66cc66;">.</span>name
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> TableName</pre></div></div>

<h2>Presenting in a page</h2>
<p>There are a number of ways of getting this into an ASP2 page. I chose to do this via a new table adaptor, and to wrap the query in an Administration class in the business logic layer.</p>
<p>Paste the SQL snippet from above into the query builder to create a new table adapter on the data designer &#8211; I have a separate &#8220;admin.xsd&#8221; to keep these non-user level types away from the core application data types. By default you will get an  adapter called &#8220;sysobjectsTableAdapter&#8221; as it picks its name up from the first table in the query. You can change this if you need to.</p>
<p>You can now go direct to an ASP page by pointing an ObjectDataSource directly at this table adapter, or you can wrap it in an intermediate layer.</p>
<h2>Unit test example</h2>
<p>My unit test framework has a database reset mechanism that empties all tables and then creates a small set of known test data using the business logic layer (BLL). This empty and reset is done in the test fixture setup, and the subsequent raft of tests use a variety of methods to valid the integrity of the data.</p>
<p>A key test is ensuring that the expected number of rows in various tables have been created. This is particularly important as there are a number of linking and cross reference tables that are not directly seen by a user, but are there for the efficiency of the schema. So checking  all these have been set correctly after the BLL has done its work is vital.</p>
<p>In the test class I have something like this :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DBReset
<span style="color: #000000;">&#123;</span> ....
   <span style="color: #0600FF;">private</span> Dictionary TableRowCounts  <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary <span style="color: #008000;">;</span>
   ...
    <span style="color: #000000;">&#91;</span>TestFixtureSetup<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> FixtureSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        TableRowCounts.<span style="color: #0000FF;">Add</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Addresses&quot;</span>, <span style="color: #FF0000;">6</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        TableRowCounts.<span style="color: #0000FF;">Add</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Agencies&quot;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ...
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This sets up a list of all the tables I am interested in and maps the expected number of rows for each table after my fixture setup has initialised the database with known data.</p>
<p>I then have the following unit test as part of this fixture:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
 <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> CheckRowCountsExpected<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Verifies that the correct number of rows in each database</span>
    <span style="color: #008080; font-style: italic;">// has been created as a result of the reset and test</span>
    <span style="color: #008080; font-style: italic;">// data initialisation.</span>
    AdminBll abll <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AdminBll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Admin.<span style="color: #0000FF;">sysobjectsDataTable</span> tabs <span style="color: #008000;">=</span> abll.<span style="color: #0000FF;">GetTableRowCounts</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Admin.<span style="color: #0000FF;">sysobjectsRow</span> trow <span style="color: #0600FF;">in</span> tabs<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
       <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>TableRowCounts.<span style="color: #0000FF;">ContainsKey</span> <span style="color: #000000;">&#40;</span>trow.<span style="color: #0000FF;">TableName</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
       <span style="color: #000000;">&#123;</span>
          <span style="color: #008080; font-style: italic;">//Only check the tables specified in the dictionary</span>
          Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>TableRowCounts<span style="color: #000000;">&#91;</span>trow.<span style="color: #0000FF;">TableName</span><span style="color: #000000;">&#93;</span>,
             trow.<span style="color: #0000FF;">RowCount</span>,
             <span style="color: #666666;">&quot;Wrong rowcount for table &quot;</span> <span style="color: #008000;">+</span> trow.<span style="color: #0000FF;">TableName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I also have a second test that verifies the admin query against each table one at a time using direct SELECT COUNT queries in SQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t believe NUnit</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/#comments</comments>
		<pubDate>Fri, 05 Jan 2007 11:07:39 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP2]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Nunit]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[unit-testing]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=11</guid>
		<description><![CDATA[Just a quick note about a little problem I found using Nunit (2.2.9, but affects earlier revisions too). After a bunch of edits to my project, I suddenly started getting messages from NUnit GUI &#8220;could not load file or assembly nunit.core&#8221;. Try as I could, this would not go away. This included everything from reinstalling [...]]]></description>
			<content:encoded><![CDATA[<p><em>Just a quick note about a little problem I found using Nunit (2.2.9, but affects earlier revisions too).</em></p>
<p>After a bunch of edits to my project, I suddenly started getting messages from NUnit GUI &#8220;could not load file or assembly nunit.core&#8221;. Try as I could, this would not go away. This included everything from reinstalling NUnit to tracking DLL loading via SysInternal&#8217;s Process Explorer tool.<br />
My project is non trivial &#8211; a top level solution containing an ASP2 web project, a couple of library DLL&#8217;s for the business logic, a unit test executable, and a testing helper library to help cross reference test results against direct SQL calls. As I&#8217;ll be writing about in another post shortly, incorporating unit testing into an ASP project is a pain in the rear and so I had been restructuring things. My natural assumption based on the error message was I had messed up something in the linking/referencing.</p>
<p>But it turned out to be an error in the application config file &#8211; for some reason NUnit reports this as a DLL missing, not that you have mistyped a bit of config text. I simply had a closing  tag in the wrong place &#8211; still syntactically correct, however.<br />
<a title="Nunit DLL load issue" href="http://vibhu.info/2007/01/02/wierd-issue-could-not-load-file-or-assembly-nunitcore/">This link</a> is by someone else who hit the exact same problem, and he has written about in more detail.</p>
<p>So remember folks: <em>never assume a system generates the correct error message for all circumstances. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding ASP Role tables to your own database</title>
		<link>http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/#comments</comments>
		<pubDate>Fri, 29 Dec 2006 23:17:25 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=5</guid>
		<description><![CDATA[This is still work in progress, but I want to incorporate the user role management tables within my own database, as the concept of users and their distinct roles ties right in with the core business model of the new site. The following link explains the standalone tool to create the tables: ASP.NET SQL Server [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin-bottom: 12pt;"><span lang="EN-GB">This is still work in progress, but I want to incorporate the user role management tables within my own database, as the concept of users and their distinct roles ties right in with the core business model of the new site.<br />
The following link explains the standalone tool to create the tables:<br />
<a href="http://msdn2.microsoft.com/en-us/library/ms229862.aspx" target="_blank">ASP.NET SQL Server Registration Tool</a></span></p>
<p>Invoking the exe runs a wizard requesting the database service &#8211; it needs to match the connection string as mentioned in the gotcha above. Since no parameters are specified, it then creates a database &#8220;aspnetdb&#8221; with all the role tables. This is not quite what I want, as I need these added to my application database.</p>
<p>The answer is to use the command line:<br />
aspnet_regsql -S  -E -A all -d</p>
<p>If you run SQL Management Server Studio Express before and after &#8211; not forgetting to hit &#8216;refresh&#8217; &#8211; then a bunch of new tables are added: aspnet_Applications, aspnet_Roles, aspnet_SchemaVersions, aspnet_Users, aspnet_UsersInRoles etc. Now it is just a matter of hooking those up with the application tables.</p>
<p>Since I wanted membership and roles as well as basic authentication I used the &#8220;-A all&#8221; option to add everything in. Of course I get profile and web-part support too with this, but I may choose to exploit such features in the future.<a name="References"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gotcha on setting up a data connection</title>
		<link>http://familywhitfield.co.uk/wordpress/2006/12/29/gotcha-on-setting-up-a-data-connection/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2006/12/29/gotcha-on-setting-up-a-data-connection/#comments</comments>
		<pubDate>Fri, 29 Dec 2006 14:26:51 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP2]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=3</guid>
		<description><![CDATA[This one gets me every time. When you set up a new data connection to a database running on a local copy of Sql Server Express, in the &#8220;Add Connection&#8221; dialog you need to prefix your machine name to the defaulted value of &#8220;SQLEXPRESS&#8221;. E.g. if the network name of your PC is called &#8220;devpc&#8221;, [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span lang="EN-GB">This one gets me every time. When you set up a new data connection to a database running on a local copy of Sql Server Express, in the &#8220;Add Connection&#8221; dialog you need to prefix your machine name to the defaulted value of &#8220;SQLEXPRESS&#8221;. E.g. if the network name of your PC is called &#8220;devpc&#8221;, then the connection string is:</span></p>
<ul>
<li>DEVPC\SQLEXPRESS</li>
</ul>
<p>While this is the obvious choice and works for many circumstances, a subtle variation is to use the following instead:</p>
<ul>
<li>localhost\SQLEXPRESS</li>
</ul>
<p>This now points the connection to an instance of SQL Server Express running on your local machine. If working in a team where each developer needs their own local copy of a database, then setting this in the web.config file can be a real help.</p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2006/12/29/gotcha-on-setting-up-a-data-connection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
