?
Solved

DataReader has rows but no Data?

Posted on 2006-06-22
5
Medium Priority
?
553 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 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