Syntax Question: Reading from MySQL database

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.
KellyJensenAsked:
Who is Participating?
 
AlexFMCommented:
int n = Int32::Parse(reader->Item[S"age"]->ToString());
0
 
KellyJensenAuthor Commented:
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?
0
 
AlexFMCommented:
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;

try
{
    n = Int32::Parse(reader->Item[S"age"]->ToString());
}
catch ( System.FormatException ex )
{
    // handle error here - for example, show message, set n to 0 etc.
}
0
 
KellyJensenAuthor Commented:
Thank you so very much, there was a glitch in my database. Your answer was very correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.