?
Solved

Check is datareader has rows in .net 2.0 c#

Posted on 2007-04-02
3
Medium Priority
?
1,683 Views
Last Modified: 2013-11-07
       public static CustomerListGetList()
        {
            CustomerList tempList = null;
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand("spTest");
            dbCommand.CommandType = CommandType.StoredProcedure;

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {  
                  //how can i check if the datareader has rows here?

                tempList = new CustomerList();
                while (dataReader.Read())
                {
                    tempList.Add(FillDataRecord(dataReader));
                }                
                dataReader.Close();
            }
            return tempList;
        }
0
Comment
Question by:mugsey
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18837716
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {  
                  //how can i check if the datareader has rows here?
                 if (dataReader.HasRows())
 . ...

0
 

Author Comment

by:mugsey
ID: 18838508
it does not have an option for this
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 18838993
I see, you used the DataFactory's that returns a IDataReader that does not expose that property


 public static CustomerListGetList()
        {
            CustomerList tempList = null;
            int intRows = 0;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand("spTest");
            dbCommand.CommandType = CommandType.StoredProcedure;

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {  
                tempList = new CustomerList();

                while (dataReader.Read())
                {
                    intRows ++;
                    tempList.Add(FillDataRecord(dataReader));
                }                
                dataReader.Close();
            }

            //check if the datareader has had rows here?
            if (intRows == 0)
            { // had no rows ... }

            return tempList;
        }
Authored by: mugsey
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question