473,583 Members | 3,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help resolving runtime error

I have a program (posted below) that is supposed to take liters, which is
the user input, and convert it to pints and gallons. The pints and gallons
are displayed in a read only textbox. I don't understand how to fix the
problem.

'declaring variables

Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)

Dim totalPints As Double = Convert.ToDoubl e(txtPints.Text )

Dim totalGallons As Double = Convert.ToDoubl e(txtGallons.Te xt)

'assigning text box values to variables

totalLiters = txtLiters.Text

totalPints = txtPints.Text

totalGallons = txtGallons.Text

'performing calculations

txtPints.Text = totalLiters * 2.1133

txtGallons.Text = totalLiters * 0.26

Runtime error message:

An unhandled exception of type 'System.FormatE xception' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.
Feb 7 '06 #1
7 1867
Charlie Brookhart wrote:
I have a program (posted below) that is supposed to take liters,
which is the user input, and convert it to pints and gallons. The
pints and gallons are displayed in a read only textbox. I don't
understand how to fix the problem.

'declaring variables

Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)
<snip> 'assigning text box values to variables


Um, you've already done that...

If you use "option strict on", it will show you what the problem is.

Andrew
Feb 7 '06 #2
Charlie,

You are not checking to see if the text in the textbox is
actually a number. You should enclose where you convert the text in the
textboxes into a number in a try catch block. If you have vb 2005 use
double.tryparse instead.

Ken
------------------------

"Charlie Brookhart" wrote:
I have a program (posted below) that is supposed to take liters, which is
the user input, and convert it to pints and gallons. The pints and gallons
are displayed in a read only textbox. I don't understand how to fix the
problem.

'declaring variables

Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)

Dim totalPints As Double = Convert.ToDoubl e(txtPints.Text )

Dim totalGallons As Double = Convert.ToDoubl e(txtGallons.Te xt)

'assigning text box values to variables

totalLiters = txtLiters.Text

totalPints = txtPints.Text

totalGallons = txtGallons.Text

'performing calculations

txtPints.Text = totalLiters * 2.1133

txtGallons.Text = totalLiters * 0.26

Runtime error message:

An unhandled exception of type 'System.FormatE xception' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.

Feb 7 '06 #3
What do you mean that I have already assigned text boxes to variables? How
do I turn on the strict option?
"Andrew Morton" <ak*@in-press.co.uk.inv alid> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Charlie Brookhart wrote:
I have a program (posted below) that is supposed to take liters,
which is the user input, and convert it to pints and gallons. The
pints and gallons are displayed in a read only textbox. I don't
understand how to fix the problem.

'declaring variables

Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)

<snip>
'assigning text box values to variables


Um, you've already done that...

If you use "option strict on", it will show you what the problem is.

Andrew

Feb 7 '06 #4
I am working with VB 2003. I simply don't understand why I am getting a
runtime error and how to fix it.

"Ken Tucker [MVP]" <Ke**********@d iscussions.micr osoft.com> wrote in message
news:00******** *************** ***********@mic rosoft.com...
Charlie,

You are not checking to see if the text in the textbox is
actually a number. You should enclose where you convert the text in the
textboxes into a number in a try catch block. If you have vb 2005 use
double.tryparse instead.

Ken
------------------------

"Charlie Brookhart" wrote:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't understand how to fix the
problem.

'declaring variables

Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)

Dim totalPints As Double = Convert.ToDoubl e(txtPints.Text )

Dim totalGallons As Double = Convert.ToDoubl e(txtGallons.Te xt)

'assigning text box values to variables

totalLiters = txtLiters.Text

totalPints = txtPints.Text

totalGallons = txtGallons.Text

'performing calculations

txtPints.Text = totalLiters * 2.1133

txtGallons.Text = totalLiters * 0.26

Runtime error message:

An unhandled exception of type 'System.FormatE xception' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.

Feb 7 '06 #5
I found the option strict statement on the MSDN library CD. That creates an
error message as well and actually prevents the building of the program,
"statement is not inside a valid method.

So, I either get runtime errors or build errors. I still do not have a
working program at this point.

I'm entering a number in a textbox. Why doesn't VB recognize that a number
has been entered as opposed to something like abcd?
"Charlie Brookhart" <ch************ ****@hotmail.co m> wrote in message
news:e_******** *************** *******@adelphi a.com...
What do you mean that I have already assigned text boxes to variables? How
do I turn on the strict option?
"Andrew Morton" <ak*@in-press.co.uk.inv alid> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Charlie Brookhart wrote:
I have a program (posted below) that is supposed to take liters,
which is the user input, and convert it to pints and gallons. The
pints and gallons are displayed in a read only textbox. I don't
understand how to fix the problem.

