?
Solved

DataReader has rows but no Data?

Posted on 2006-06-22
5
Medium Priority
?
500 Views
Last Modified: 2008-02-01
I'm using an MS access database to store data for a program but when I go and try to receive data from the database via the code snippet below. When it get's to reading the data (in the line with the "*No Data*" label the following exception is thrown "No data exists for the row/column." with a source of "System.Data"

How can that happen when the HasRows'  property checks to see if there is data there or not?

OleDbCommand cmd = new OleDbCommand("SELECT * FROM Country WHERE NationID ='"+name+"';", conn);
                OleDbDataReader data = cmd.ExecuteReader();
               
                if (data.HasRows)
                {
     
                    numAirforce.Value = data.GetDecimal(3); *No data*
                    .....
               }
               data.Close();
               conn.Close();
0
Comment
Question by:gilllyo
[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
5 Comments
 
LVL 5

Expert Comment

by:t_itanium
ID: 16966496
is the cmd executing properly??? try to use break points and f11 to trace the code......

some times writing sql command in such way raise an error

correction..

'" + name + "' have a space before and after the +'s

cheers
0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 16966523
Hi Dear,

I think there is no need of ; and
          "SELECT * FROM Country WHERE NationID ='"+name+"'"
if (data.HasRows)
                {
                   data.Read();
                    numAirforce.Value = data.GetDecimal(3); *No data* // is this column can convert to decimal
                    .....
               }

Regards

Adil
0
 
LVL 11

Expert Comment

by:Agarici
ID: 16966715
when calling data.getdecimal no conversion is made so the column must be of type decimal

try using getfloat or getdouble according to your data model

hth,
A.
0
 
LVL 7

Accepted Solution

by:
pradeepsudharsan earned 375 total points
ID: 16967331
Hi,
try like this
if (data.HasRows)
{
    while (data.Read())
     {
      numAirforce.Value = data.GetDecimal(3);
      }
}
else
{
   //do nothing
}

Convert the data.GetDecimal(3) value to  numAirforce's data type

regards
Pradeep
0
 

Author Comment

by:gilllyo
ID: 16968301
Thank you...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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