Hi all,
I have a textbox which i wrote Convert.toint32(txtbox.text) which takes phone no.
Suppose i want to pass blank textbox it throws an error that input string is not in correct format.
i have a requirement that the textbox can be blank.
hope i may get a solution.
Thanks in advance
Vijay
Textbox value to int
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 28, 2012, 6:41 PM EST
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Insert Phone no");
}
else
{
label1.Text = Convert.ToInt32(textBox1.Text).ToString();
}
}
Thanks
SenthilkumarRank #387 · 6.5K reputationPosted Feb 28, 2012, 8:43 PM EST
int i;
bool result = int.TryParse(textBox1.Text, out i);
if (result)
{
i = Convert.ToInt32(textBox1.Text);
}
Because the value can be null or string or alphanumric.
If it is useful then mark it as accepted answer.
VulpesRank #55 · 29.2K reputationPosted Feb 28, 2012, 11:51 PM EST
If the phone number is blank or invalid, you could then set this variable to null:
string t = textBox1.Text.Trim();
int? phoneNumber = null;
if (t != "")
{
int temp;
if(int.TryParse(t, out temp)) phoneNumber = temp;
}