Link to home
Start Free TrialLog in
Avatar of gilllyo
gilllyo

asked on

DataReader has rows but no Data?

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();
Avatar of t_itanium
t_itanium
Flag of United Arab Emirates image

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
Avatar of MuhammadAdil
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
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.
ASKER CERTIFIED SOLUTION
Avatar of pradeepsudharsan
pradeepsudharsan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gilllyo
gilllyo

ASKER

Thank you...