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

Dani AI

Generated

Short summary: the runtime error "Invalid attempt to read when no data is present" happens when code tries to access columns on a DataReader before any row has been advanced (or after the reader is closed). As , and pointed out, you must call Read() (or iterate) before accessing fields; confirms that fixed the original problem.

Few practical points that go beyond the earlier replies:

  • dataReader["Email"] == null is incorrect for database NULLs. A column that contains SQL NULL is returned as DBNull.Value, not C# null. Use IsDBNull or compare to DBNull.Value to detect it, then map to a nullable/C# value.
  • For performance and safety, get the column ordinal once (with GetOrdinal("Email")) and reuse it inside the loop. GetOrdinal throws if the column name is missing, which helps find typos early.
  • Use HasRows when you need to know whether the result set contains rows before calling Read() (it does not advance the reader).
  • Always dispose reader/command (use using) so connections close reliably.

Example pattern (conceptual; adjust for your provider):

using (var cmd = conn.CreateCommand()) {
    cmd.CommandText = "...";
    using (var rdr = cmd.ExecuteReader()) {
        if (!rdr.HasRows) return;
        int emailIdx = rdr.GetOrdinal("Email");
        while (rdr.Read()) {
            string email = rdr.IsDBNull(emailIdx) ? null : rdr.GetString(emailIdx);
            // process email
        }
    }
}

Troubleshooting checklist: confirm the SQL actually selects the Email column (no misspellings), ensure the connection is open and ExecuteReader() ran, call Read() before accessing fields, and check for DBNull.Value rather than null.

Recommended Answers

All 5 Replies

You missed Read() method.

if (dataReader4.Read())
   var=dataReader4.IsDBNull(0);
}

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.

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!!!

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

Hi

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

:)
Tank50

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.