954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error in reading DataReader

Hi

I want to read the content in datareader.So I use below coding to do that
if (dataReader4.IsDBNull(0)){

}
but I run above coding it gave me an error meassage.The error Meassage is
"Invalid attempt to read when no data is present".

then I use below coding
dataReader4["Email"]==null
but It also gave me above error meassage.There is no record for that field,that only for one record,for othere records there are email address.

Thanks
Tank50

Tank50
Junior Poster
124 posts since Aug 2008
Reputation Points: 12
Solved Threads: 1
 

You missed Read() method.

if (dataReader4.Read())
   var=dataReader4.IsDBNull(0);
}
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Hi,
Read example here . It will help you. You have to call dataReader4.Read() before reading it. If still problem persist please post code what you have done so far.

DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

Hi

I want to read the content in datareader.So I use below coding to do that if (dataReader4.IsDBNull(0)){

} but I run above coding it gave me an error meassage.The error Meassage is "Invalid attempt to read when no data is present".

then I use below coding dataReader4["Email"]==null but It also gave me above error meassage.There is no record for that field,that only for one record,for othere records there are email address.

Thanks Tank50

//****************************************************
Hi,
try this code to read datareader...// Instance a new object
Object a = new Object();

// Check if datareader is null
if (datareader == null)
return;

// Get data from datareader
while (datareader.Read())
{
// Code example...
a = datareader.GetValue(0);
}

Hope it works for you...
:idea: See ya!!!

maestridaniele
Newbie Poster
1 post since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

It looks like you start using the reader without reading some data first.

void ReadData(SqlConnection conn, string sql)
SqlDataReader datareader;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
datareader = cmd.ExecuteReader();
while (datareader.Read()) {
 // perform your code here
}
}


Cheers,
Dennis

djjaries
Newbie Poster
6 posts since Mar 2008
Reputation Points: 10
Solved Threads: 2
 

Hi

Thanks guys.I was in greate trouble coz of this error meassage.Now it ok ,I miss datareader.Read() part.

:)
Tank50

Tank50
Junior Poster
124 posts since Aug 2008
Reputation Points: 12
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You