Re: Invalid attempt to read when no data is present
From: Kevin Spencer (kevin_at_DIESPAMMERSDIEtakempis.com)
Date: 02/24/05
- Next message: Brian Ciarcia: "Re: Loop through a Plain Radio Button in ASP.NET"
- Previous message: markfoong_at_yahoo.com: "ASP.NET interacts with Flash"
- In reply to: Andy Sutorius: "Re: Invalid attempt to read when no data is present"
- Next in thread: Andy Sutorius: "Re: Invalid attempt to read when no data is present"
- Reply: Andy Sutorius: "Re: Invalid attempt to read when no data is present"
- Messages sorted by: [ date ] [ thread ]
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
>> >
>> >
>>
>>
>
>
- Next message: Brian Ciarcia: "Re: Loop through a Plain Radio Button in ASP.NET"
- Previous message: markfoong_at_yahoo.com: "ASP.NET interacts with Flash"
- In reply to: Andy Sutorius: "Re: Invalid attempt to read when no data is present"
- Next in thread: Andy Sutorius: "Re: Invalid attempt to read when no data is present"
- Reply: Andy Sutorius: "Re: Invalid attempt to read when no data is present"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|