Exception Error When Calling Procedure Multiple Times

A

aziz001

I have a form where a person can select how many of a product they want
e.g. 1x2g Syringe, 15x100g jar etc. The code will then display string
output in a label, listing the products that have a quanitity of
greater than 0. This works fine. Now what I wanted to do is that each
time a quanitity text box is changed the form would refresh the string
output. I have done this by calling the procedure (which outputs the
string output) many times for each quantity box's EventChanged event

Here's the procedure which outputs the string output (there are many
more 'gram' variables but they're all similar so i didn't include
them):

Public Sub PackingOutput()
Dim totalgram As String
Dim gram2 As String
Dim gram5 As String
Dim gram10 As String

Dim num_txt2g As Integer
Dim num_txt5g As Integer
Dim num_txt10g As Integer

num_txt2g = CInt(txt2g.Text)
If num_txt2g > 0 Then
gram2 = num_txt2g & " " & lbl2g.Text & ", "
End If

num_txt5g = CInt(txt5g.Text)
If num_txt5g > 0 Then
gram5 = num_txt5g & " " & lbl5g.Text & ", "
End If

num_txt10g = CInt(txt10g.Text)
If num_txt10g > 0 Then
gram10 = num_txt10g & " " & lbl10g.Text & ", "
End If

totalgram = gram2 & gram5 & gram10
lblPackingOutput2.Text = totalgram

And here's an example of the EventChanged events:

Private Sub txt2g_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles txt2g.TextChanged
Call PackingOutput()
End Sub


Private Sub txt5g_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles txt5g.TextChanged
Call PackingOutput()
End Sub

Private Sub txt10g_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles txt10g.TextChanged
Call PackingOutput()
End Sub


The problem is when I just have txt2g_TextChanged it works fine, but if
I have more than one sub calling the same procedure I get "an unhandled
exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll. Addition information: Cast from string "" to
type 'Integer' is not valid"


Anyone know how to fix?
 
J

Josef Brunner

Hi,

The problem is when I just have txt2g_TextChanged it works fine, but if
I have more than one sub calling the same procedure I get "an unhandled
exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll. Addition information: Cast from string "" to
type 'Integer' is not valid"


your PackingOutput Sub tries to convert the given texts to integer without
checking if the values in these boxes are numeric. It looks a at least one
text box contained an empty string and the CInt function can't convert this
empty string to an integer...

just a first gues..
Josef
 
A

AlanT

The problem is not really that you are calling the procedure multiple
times.

'Cast from string "" to type 'Integer' is not valid' - means that at
some point one of

txt2g.text
txt5g.text
txt10g.text

contains ""

1) put in some checking in PackingOutput() for empty strings
2) If the only thing that you are doing is calling PackingOutput, then
you can use the one handler for all events

Private Sub HandleTextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
txt2g.TextChanged,
txt5g.TextChanged,
txt10g.TextChanged

PackingOutput()

End Sub

hth,
Alan.
 
A

aziz001

I've checked the text boxes and they all have '0' in them when the
program is initialised. Also before doing anything with them (i.e.
comparison) I always convert the text '0' to an integer using Cint.

Also if I uncomment any of the other ....

num_txt2g = CInt(txt2g.Text)
If num_txt2g > 0 Then
gram2 = num_txt2g & " " & lbl2g.Text & ", "
End If

.... type of commands, same error comes up. It's only if I have the
first one that the program works.

I'm totally stumped.
I've uploaded the entire form here
http://rapidshare.de/files/15005081/packingoptions.zip.html in case
anyone can help.
 
A

AlanT

Try changing the Debug/Exception settings for 'Common Language Runtime
Exceptions' to Break into the Debugger when an exception is thrown (it
is usually set to Continue).

This will stop the program at the point where the exception occurs and
you can check the values in the various textboxes at that point.

Another option... add a Trace.Writeline() for each textbox value before
calling CInt to see what the value is.

hth,
Alan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top