472,975 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 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 10140
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.