CSharp

Hi Guys,

I have some windows form application that I built for testing and learning.

The application looks like:
app
The purpose is to calculate between Height and weight which actually work fine.  the issue I get is when user doesn't type anythingin the textbox fields he gets an exception {"Input string was not in a correct format."}.

Here is my code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnshow_Click(object sender, EventArgs e)
        {  
                string Name = textname.Text;
                int Height = Convert.ToInt32(textheight.Text);
                double Weight = Convert.ToDouble(textweight.Text);
            

                BMI obj1 = new BMI(Name, Height,Weight);
                {
                    MessageBox.Show
                        ("Name: " + obj1.getName() + Environment.NewLine + "Height: " + obj1.getHights() + Environment.NewLine + "Weight: " + obj1.getWeight() + Environment.NewLine + "Calculation: " + obj1.CalculateBMI());
                }
         



        }

Open in new window

LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
private void btnshow_Click(object sender, EventArgs e)
        {  
                string Name = textname.Text;
                if(!String.IsNullOrEmpty(Name))
{
.....
}
0
p_davisCommented:
just need to check  for empty  or null string, i would think
0
Moti Mashiah.NET DeveloperAuthor Commented:
Yes, I know but how to do it?

I'm very new and just learning.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Moti Mashiah.NET DeveloperAuthor Commented:
K,

I did something like this:
and now I'm getting error on the "int Height" {"Input string was not in a correct format."}
 
            string Name = textname.Text;
            if (!string.IsNullOrEmpty(Name))
            {
                MessageBox.Show("Enter Name");
            }
            int Height = Convert.ToInt32(textheight.Text);
            double Weight = Convert.ToDouble(textweight.Text);
0
p_davisCommented:
Just add a return; if within the null empty check
0
p_davisCommented:
wait... one minute -- i was using my phone... now at my computer.
0
p_davisCommented:
i was using ! ( the not symbol for c) to check if the string want' null or empty then continue with using it..... you have it as if isn't null or empty then give a message.... if you are going to use it in the way of if it is null or empty then provide user input you need to take away the !


otherwise, i think this is what you might want

private void btnshow_Click(object sender, EventArgs e)
        {  
                string Name = textname.Text;
if(!string.IsNullOrEmpty(Name)
{
                int Height = Convert.ToInt32(textheight.Text);
                double Weight = Convert.ToDouble(textweight.Text);
            

                BMI obj1 = new BMI(Name, Height,Weight);
                {
                    MessageBox.Show
                        ("Name: " + obj1.getName() + Environment.NewLine + "Height: " + obj1.getHights() + Environment.NewLine + "Weight: " + obj1.getWeight() + Environment.NewLine + "Calculation: " + obj1.CalculateBMI());
                }
}
else
{
    MessageBox.Show("Enter Name");
}



        }

Open in new window

0
Moti Mashiah.NET DeveloperAuthor Commented:
k, that works when all my fields are empty, but what if user insert Name but doesn't insert the other to fields "Height" and "Weight"?

it is going to fail again. like it happens to me now.
0
Moti Mashiah.NET DeveloperAuthor Commented:
Please, review the code and see what I mean.
If you insert to textname and leave the other fields empty, you are going to get an exception.

private void btnshow_Click(object sender, EventArgs e)
        {

            string Name = textname.Text;
            if (!string.IsNullOrEmpty(Name))
            {
                int Height = Convert.ToInt32(textheight.Text);
                double Weight = Convert.ToDouble(textweight.Text);

                BMI obj1 = new BMI(Name, Height, Weight);
                MessageBox.Show
                    ("Name: " + obj1.getName()
                    + Environment.NewLine
                    + "Height: "
                    + obj1.getHights()
                    + Environment.NewLine
                    + "Weight: "
                    + obj1.getWeight()
                    + Environment.NewLine
                    + "Calculation: "
                    + obj1.CalculateBMI());

            }

            else
            {
                MessageBox.Show("Please, you have to insert details in all fields");
            }

Open in new window

0
Franck GaspozSoftware Architect, Technical ExpertCommented:
Hi,
you can use TryParse methods which avoids to have to test exceptions, by this way :

int Height;
double Weight;
if ( Int32.TryParse(textheight.Text,out Height) && Double.TryParse(textweight.Text,out Weight)) {
  // both strings are valid, variables Height and Weight are initialized
  BMI obj1 = ...
} else
  // eventually display a message here
 ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Moti Mashiah.NET DeveloperAuthor Commented:
Solved my issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.