<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>bencoffman.com/blog - SQL</title>
    <link>http://bencoffman.com/blog/</link>
    <description>News about Tech and a few other things.</description>
    <language>en-us</language>
    <copyright>Ben Coffman</copyright>
    <lastBuildDate>Wed, 14 Jul 2010 04:14:10 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>coffmanben@gmail.com</managingEditor>
    <webMaster>coffmanben@gmail.com</webMaster>
    <item>
      <trackback:ping>http://bencoffman.com/blog/Trackback.aspx?guid=045e6f8a-b43f-4c79-8d7f-545b9eb95304</trackback:ping>
      <pingback:server>http://bencoffman.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://bencoffman.com/blog/PermaLink,guid,045e6f8a-b43f-4c79-8d7f-545b9eb95304.aspx</pingback:target>
      <dc:creator>Ben Coffman</dc:creator>
      <wfw:comment>http://bencoffman.com/blog/CommentView,guid,045e6f8a-b43f-4c79-8d7f-545b9eb95304.aspx</wfw:comment>
      <wfw:commentRss>http://bencoffman.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=045e6f8a-b43f-4c79-8d7f-545b9eb95304</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img src="http://bencoffman.com/blog/content/binary/microsoft_.net_logo.png" height="75px" width="120px" border="0" />
        <br />
        <br />
        <b>
          <br />
          <br />
