开发者

Input string was not in a correct format - Converting textbox text to Int32

    Postalcode = Convert.ToInt32(txtcity.Text);
    Mobileno = Convert.ToInt32(txmobileno.Text);
    Phoneno = Convert.ToInt32(txtphoneno.Text);

I am getting this error please any on开发者_如何学Pythone help


Are you sure your txtcity contains only numbers... You are trying to convert txtcity into numeric and store in postalcode...

Further if you want to check whether the text parsed is number use Int32.TryParse() method

The TryParse method will convert your string to int or will return false if not possible...

http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

An example is available here http://msdn.microsoft.com/en-us/library/f02979c7.aspx#Y1170


Thus means that one of your text box values failed to convert to a valid integer. You could try and use the following

int city = 0;
if(int.TryParse(txtcity.Text, out city))
{
  Postalcode = city;
}


How do you expect to convert a city to an integer? Also the phone and mobile numbers can have ( ) or -, so they can't be converted to integers.


Most likely your textbox is returning non-numeric values causing Convert.ToInt32 to fail. You may either use string variables PostalCode, Mobileno and Phoneno fields (which I recommend because these fields generally tend to contain alphanumeric characters like +91 or 123-123 or FA1203)

However, if you are too keen to use Integer only, then use Int32.TryParse so as to avoid specified run-time error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