Syntax Question: Reading from MySQL database

Posted on 2004-11-01
Last Modified: 2010-04-24
I working with a MYSQL database, using C++ .NET. The program compiles, and outputs data to the console, using a line such as this:

Console::WriteLine("{0}", reader->Item[S"age"]);

However, I want to do more than just display the "age" to the console, I want to do calculations on the data. So, how do I convert the infomation to an Integer? I've tried variations of "Int32::Parse", ".ToString", "Int32::ToInt32" etc., but can't get the right syntax.
Please help. Thank You.
Question by:KellyJensen
    LVL 48

    Accepted Solution

    int n = Int32::Parse(reader->Item[S"age"]->ToString());

    Author Comment

    AlexFM, thank you for your answer.  It compiles and runs, but after an incredible amount of grinding away, this error message pops up:

    Microsoft Development Environment
    An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll.
    Additional information: Input string was not in correct format.

    Any other suggestions?
    LVL 48

    Expert Comment

    Possibly in most records database field contains valid number in the age field. But some records may contain, for example NULL or other invalid value - you can see this in your current program version. You need to handle exception:

    int n;

        n = Int32::Parse(reader->Item[S"age"]->ToString());
    catch ( System.FormatException ex )
        // handle error here - for example, show message, set n to 0 etc.

    Author Comment

    Thank you so very much, there was a glitch in my database. Your answer was very correct.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Suggested Solutions

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles a…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    846 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

    7 Experts available now in Live!

    Get 1:1 Help Now