int sum = int.Parse(TextBox5.Text) + int.Parse(TextBox6.Text) + int.Parse(TextBox7.Text);
Label1.Text = String.Format("{0}", sum);
when I enter all three textboxes it runs clearly but if i keep any one or two of them null it gives error...
input was not in correct formate ..
how can I solve this error.....
Srinubabu RavillaPosted May 31, 2013, 6:51 AM
Shankar MPosted Apr 11, 2013, 12:01 AM
int.TryParse Method tries to convert the given Input to a number returning a boolean (true/false) value.
If the return value is true - It is a valid Number
If the return equals to false - The Input is not a number , that is Invalid.
Please try this,
Regards,
Shankar M
Satyapriya NayakPosted Apr 10, 2013, 9:48 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Add
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
int i, j,k;
int.TryParse(textBox1.Text, out i);
int.TryParse(textBox2.Text, out j);
int.TryParse(textBox3.Text, out k);
int result = i + j + k;
label1.Text = result.ToString();
}
}
}
Gajendra YadavPosted Apr 10, 2013, 8:02 AM
Before sum do validation if textbox is not emty the add this.
or
You Use int.TryParse() method instead of int.Parse or convert.Toint32 method.
I hope it will help you
Shankar MPosted Apr 10, 2013, 7:52 AM
It is best Practise to use int.TryParse method.
Please view this link,
http://www.homeandlearn.co.uk/csharp/csharp_s3p9.html
Thanks & Regards,
Shankar M