Re: Invalid attempt to read when no data is present

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Kevin Spencer (kevin_at_DIESPAMMERSDIEtakempis.com)
Date: 02/24/05


Date: Thu, 24 Feb 2005 14:43:44 -0500

Hi Andy,

Odd that you can debug with the code you posted. After reading your reply I
looked more closely (sorry I didn't in the first place) and noticed an error
that should have thrown an exception:

SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString);

This line should throw an exception because there is no constructor for a
SqlDataReader that takes 2 strings as parameters. There are 4 overloads for
the constructor:

public SqlCommand() // parameterless
public SqlCommand(string) // 1 string parameter
(query)
public SqlCommand(string, SqlConnection) // 1 string parameter (query), 1
SqlConnection parameter
public SqlCommand(string, SqlConnection, SqlTransaction) // see above, plus
1 SqlTransaction

In any case, without a Connection, there is no data.

-- 
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.
"Andy Sutorius" <andy@sutorius.com> wrote in message 
news:6zpTd.1537$ha.28979@twister.southeast.rr.com...
> Kevin,
>
> When I placed your snippet in my code I decided to step through in debug. 
> As
> soon as I F11 off of the if statement I get the same error. It's failing
> when testing for null. Any recommendations?
>
> Thanks,
>
> Andy
>
>
> "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
> news:uGLHwCqGFHA.3092@tk2msftngp13.phx.gbl...
>> >   string strSubject = Convert.ToString(dtrEmail["subject"]);
>> >   string strContent = Convert.ToString(dtrEmail["content"]);
>>
>> Are you checking for NULL values before attempting to convert them to
>> strings? A NULL cannot be converted to a string. Example:
>>
>> string strSubject;
>> int ordinal;
>> ordinal = dtrEmail.GetOrdinal("subject");
>> if (!dtrEmail.IsDbNull(ordinal)
>>     strSubject = dtrEmail.GetString(ordinal);
>> else
>>     strSubject = "";
>>
>> -- 
>> HTH,
>>
>> Kevin Spencer
>> Microsoft MVP
>> .Net Developer
>> Neither a follower nor a lender be.
>>
>> "Andy Sutorius" <andy@sutorius.com> wrote in message
>> news:GCoTd.1391$ha.23643@twister.southeast.rr.com...
>> > Hi,
>> >
>> > I am getting the error "Invalid attempt to read when no data is 
>> > present"
>> > when I run the following code. I have checked the sql and it is
> returning
>> > data. What am I missing?
>> >
>> >   string sqlGetEmail = "SELECT content, subject FROM tblContent WHERE 
>> > id
> =
>> > " + Convert.ToInt16(ddlChooseEmail.SelectedValue.ToString());
>> >   SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString);
>> >   SqlDataReader dtrEmail = cmdGetEmail.ExecuteReader();
>> >
>> >   string strSubject = Convert.ToString(dtrEmail["subject"]);
>> >   string strContent = Convert.ToString(dtrEmail["content"]);
>> >
>> >   dtrEmail.Close();
>> >
>> >
>> > Thanks,
>> >
>> > Andy
>> >
>> >
>>
>>
>
> 


Relevant Pages

  • Re: Problems piping input into program
    ... get the strings as args list in mainfunction, ... your program doesn't do anything redirecting ... you should first write interactive program with stdin as input, ... debug it interactively, then test it with redirct from coonsole window. ...
    (microsoft.public.vc.ide_general)
  • Any more ideas? -- RE: How to debug binary-only modules and binaries
    ... Thanks for your reply, but I've tried strings, objdump, gdb and several ... --Guolin Cheng ... How to debug binary-only modules and binaries ...
    (Fedora)
  • Re: How does ASP.NET find code-behind DLLs
    ... of code (Debug and Release) for 1 app? ... > "Kevin Spencer" wrote in message ... >> use Visual Studio for deployment; it's really too simple to need it. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: visual studio .net
    ... I was able to debug until I added a login page and another aspx page. ... "Kevin Spencer" wrote: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Deugging release build of MFC app
    ... I was hoping someone could shed some light on what's required to debug a release build of an MFC application in Visual Studio 2003. ... I see that the default configuration for Release builds includes debug symbols, but when I step through the code, the values I see in the watch window don't jive with their "actual" values. ... Another aid is to format strings and send them to OutputDebugString instead of to a message box. ... The strings appear in the debugger output window without stopping execution flow. ...
    (microsoft.public.vc.mfc)