Datareader, there is no data...

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hello,

How can I check the recordcount?

Here is a little bit of my code:
// Load first row into Datareader
dr.Read();

if (dr["Category"].ToString() == "xmlsrc") {

When I do this I get an error because there is no data... it only have to
check this dr["Category"].ToString() == "xmlsrc") if there is data.
How can I do that?

Thanks!
 
When I search for your best practice with google.com I don't find al lot of
material.

http://www.google.nl/search?hl=nl&ie=UTF-8&oe=UTF-8&q=ColumnEnum&lr=

What namespace do I have to use for ColumnEnum?

Thanks!



PJ said:
the .Read() method returns false if it has reached the end of it's record
stream so your code should be

if ( dr.Read() )
{
//perform actions on row
}

and...btw, use enums rather than strings to access your columns...it's
quicker

if ( (string)dr[Convert.ToInt32(ColumnEnum.Category)] == "xmlsrc" )
//...

Arjen said:
Hello,

How can I check the recordcount?

Here is a little bit of my code:
// Load first row into Datareader
dr.Read();

if (dr["Category"].ToString() == "xmlsrc") {

When I do this I get an error because there is no data... it only have to
check this dr["Category"].ToString() == "xmlsrc") if there is data.
How can I do that?

Thanks!
 
ColumnEnum was just an example of an enum you would create to access the
columns in your SqlDataReader

enum ColumnEnum
{
Id, Category, etc
}

this way you are accessing the columns by their int index, rather than their
name, which is obviously faster.

Arjen said:
When I search for your best practice with google.com I don't find al lot of
material.

http://www.google.nl/search?hl=nl&ie=UTF-8&oe=UTF-8&q=ColumnEnum&lr=

What namespace do I have to use for ColumnEnum?

Thanks!



PJ said:
the .Read() method returns false if it has reached the end of it's record
stream so your code should be

if ( dr.Read() )
{
//perform actions on row
}

and...btw, use enums rather than strings to access your columns...it's
quicker

if ( (string)dr[Convert.ToInt32(ColumnEnum.Category)] == "xmlsrc" )
//...

Arjen said:
Hello,

How can I check the recordcount?

Here is a little bit of my code:
// Load first row into Datareader
dr.Read();

if (dr["Category"].ToString() == "xmlsrc") {

When I do this I get an error because there is no data... it only have to
check this dr["Category"].ToString() == "xmlsrc") if there is data.
How can I do that?

Thanks!
 
Back
Top