Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Ways to resolve input string was not in a correct format in c# errors
Answered
Léo Collet
  • 32
Léo Collet
Asked: May 11, 20222022-05-11T19:01:10+00:00 2022-05-11T19:01:10+00:00In: S

Ways to resolve input string was not in a correct format in c# errors

  • 32

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:


namespace MyCalculator
 {
  public partial class MyForm : Form
  {
 
  int n1, n2, n3;
  String myResult;
 
  public MyForm()
  {
  InitializeComponent();
  n1 = Int32.Parse(inputField1.Text);
  n2 = Int32.Parse(inputField2.Text);
  }
 
  private void addBtn_Click(object sender, EventArgs e)
  {
  addFunc();
  calResult();
  }
 
  private void subsBtn_Click(object sender, EventArgs e)
  {
  subsFunct();
  calculateResult();
  }
 
  private void clrBtn_Click(object sender, EventArgs e)
  {
  clear();
  }
 
  private void addFunc()
  {
  n3 = n1 + n2;
  myResult = Convert.ToString(n3);
  }
 
  private void subsFunc()
  {
  n3 = n1 - n2;
  myResult = Convert.ToString(n3);
  }
 
  private void calResult()
  {
  lbl1.Text = myResult;
  }
 
  private void clear()
  {
  lbl1.Text = "";
  inputField1.Text = "";
  inputField2.Text = "";
  }
 }

but in my program, I am getting the warning:

Input string was not in a correct format

Can someone explain to me why the issue “the input string was not in a correct format in c#” happened? Where have I gone wrong? Thank you!

exception
  • 3 3 Answers
  • 82 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    paully
    2022-05-27T03:11:06+00:00Added an answer on May 27, 2022 at 3:11 am

    You’re trying to convert your Integer value to String but you did not do it properly. Therefore, the Integer value passed to the parse/convert function might not contain a real integer value.

    Two things you could do to improve your code:

    1. Validate and parse your inputFields when the user clicks on it. You should use ‘Int.TryParse’ rather than ‘Convert’ or ‘Int32.Parse’
    2. Better handle the thrown exceptions
    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Léa Maillard
    2022-05-25T19:28:25+00:00Added an answer on May 25, 2022 at 7:28 pm

    This was the exact exception I encountered, but it had nothing to do avec parsing numerical inputs. This is not the answer to the OP’s query, but it is acceptable to share the knowledge.

    I had declared a string, and was formatting it with JQTree. This requires curly braces ().. To be considered a properly formatted string, you must use doubled curly braces.

    string measurements = string.empty;
    measurements += string.Format(@"
     {{label: 'Measurement Name: {0}',
     children: [
     {{label: 'Measured Value: {1}'}},
     {{label: 'Min: {2}'}},
     {{label: 'Max: {3}'}},
     {{label: 'Measured String: {4}'}},
     {{label: 'Expected String: {5}'}},
     ]
     }},",
     drv["MeasurementName"] == null ? "NULL" : drv["MeasurementName"],
     drv["MeasuredValue"] == null ? "NULL" : drv["MeasuredValue"],
     drv["Min"] == null ? "NULL" : drv["Min"],
     drv["Max"] == null ? "NULL" : drv["Max"],
     drv["MeasuredString"] == null ? "NULL" : drv["MeasuredString"],
     drv["ExpectedString"] == null ? "NULL" : drv["ExpectedString"]);

    This will hopefully help others who are trying to answer this question, but don’t know how to interpret numerical data.

    • 1
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. ferneparker
    2022-07-12T06:52:16+00:00Added an answer on July 12, 2022 at 6:52 am

    This error indicates that the string from which you are trying to extract an integer is invalid.

    It is unlikely that the text boxes contain a valid integer right away when the form is created. This is why you are getting the integer values. It would be much easier to update n1 or n2 using the button click events, in the same manner as you do in the constructor. You might also want to check out the method. It’s easier to use, even if the string doesn’t actually contain an integer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.