Error Readout:<br /></b>System.IndexOutOfRangeException:<b><br /><br /></b><b>Fix:<br /></b>I encountered the above error when I tried to run the query, 
<br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">SELECT</span><span style="color: Fuchsia; background-color: Transparent; font-family: Courier New; font-size: 11px;">MAX</span>(expression
)<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">FROM</span> tables<br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">WHERE</span> predicates;</span><br /></pre>
and then loop through the results using a datareader with code like this,<br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">while</span>(rdr.Read())<br />
{<br />
someLabel.Items.Add(rdr[<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"ColumnName"</span>].ToString());<br />
}<br /></span></pre>I got the error above, because when you do a "max" sql query it doesn't
return any column header. 
<br /><br />
All you need to do is give the sql result set a column header.<br /><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"><br /><b></b></span></span><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">SELECT</span><span style="color: Fuchsia; background-color: Transparent; font-family: Courier New; font-size: 11px;">MAX</span>(expression) <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">as</span> "ColumnName" <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"><br />
FROM</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">table</span><br /><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">where</span> predicates;</span></pre><b><br />
Explanation:<br /></b>This is another one of my bone head mistakes. It's such a simple little thing
I overlook, and gets me so disappointed in myself when I realize I spent time trying
to figure this out. It's generally followed by me looking over my shoulder and seeing
if any of my peers saw it. I'm mostly blogging this in hopes I won't make a silly
mistake like this again<b>. 
<br /><br /></b>Why is it always the baby errors that hang me up the most? It can never be something
deep in the CLR, or possibly find a mistake in .Net. Nope, noway, not gonna happen
.Net is pretty solid...for the most part.<b><br /></b><p></p><img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=045e6f8a-b43f-4c79-8d7f-545b9eb95304" /></body>
      <title>System.IndexOutOfRangeException: with a datareader</title>
      <guid isPermaLink="false">http://bencoffman.com/blog/PermaLink,guid,045e6f8a-b43f-4c79-8d7f-545b9eb95304.aspx</guid>
      <link>http://bencoffman.com/blog/2010/07/14/SystemIndexOutOfRangeExceptionWithADatareader.aspx</link>
      <pubDate>Wed, 14 Jul 2010 04:14:10 GMT</pubDate>
      <description>&lt;img src="http://bencoffman.com/blog/content/binary/microsoft_.net_logo.png" height="75px" width="120px" border="0"&gt; 
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;
&lt;br&gt;
&lt;br&gt;
Error Readout:&lt;br&gt;
&lt;/b&gt;System.IndexOutOfRangeException:&lt;b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/b&gt;&lt;b&gt;Fix:&lt;br&gt;
&lt;/b&gt;I encountered the above error when I tried to run the query, 
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;SELECT&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;MAX&lt;/span&gt;(expression
)&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;FROM&lt;/span&gt; tables&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;WHERE&lt;/span&gt; predicates;&lt;/span&gt;
&lt;br&gt;
&lt;/pre&gt;
and then loop through the results using a datareader with code like this,&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;while&lt;/span&gt;(rdr.Read())&lt;br&gt;
{&lt;br&gt;
someLabel.Items.Add(rdr[&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"ColumnName"&lt;/span&gt;].ToString());&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;I got the error above, because when you do a "max" sql query it doesn't
return any column header. 
&lt;br&gt;
&lt;br&gt;
All you need to do is give the sql result set a column header.&lt;br&gt;
&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;SELECT&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;MAX&lt;/span&gt;(expression) &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; "ColumnName" &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
FROM&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;table&lt;/span&gt; 
&lt;br&gt;
&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;where&lt;/span&gt; predicates;&lt;/span&gt;&lt;/pre&gt;&lt;b&gt;
&lt;br&gt;
Explanation:&lt;br&gt;
&lt;/b&gt;This is another one of my bone head mistakes. It's such a simple little thing
I overlook, and gets me so disappointed in myself when I realize I spent time trying
to figure this out. It's generally followed by me looking over my shoulder and seeing
if any of my peers saw it. I'm mostly blogging this in hopes I won't make a silly
mistake like this again&lt;b&gt;. 
&lt;br&gt;
&lt;br&gt;
&lt;/b&gt;Why is it always the baby errors that hang me up the most? It can never be something
deep in the CLR, or possibly find a mistake in .Net. Nope, noway, not gonna happen
.Net is pretty solid...for the most part.&lt;b&gt;
&lt;br&gt;
&lt;/b&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=045e6f8a-b43f-4c79-8d7f-545b9eb95304" /&gt;</description>
      <comments>http://bencoffman.com/blog/CommentView,guid,045e6f8a-b43f-4c79-8d7f-545b9eb95304.aspx</comments>
      <category>.Net</category>
      <category>SQL</category>
    </item>
    <item>
      <trackback:ping>http://bencoffman.com/blog/Trackback.aspx?guid=0d2ec25e-74da-411e-9530-f9969dbee498</trackback:ping>
      <pingback:server>http://bencoffman.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://bencoffman.com/blog/PermaLink,guid,0d2ec25e-74da-411e-9530-f9969dbee498.aspx</pingback:target>
      <dc:creator>Ben Coffman</dc:creator>
      <wfw:comment>http://bencoffman.com/blog/CommentView,guid,0d2ec25e-74da-411e-9530-f9969dbee498.aspx</wfw:comment>
      <wfw:commentRss>http://bencoffman.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=0d2ec25e-74da-411e-9530-f9969dbee498</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img src="http://bencoffman.com/blog/content/binary/database1.PNG" border="0" />
        <b>
          <br />
          <br />
Error Readout:</b>
        <br />
Incorrect syntax near ')'<br /><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="color: rgb(206, 226, 234); font-family: arial,sans-serif; font-size: 13px; line-height: 15px; white-space: pre-wrap;"></span></span><br /><b>The Problem:</b><br />
When I was trying to pass in <i>getdate()</i> as a parameter to execute a stored procedure
i got the above error.<br /><br /><b><i>Example:</i></b><br />
exec dbo.storedProcedure<br />
    @param1 = '8D8DF077-7491-491B-AFA2-8F088821A073',<br />
    @param2 = '8D8DF077-7491-491B-AFA2-8F088821A073',<br />
    @param3 = 'Market: SAP 4 Eva',<br />
    @param4 = getdate()<br /><br /><br /><b>The Solution:</b><br />
A sql execute statement can't pass a function such as <i>getdate()</i> in through
a parameter. To fix this either: 
<br /><br /><ol><li>
Put <i>getdate() </i>directly in your sql statement that resides in the stored procedure 
</li><li>
Pass a <i>null </i>if your table definition allows you to.</li><li>
Create a temp variable store the value of <i>getdate() </i>in that variable and pass
in that variable through the datetime parameter.<br /></li></ol><p></p><img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=0d2ec25e-74da-411e-9530-f9969dbee498" /></body>
      <title>Incorrect syntax near ')'.</title>
      <guid isPermaLink="false">http://bencoffman.com/blog/PermaLink,guid,0d2ec25e-74da-411e-9530-f9969dbee498.aspx</guid>
      <link>http://bencoffman.com/blog/2010/06/23/IncorrectSyntaxNear.aspx</link>
      <pubDate>Wed, 23 Jun 2010 17:41:51 GMT</pubDate>
      <description>&lt;img src="http://bencoffman.com/blog/content/binary/database1.PNG" border="0"&gt; &lt;b&gt;
&lt;br&gt;
&lt;br&gt;
Error Readout:&lt;/b&gt;
&lt;br&gt;
Incorrect syntax near ')'&lt;br&gt;
&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: rgb(206, 226, 234); font-family: arial,sans-serif; font-size: 13px; line-height: 15px; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br&gt;
&lt;b&gt;The Problem:&lt;/b&gt;
&lt;br&gt;
When I was trying to pass in &lt;i&gt;getdate()&lt;/i&gt; as a parameter to execute a stored procedure
i got the above error.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;&lt;i&gt;Example:&lt;/i&gt;&lt;/b&gt;
&lt;br&gt;
exec dbo.storedProcedure&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @param1 = '8D8DF077-7491-491B-AFA2-8F088821A073',&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @param2 = '8D8DF077-7491-491B-AFA2-8F088821A073',&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @param3 = 'Market: SAP 4 Eva',&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @param4 = getdate()&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;The Solution:&lt;/b&gt;
&lt;br&gt;
A sql execute statement can't pass a function such as &lt;i&gt;getdate()&lt;/i&gt; in through
a parameter. To fix this either: 
&lt;br&gt;
&lt;br&gt;
&lt;ol&gt;
&lt;li&gt;
Put &lt;i&gt;getdate() &lt;/i&gt;directly in your sql statement that resides in the stored procedure 
&lt;/li&gt;
&lt;li&gt;
Pass a &lt;i&gt;null &lt;/i&gt;if your table definition allows you to.&lt;/li&gt;
&lt;li&gt;
Create a temp variable store the value of &lt;i&gt;getdate() &lt;/i&gt;in that variable and pass
in that variable through the datetime parameter.&lt;br&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=0d2ec25e-74da-411e-9530-f9969dbee498" /&gt;</description>
      <comments>http://bencoffman.com/blog/CommentView,guid,0d2ec25e-74da-411e-9530-f9969dbee498.aspx</comments>
      <category>SQL</category>
    </item>
  </channel>
</rss>