'declaring variables

Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)

<snip>
'assigning text box values to variables


Um, you've already done that...

If you use "option strict on", it will show you what the problem is.

Andrew


Feb 7 '06 #6
First rule: Always check the validity of your user's input. In this case,
you are not checking to see if the user entered a number or not. By using
Convert.ToDoubl e, you will get a run-time exception if you pass in something
..Net doesn't recognize as a number. You need to check to see if it is a
number before trying to force a conversion on it. Failing that, wrap each
Convert.ToDoubl e (or Double.Parse) statement in a Try..Catch..End Try block.
(This is actually with .Net 1.x does under the covers with the VB library's
IsNumeric function, it just throws away the result and passes back whether
it was able to convert it or not.)

Second rule: Everything entered in a textbox on the UI is a string.
TextBox.Text does not accept a number. You must tell the textbox how you
want the number to appear as a string. For example,
Textbox1.text=M yNumber.ToStrin g("c2") will present the number formatted with
the current regional setting's currency format and 2 decimal places.
Textbox1.text=M yNumber throws an exception. Also Textbox1=MyNumb er will
throw a number as you can not assign a number to a textbox as they are not
the same type of object.

I recommend stepping back from the liters to pints issue and looking at the
basic data type issues. The reason for the Option Strict recommendation was
to force you to identify that you can't assign a number to a textbox.text
property. You are hitting the same error. By turning Option Strict On, you
catch the exception at compile time rather than waiting for it to crash your
application when an end user is consuming the application. Option Strict is
a good thing.

Jim Wooley
"Charlie Brookhart" <ch************ ****@hotmail.co m> wrote in message
news:rr******** ************@ad elphia.com...
I am working with VB 2003. I simply don't understand why I am getting a
runtime error and how to fix it.

"Ken Tucker [MVP]" <Ke**********@d iscussions.micr osoft.com> wrote in
message
news:00******** *************** ***********@mic rosoft.com...
Charlie,

You are not checking to see if the text in the textbox is
actually a number. You should enclose where you convert the text in the
textboxes into a number in a try catch block. If you have vb 2005 use
double.tryparse instead.

Ken
------------------------

"Charlie Brookhart" wrote:
> I have a program (posted below) that is supposed to take liters, which is > the user input, and convert it to pints and gallons. The pints and gallons > are displayed in a read only textbox. I don't understand how to fix the
> problem.
>
> 'declaring variables
>
> Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)
>
> Dim totalPints As Double = Convert.ToDoubl e(txtPints.Text )
>
> Dim totalGallons As Double = Convert.ToDoubl e(txtGallons.Te xt)
>
>
>
> 'assigning text box values to variables
>
> totalLiters = txtLiters.Text
>
> totalPints = txtPints.Text
>
> totalGallons = txtGallons.Text
>
> 'performing calculations
>
> txtPints.Text = totalLiters * 2.1133
>
> txtGallons.Text = totalLiters * 0.26
>
> Runtime error message:
>
> An unhandled exception of type 'System.FormatE xception' occurred in
> mscorlib.dll
>
> Additional information: Input string was not in a correct format.
>
>
>


Feb 7 '06 #7
The IsNumeric function would work in combination with an if then else
statement, would it not? If the user enters something other than a number,
an message box would display.

Being that I don't have any programming experience at all, and that I am
having to learn VB from a book, it looks like I am going to have a lot of
reading to in order to understand what goes into verrifying user input and
converting strings into something other than text.

Thank you to all who have responded to my post, and thanks for your help.

"Jim Wooley" <jw******@bells outh.net> wrote in message
news:uM******** ******@TK2MSFTN GP10.phx.gbl...
First rule: Always check the validity of your user's input. In this case,
you are not checking to see if the user entered a number or not. By using
Convert.ToDoubl e, you will get a run-time exception if you pass in something .Net doesn't recognize as a number. You need to check to see if it is a
number before trying to force a conversion on it. Failing that, wrap each
Convert.ToDoubl e (or Double.Parse) statement in a Try..Catch..End Try block. (This is actually with .Net 1.x does under the covers with the VB library's IsNumeric function, it just throws away the result and passes back whether
it was able to convert it or not.)

