473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can't convert Text string to an int32 using Convert.Int32

Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code

int wmin, wsec, x
int hrs, min, sec, miles
miles = Convert.ToInt32(txtMiles.text)
hrs = Convert.ToInt32(txtHours.text)
min = Convert.ToInt32(txtMinutes.text)
sec = Convert.ToInt32(txtSeconds.text)
. .

I even tried forcing the values to string by using .... miles = Convert.ToInt32(txtMiles.text.ToString())
still not working...
Help!
Nov 16 '05 #1
3 10220
The property should be .Text for one. And if the conversion can't take place a
FormatException
is expected. You can try using double.TryParse instead and converting to an
integer. There are
other methods as well (Whidbey has an int.TryParse), and I've developed a
library that does
what you are looking for:

http://weblogs.asp.net/justin_rogers...es/104504.aspx
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Convert TextBox.Text to Int32 Problem" <ch**********@centerpointenergy.com>
wrote in message news:1F**********************************@microsof t.com...
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In
"old school C" we just used the atoi function and there you have it. So I
enquired and found the Convert class with it's promising ToInt32 method,
great... but it doesn't work. The thing keeps throwing Format Exceptions all
over the place. What is the "C#" way to do this??? code:
int wmin, wsec, x;
int hrs, min, sec, miles;
miles = Convert.ToInt32(txtMiles.text);
hrs = Convert.ToInt32(txtHours.text);
min = Convert.ToInt32(txtMinutes.text);
sec = Convert.ToInt32(txtSeconds.text);
. . .

I even tried forcing the values to string by using .... miles = Convert.ToInt32(txtMiles.text.ToString()); still not working....
Help!

Nov 16 '05 #2
Convert TextBox.Text to Int32 Problem
<ch**********@centerpointenergy.com> wrote:
Need a little help here. I saw some related posts, so here goes... I
have some textboxes which are designed for the user to enter a
integer value. In "old school C" we just used the atoi function and
there you have it. So I enquired and found the Convert class with
it's promising ToInt32 method, great... but it doesn't work. The
thing keeps throwing Format Exceptions all over the place. What is
the "C#" way to do this??? code:

int wmin, wsec, x;
int hrs, min, sec, miles;
miles = Convert.ToInt32(txtMiles.text);
hrs = Convert.ToInt32(txtHours.text);
min = Convert.ToInt32(txtMinutes.text);
sec = Convert.ToInt32(txtSeconds.text);
. . .
If Convert.ToInt32(string) is throwing an exception, then your text
presumably isn't a valid integer. Note that unlike atoi, which stops
when it meets the first non-numeric character and returns whatever it's
parsed so far, Convert.ToInt32(string) and Int32.Parse both require the
whole string to be a valid integer value (although whitespace is
allowed, by default).

If you want to emulate atoi, you'll have to write a method to find the
leading substring of a string which is composed of digits (perhaps
prefixed with '-'), or "0" if such a string would have no digits in.
I even tried forcing the values to string by using .... miles =
Convert.ToInt32(txtMiles.text.ToString());


If it wasn't a string before, what was it? I would imagine it was
already a string - calling ToString() on a string isn't going to make
any difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
I went to the weblog.asp.net site. It looks great, how do I compile the library, are there any instructions?
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: phancey | last post by:
I am trying to invoke a web service method dynamically. I have created a generic function that takes a method name, string of parameters and calls the web method using System.Reflection: ...
3
by: Tim Osborne | last post by:
I want to know if a call to Convert.Int32 can handle a Culture specific string. I have the following code: string sText = "111,111,111"; int i; i = Convert.Int32(...
5
by: Andy Sutorius | last post by:
Hi, I am attempting to convert this vb function to csharp but I am getting stuck on the if statement dt.Rows(iLoop)("FAQCategoryID")). The compiler says "method name expected" and underlines...
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
2
by: Simon Harris | last post by:
I have created a web service, which when I call in my browser presents the text form etc. When I click the button, I get this error: System.ArgumentException: Cannot convert to System.Int32....
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
2
by: Andez | last post by:
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to know where when how - it logs to a text file - in...
2
by: TG | last post by:
Hi! I am trying to export only the visible columns from a datagridview in my windows form in VB 2008. Should't it be no comma after the first row where the headers are? Also should...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.