Datareader, there is no data...

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

Arjen

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

PJ

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top