Second rule: Everything entered in a textbox on the UI is a string.
TextBox.Text does not accept a number. You must tell the textbox how you
want the number to appear as a string. For example,
Textbox1.text=M yNumber.ToStrin g("c2") will present the number formatted with the current regional setting's currency format and 2 decimal places.
Textbox1.text=M yNumber throws an exception. Also Textbox1=MyNumb er will
throw a number as you can not assign a number to a textbox as they are not
the same type of object.

I recommend stepping back from the liters to pints issue and looking at the basic data type issues. The reason for the Option Strict recommendation was to force you to identify that you can't assign a number to a textbox.text
property. You are hitting the same error. By turning Option Strict On, you
catch the exception at compile time rather than waiting for it to crash your application when an end user is consuming the application. Option Strict is a good thing.

Jim Wooley
"Charlie Brookhart" <ch************ ****@hotmail.co m> wrote in message
news:rr******** ************@ad elphia.com...
I am working with VB 2003. I simply don't understand why I am getting a
runtime error and how to fix it.

"Ken Tucker [MVP]" <Ke**********@d iscussions.micr osoft.com> wrote in
message
news:00******** *************** ***********@mic rosoft.com...
Charlie,

You are not checking to see if the text in the textbox is
actually a number. You should enclose where you convert the text in the textboxes into a number in a try catch block. If you have vb 2005 use
double.tryparse instead.

Ken
------------------------

"Charlie Brookhart" wrote:

> I have a program (posted below) that is supposed to take liters, which
is
> the user input, and convert it to pints and gallons. The pints and

gallons
> are displayed in a read only textbox. I don't understand how to fix

the > problem.
>
> 'declaring variables
>
> Dim totalLiters As Double = Convert.ToDoubl e(txtLiters.Tex t)
>
> Dim totalPints As Double = Convert.ToDoubl e(txtPints.Text )
>
> Dim totalGallons As Double = Convert.ToDoubl e(txtGallons.Te xt)
>
>
>
> 'assigning text box values to variables
>
> totalLiters = txtLiters.Text
>
> totalPints = txtPints.Text
>
> totalGallons = txtGallons.Text
>
> 'performing calculations
>
> txtPints.Text = totalLiters * 2.1133
>
> txtGallons.Text = totalLiters * 0.26
>
> Runtime error message:
>
> An unhandled exception of type 'System.FormatE xception' occurred in
> mscorlib.dll
>
> Additional information: Input string was not in a correct format.
>
>
>



Feb 8 '06 #8

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

Similar topics

3
2036
by: JGBNS via DotNetMonster.com | last post by:
Hi, I am new to this forumand I apologize as i am not a .net programmer but we have a program being developed by a .net programmer. Nowwe have run into an ftp snag and I think it is part ftp and part .net coding. Heres the issue.. We have an image directory full of photos like 200,000 in it, and if you use an ftp client to ftp to the...
13
1612
by: placid | last post by:
Hi all, I was just if anyone knows a way to resolve a list of integers say {1,2,3,4,5} into a range that is 1-5? #include <stdlib.h> #include <stdio.h> #include <ctype.h> int main(int argc, char*argv)
3
1928
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects to attributes in tags (think Style tag -> Style object). The problem that I am chasing is: Html side:
7
2524
by: fox | last post by:
Hi, Lacking javascript knowledge, I just realized why my project has a bug. I am using ASP to loop through a set of records while it creates URLs with a querystring that has a single value pair. This URL needs to open in a floating window if clicked. (this is for an administrator and so opening a small floater gives them more efficient...
0
2530
by: Pucca | last post by:
Hi I'm using vs2005. I am getting a bunch of compiler warnings after I made some changes to my code that was compiling clean. I'm also getting memory errors when I run my program and it's pinvoking some C++ code, which was working also fine before. I wonder if these warning are causing the problem. I would appreciate some help or...
4
278
by: Neo | last post by:
I have the following code structure- class A { public: virtual void foo_a()=0; }; class B { public: virtual void foo_b()=0; };
9
1333
by: =?ISO-8859-1?Q?Janne_H=E4rk=F6nen?= | last post by:
Hello, Is there a simple way to resolve declaring class of a method at runtime ? Consider this simple example: $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on cygwin Type "help", "copyright", "credits" or "license" for more information.
0
1369
by: =?Utf-8?B?TWlrZSBMYWZmZXJ0eQ==?= | last post by:
I have a C# COM dll (using ComVisible, ClassInterface etc) and that dll has a number of references (other custom dll's). For example MyComProj.dll, MyComProjReference.dll which live in C:\MyAssemblies I register the MyComProj.dll using regasm etc. I can now instantiate the required COM object in this dll if I create a new exe project...
1
2925
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements: The One...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6574
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3813
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.