Re: HasRows() vs. Read() and ERROR suppression

From: James Hokes (no_way_at_noemail.com)
Date: 03/01/05


Date: Tue, 1 Mar 2005 15:46:06 -0500

Scott,

I am aware of that fact at this point.

What I am really wondering, though, is what is the utility of HasRows()?
It seems worthless at first glance, but that doesn't sit right with me.

Thanks,
James Hokes

"Scott M." <s-mar@nospam.nospam> wrote in message
news:uzPKlopHFHA.2740@TK2MSFTNGP12.phx.gbl...
> That's because ExectueReader doesn't return any data, just an object that
> can look at the data.
>
> The Read method (when called for the first time) advances you to the first
> record found by executing the commandText. If you don't call Read, you
> are never attempting to process any data.
>
>
> "James Hokes" <no_way@noemail.com> wrote in message
> news:e%23BZFgpHFHA.3588@TK2MSFTNGP14.phx.gbl...
>> Scott,
>>
>> Specifically, it is a type conversion error, Level 16, State 1.
>>
>> Here's the info:
>> Server: Msg 241, Level 16, State 1, Procedure selADOTest, Line 7
>> Syntax error converting datetime from character string.
>>
>> However, as I stated in the post, the error does not fire if you
>> pre-emptively check .HasRows() and avoid calling .Read().
>> I would have thought the exception would be thrown upon .ExecuteReader()
>> on the Command object, but it does not. Apparently, for some reason you
>> MUST call .Read() on the SqlDataReader, or you don't get the exception.
>>
>> Baffling.
>>
>> I posted a repro script (both T-SQL and VB.Net) in the other part of the
>> thread where I'm conversing with Marina.
>>
>> Thanks,
>> James Hokes
>>
>>
>> "Scott M." <s-mar@nospam.nospam> wrote in message
>> news:eUyqMWoHFHA.1860@TK2MSFTNGP15.phx.gbl...
>>> What type of SQL exception are you running into?
>>>
>>> Have you tried:
>>>
>>> Try
>>> rdr = cmd.ExecuteReader
>>>
>>> If rdr.HasRows Then
>>> While rdr.Read
>>> 'Process row..
>>> Loop
>>> End If
>>> Catch ex as Exception
>>> 'examine exception
>>> End Try
>>>
>>> "James Hokes" <no_way@noemail.com> wrote in message
>>> news:%23bxEyRoHFHA.4060@TK2MSFTNGP14.phx.gbl...
>>>> Hi All,
>>>>
>>>> We're using the 1.1. Framework against SQL Server 2000, and are having
>>>> a strange issue where we don't get errors back from the stored
>>>> procedure, i.e. the exception never gets thrown.
>>>>
>>>>
>>>> We use SqlDataReader objects to stream stored procedure results back
>>>> when we have a smallrowsets.
>>>>
>>>> Normally, our code looks like so:
>>>>
>>>> '...set up connection and command...
>>>> rdr = cmd.ExecuteReader
>>>>
>>>> While rdr.Read
>>>> 'Process row...
>>>> Loop
>>>>
>>>> 'et cetera...
>>>>
>>>> In this case, the exception fires normally.
>>>>
>>>>
>>>> However, one of our developers tried:
>>>>
>>>> If rdr.HasRows Then
>>>> While rdr.Read
>>>> 'Process row..
>>>> Loop
>>>> End If
>>>>
>>>> This results in the total suppression of SQL Server errors that
>>>> occurred in the stored procedure.
>>>>
>>>> Why does this occur?
>>>> Of what use is HasRows() if it doesn't throw the exceptions that are
>>>> apparently waiting in the SqlDataReader stream?
>>>>
>>>> Are there any other "scenarios" we should be aware of which would
>>>> suppress the SQL Exceptions? This is scary.
>>>>
>>>>
>>>> Thanks,
>>>> James Hokes
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Relevant Pages

  • Re: HasRows() vs. Read() and ERROR suppression
    ... The Read method (when called for the first time) advances you to the first ... > Syntax error converting datetime from character string. ... > on the Command object, ... > MUST call .Readon the SqlDataReader, or you don't get the exception. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Structured Exception Handling (was: Try Finally...)
    ... >statements will be traversed outwards exactly once, executing each ... What really bite me is the re-raise of the exception ... and the continuation in statementList2. ... This means that during unwinding the code immediately returns, ...
    (comp.lang.pascal.delphi.misc)
  • Re: Why finally?
    ... I get the same results from executing the code as well. ... This seems to be a definite improvement of V1.x of the framework. ... normal exception paths. ... // Inject an asynchronous ThreadAbortException in the target thread, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DirectShow Insanity
    ... The exception happens immediately upon executing the ... > and what exactly is shown in the debug output window? ... > what happens if you wait for completion befor exiting the program? ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: SqlException after Adapter.Update: how to determine the source column and parameter involved?
    ... {"Syntax error converting the varchar value 'Test' to a column of data type int."} ... I think the exception may be spurious, for when I look at things in the Immediate Window, it all looks OK: ... The correct column is being updated with the value 'Test'. ... I don't see any parameters with incorrect SourceColumn mappings, and "addr1" the source column for parameteris not used more than once in the code where the parameters are instantiated. ...
    (microsoft.public.dotnet.framework.adonet)