473,388 Members | 1,340 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,388 software developers and data experts.

Exception Error When Calling Procedure Multiple Times

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?

Mar 8 '06 #1
4 1166
Hi,

<az*****@googlemail.com> schrieb im Newsbeitrag
news:11*********************@j33g2000cwa.googlegro ups.com...
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
Mar 8 '06 #2

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.

Mar 8 '06 #3
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/...tions.zip.html in case
anyone can help.

Mar 8 '06 #4

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.

Mar 9 '06 #5

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

Similar topics

2
by: CJM | last post by:
I have page that starts a transaction and runs several StoredProcs before committing or rollingback. An initial SP create a header records, and then the code goes into a loop and runs 4 other SP's...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
16
by: Steve Jorgensen | last post by:
I'm trying to figure out if there is a way to generate standard error handlers that "know" if the calling code has an error handler in effect (On Error Goto <label> or On Error Resume Next) before...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
9
by: Ron | last post by:
my application is throwing an exception error when closing if I run a procedure in the app. I can't even trap the error with try/catch ex As Exception. Is there a way to completely shut down the...
7
by: Yarco | last post by:
Well, friend. Yes, I'm doing PHP. But exception is a traditional machinism in programming languages. I'm very doubt about this. In procedure language, we return error code when we meet an error. So...
2
by: william.david.anderson | last post by:
Hi there, I'm thinking of using a PREPARE statement inside a stored procedure, but am wondering about the performance penalty associated with calling PREPARE multiple times. Below is an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.