Solved

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Posted on 2016-10-07
5
73 Views
Last Modified: 2016-10-07
private void TxtMarks1030_TextChanged(object sender, EventArgs e)
        {

            Int32 marks1030 = Convert.ToInt32(TxtMarks1030.Text);

            if (marks1030 >= 101)
            {
                LblGrade1030.Text = "Please enter less than 100";
            }
            else if (marks1030 >= 90)
            {
                LblGrade1030.Text = "A";
            }
            else if (marks1030 >= 80)
            {
                LblGrade1030.Text = "B";
            }
            else if (marks1030 >= 65)
            {
                LblGrade1030.Text = "C";
            }
            else if (marks1030 >= 50)
            {
                LblGrade1030.Text = "D";
            }
            else if (marks1030 >= 40)
            {
                LblGrade1030.Text = "E";
            }
            else
            {
                LblGrade1030.Text = "Fail";
            }
        }
0
Comment
Question by:the student
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 41834431
This exception occurs because the string being presented to convert is not convertable to an integer; e.g -
using System;

namespace EE_Q28975134
{
	class Program
	{
		static string grade1 = "90";
		static string grade2 = "I am not the number 90";

		static void Main(string[] args)
		{
			GetGrade(grade1);
			Console.WriteLine();
			GetGrade(grade2);
			Console.ReadLine();
		}

		private static void GetGrade(string value)
		{
			try 
			{
				int grade = Convert.ToInt32(value);
				if (grade > 100)
					Console.WriteLine("Please enter a grade less than 100.");
				else
					Console.WriteLine("Your test score - [{0}] produces a grade of: {1}", value, grade >= 90 ? "A" : grade >= 80 ? "B" : grade >= 65 ? "C" : grade >= 50 ? "D" : grade >= 40 ? "D" : "F");
			}
			catch (Exception ex)
			{
				Console.WriteLine("The value entered cannot convert to an integer, grade cannot be calculated.  {0}", ex.Message);
			}
		}
	}
}

Open in new window

Produces the following output -Capture.JPG
To correct you can use a try...catch block as I demonstrated:
private void TxtMarks1030_TextChanged(object sender, EventArgs e)
{
	try
	{
		Int32 marks1030 = Convert.ToInt32(TxtMarks1030.Text);
		if (marks1030 >= 101)
			LblGrade1030.Text = "Please enter less than 100";
		else if (marks1030 >= 90)
			LblGrade1030.Text = "A";
		else if (marks1030 >= 80)
			LblGrade1030.Text = "B";
		else if (marks1030 >= 65)
			LblGrade1030.Text = "C";
		else if (marks1030 >= 50)
			LblGrade1030.Text = "D";
		else if (marks1030 >= 40)
			LblGrade1030.Text = "E";
		else
			LblGrade1030.Text = "Fail";
	}
	catch (Exception)
	{
		LblGrade1030.Text = "Cannot convert grade";
	}
}

Open in new window

Or you could use a different method to convert your field's value, a method that does not throw an exception.  Something like Int32.TryParse.  Since it returns a boolean value that siginifies the conversion was successful, you would change your method to do this:
private void TxtMarks1030_TextChanged(object sender, EventArgs e)
{
	Int32 marks1030;
	if(Int32.TryParse(TxtMarks1030.Text, out marks1030))
	{
		if (marks1030 >= 101)
			LblGrade1030.Text = "Please enter less than 100";
		else if (marks1030 >= 90)
			LblGrade1030.Text = "A";
		else if (marks1030 >= 80)
			LblGrade1030.Text = "B";
		else if (marks1030 >= 65)
			LblGrade1030.Text = "C";
		else if (marks1030 >= 50)
			LblGrade1030.Text = "D";
		else if (marks1030 >= 40)
			LblGrade1030.Text = "E";
		else
			LblGrade1030.Text = "Fail";
	}
	else
	{
		LblGrade1030.Text = "Cannot convert grade";
	}
}

Open in new window


Proof of concept rewriting the above example:
using System;

namespace EE_Q28975134
{
	class Program
	{
		static string grade1 = "90";
		static string grade2 = "I am not the number 90";

		static void Main(string[] args)
		{
			GetGrade(grade1);
			Console.WriteLine();
			GetGrade(grade2);
			Console.ReadLine();
		}

		private static void GetGrade(string value)
		{
			int grade;
			if (int.TryParse(value, out grade))
			{
				if (grade > 100)
					Console.WriteLine("Please enter a grade less than 100.");
				else
					Console.WriteLine("Your test score - [{0}] produces a grade of: {1}", value, grade >= 90 ? "A" : grade >= 80 ? "B" : grade >= 65 ? "C" : grade >= 50 ? "D" : grade >= 40 ? "D" : "F");
			}
			else
			{
				Console.WriteLine("The value entered cannot convert to an integer, grade cannot be calculated.");
			}
		}
	}
}

Open in new window

Now produces a very similar output (minus the exception message ofcourse) -Capture.JPG
-saige-
0
 

Author Comment

by:the student
ID: 41834444
Thank you, it_saige.

It was very helpful now it's working fine. You made my day. This was the part of assignment. thank you again
0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 41834461
Test for null or empty string.and validated input.
 private void TxtMarks1030_TextChanged(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(TxtMarks1030.Text) == false)
            {
                try
                {
                    Int32 marks1030 = Convert.ToInt32(TxtMarks1030.Text);
                    if (marks1030 >= 101) { LblGrade1030.Text = "Please enter less than 100"; }
                    else if (marks1030 >= 90) { LblGrade1030.Text = "A"; }
                    else if (marks1030 >= 80) { LblGrade1030.Text = "B"; }
                    else if (marks1030 >= 65) { LblGrade1030.Text = "C"; }
                    else if (marks1030 >= 50) { LblGrade1030.Text = "D"; }
                    else if (marks1030 >= 40) { LblGrade1030.Text = "E"; }
                    else { LblGrade1030.Text = "Fail"; }
                }
                catch {
                    LblGrade1030.Text = "You must enter an integer  from 0 to 100";
                }
            }
        }

Open in new window


Yet another method that uses tryparse
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LblGrade1030.Text = "Enter an Integer from 0 to 100";
        }

        private void TxtMarks1030_TextChanged(object sender, EventArgs e)
        {
            Int32 marks1030;
            LblGrade1030.Text = "Enter an Integer from 0 to 100";
            if (int.TryParse(TxtMarks1030.Text, out marks1030))
            {

                if (marks1030 >= 101) { LblGrade1030.Text = "Please enter less than 100"; }
                else if (marks1030 >= 90) { LblGrade1030.Text = "A"; }
                else if (marks1030 >= 80) { LblGrade1030.Text = "B"; }
                else if (marks1030 >= 65) { LblGrade1030.Text = "C"; }
                else if (marks1030 >= 50) { LblGrade1030.Text = "D"; }
                else if (marks1030 >= 40) { LblGrade1030.Text = "E"; }
                else { LblGrade1030.Text = "Fail"; }
            }

        }
    }

}

Open in new window

0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 41834462
Should have refreshed the page first
2
 

Author Comment

by:the student
ID: 41834467
Thanks David Johnson
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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