Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-07
5
Medium Priority
?
78 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 2000 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 83

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 83

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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