Re: Invalid attempt to read when no data is present

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


Date: Thu, 24 Feb 2005 13:43:08 -0500


> 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 with Delete Command
    ... The SQL could get fairly messy if you need to construct it in code, ... ContactID, and WebComID, and create your on-the-fly SQL on that saved query, ... to find the list of ContactIDs from the junction table, ... This is a style/readability thing: if you are going to use string ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Need help with Code Please!!!
    ... posted in response to my last post was some SQL, but this is not the SQL that ... the actual string that gets built at the end of the SQ1 build process. ... The message says Syntax error in query expression ... Dim rs As Recordset 'object ref to qryCompany\USFNumber ...
    (microsoft.public.access.formscoding)
  • RE: Web Part and Access database
    ... I dont know if it is possible to connect to access,but you can download SQL ... Server Error in '/Webparts' Application. ... The connection string specifies a local Sql Server Express instance ... String user, String password, Boolean trusted, String connectionString) ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: INDEXES: BTRIEVE vs EXTFH (cobol)
    ... Bill Bach wrote: ... In SQL, you can specify just about anything, but at the lower MKDE ... the engine has to pick the right Btrieve ... When COBOL needs to find this value, it knows that the key is a string. ...
    (comp.databases.btrieve)
  • Re: INDEXES: BTRIEVE vs EXTFH (cobol)
    ... In SQL, you can specify just about anything, but at the lower MKDE ... the engine has to pick the right Btrieve ... When COBOL needs to find this value, it knows that the key is a string. ...
    (comp.databases.btrieve)