Solved

Check is datareader has rows in .net 2.0 c#

Posted on 2007-04-02
3
1,669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

726 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