Solved

DataReader has rows but no Data?

Posted on 2006-06-22
5
453 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
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 125 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

9 Experts available now in Live!

Get 1:1 Help Now