Solved

Check is datareader has rows in .net 2.0 c#

Posted on 2007-04-02
3
1,645 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 142

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 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now