<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>Makiwa &#187; ADO.NET</title>
	<link>http://www.makiwa.com</link>
	<description>Stuart Campbell's occasional musings about software development, etc.</description>
	<pubDate>Thu, 08 May 2008 11:04:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Return value parameter returns null</title>
		<link>http://www.makiwa.com/index.php/2006/08/29/return-value-parameter-returns-null/</link>
		<comments>http://www.makiwa.com/index.php/2006/08/29/return-value-parameter-returns-null/#comments</comments>
		<pubDate>Tue, 29 Aug 2006 14:07:48 +0000</pubDate>
		<dc:creator>Stu</dc:creator>
		
		<category><![CDATA[ADO.NET]]></category>

		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.makiwa.com/index.php/2006/08/29/return-value-parameter-returns-null/</guid>
		<description><![CDATA[I was really confused this morning. I was executing a stored proc that returned a single row of data, and a return value. However, the return value was always null - even though the stored proc ALWAYS returned a return value. The code was domething like this:
PLAIN TEXT
C#:




SqlParameter retval = new SqlParameter&#40;"@RETURN_VALUE", SqlDbType.Int&#41;;


retval.Direction = ParameterDirection.ReturnValue;


command1.Parameters.Add&#40;retval&#41;;


&#160;


using [...]]]></description>
			<content:encoded><![CDATA[<p>I was really confused this morning. I was executing a stored proc that returned a single row of data, and a return value. However, the return value was always null - even though the stored proc ALWAYS returned a return value. The code was domething like this:</p>
<div class="igBar"><span id="lcsharp-2"><a href="#" onclick="javascript:showPlainTxt('csharp-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-2">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SqlParameter retval = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SqlParameter<span style="color: #000000;">&#40;</span><span style="color: #808080;">"@RETURN_VALUE"</span>, SqlDbType.<span style="color: #FF0000;">Int</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">retval.<span style="color: #0000FF;">Direction</span> = ParameterDirection.<span style="color: #0000FF;">ReturnValue</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">command1.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>retval<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SqlDataReader reader = command1.<span style="color: #0000FF;">ExecuteReader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>reader.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; var1 = reader<span style="color: #000000;">&#91;</span><span style="color: #808080;">"Col1"</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; var2 = reader<span style="color: #000000;">&#91;</span><span style="color: #808080;">"col2"</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>retval.<span style="color: #0000FF;">Value</span> != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; errorCode = Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>retval.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; errorCode = -<span style="color: #FF0000;color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It turns out that <strong>you need to close the reader before you can access the return value parameter</strong>.<br />
From <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlDataReaderClassCloseTopic.asp">MSDN Library: SqlDataReader.Close()</a></p>
<blockquote><p>The Close method fills in the values for output parameters, return values and RecordsAffected...</p></blockquote>
<p>I can see why it's been implemented like that (the return value, output parameters and number of records affected come at the end, after the result set) - but I have to say it is counter-intuitive to have to close the reader before you can access these values.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.makiwa.com/index.php/2006/08/29/return-value-parameter-returns-null/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Enterprise Library Data Access and Stored Procedure Synonyms</title>
		<link>http://www.makiwa.com/index.php/2006/08/23/enterprise-library-data-access-and-stored-procedure-synonyms/</link>
		<comments>http://www.makiwa.com/index.php/2006/08/23/enterprise-library-data-access-and-stored-procedure-synonyms/#comments</comments>
		<pubDate>Tue, 22 Aug 2006 23:51:14 +0000</pubDate>
		<dc:creator>Stu</dc:creator>
		
		<category><![CDATA[ADO.NET]]></category>

		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.makiwa.com/index.php/2006/08/23/enterprise-library-data-access-and-stored-procedure-synonyms/</guid>
		<description><![CDATA[The Enterprise Library Data Access application block offers some time-saving routines for calling stored procedures. However, they don't all work when the stored procedure is a synonym.
For example, once I have a Database object, I can get a DataReader from a stored proc called GetUser by simply calling:
PLAIN TEXT
C#:




Database db = DatabaseFactory.CreateDatabase&#40;&#41;;


IDataReader reader = db.ExecuteReader&#40;"GetUser", [...]]]></description>
			<content:encoded><![CDATA[<p>The Enterprise Library Data Access application block offers some time-saving routines for calling stored procedures. However, they don't all work when the stored procedure is a synonym.</p>
<p>For example, once I have a Database object, I can get a DataReader from a stored proc called GetUser by simply calling:</p>
<div class="igBar"><span id="lcsharp-6"><a href="#" onclick="javascript:showPlainTxt('csharp-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-6">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Database db = DatabaseFactory.<span style="color: #0000FF;">CreateDatabase</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IDataReader reader = db.<span style="color: #0000FF;">ExecuteReader</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">"GetUser"</span>, userId<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>However, if the stored procedure is a synonym I get an error:</p>
<p><code>System.InvalidOperationException : The stored procedure 'GetUser' doesn't exist.</code></p>
<p>The problem isn't actually the Enterprise Library. The problem is - as far as I can see - a bug in ADO.NET. It is the following call that fails:</p>
<div class="igBar"><span id="lcsharp-7"><a href="#" onclick="javascript:showPlainTxt('csharp-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-7">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SqlCommandBuilder.<span style="color: #0000FF;">DeriveParameters</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>SqlCommand<span style="color: #000000;">&#41;</span>discoveryCommand<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It seems to me that <code>SqlCommandBuilder.DeriveParameters(SqlCommand command)</code> hasn't been tested with the stored-proc-is-a-synonym scenario: the SQL that it uses to retrieve the parameters returns an empty result set.</p>
<p>I'm not too phased by this problem because I don't really like specifying the parameter values by ordinal position, which is what you're doing when you just supply a <code>params object[]</code> of values. So, I actually prefer using <strong>the workaround</strong>, which is to <strong>create the parameters explicitly</strong>:</p>
<div class="igBar"><span id="lcsharp-8"><a href="#" onclick="javascript:showPlainTxt('csharp-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-8">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Database db = DatabaseFactory.<span style="color: #0000FF;">CreateDatabase</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SqlCommand cmd = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SqlCommand<span style="color: #000000;">&#40;</span><span style="color: #808080;">"GetUser"</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#40;</span>SqlConnection<span style="color: #000000;">&#41;</span>db.<span style="color: #0000FF;">CreateConnection</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cmd.<span style="color: #0000FF;">CommandType</span> = CommandType.<span style="color: #0000FF;">StoredProcedure</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">db.<span style="color: #0000FF;">AddInParameter</span><span style="color: #000000;">&#40;</span>cmd, <span style="color: #808080;">"@UserId"</span>, DbType.<span style="color: #0000FF;">Int32</span>, userId<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IDataReader reader = db.<span style="color: #0000FF;">ExecuteReader</span><span style="color: #000000;">&#40;</span>cmd<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.makiwa.com/index.php/2006/08/23/enterprise-library-data-access-and-stored-procedure-synonyms/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
