Solved

System.OverflowException..........Error using Mid command

Posted on 2004-09-02
5
258 Views
Last Modified: 2011-10-03
My program keeps hanging in this one location with the following error message.

System.OverflowException.........The help file I read said it has something to do with an invalid calculation.

Here is the dim statement for the variable.
Dim campNum As Int16

Here is the line where I get the error message

objStreamReader = New StreamReader("C:\fbisd\t178914a.dat")
        strLine = objStreamReader.ReadLine
        Do While Not strLine Is Nothing
            admDate = Mid(strLine, 1, 4)
            admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
            grade = Mid(strLine, 5, 2)
            esc = Mid(strLine, 7, 2)
            campNum = Mid(strLine, 9, 9)  <---------------------------------- This Line gets the error message

As you can see, I am just reading a .dat file.  I checked the data at position 9 and it is all numeric data for 9 digits long.

HELP !!!!!

Any help is very much appreciated.

Thanks
Kenny




0
Comment
Question by:kwh3856
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11970305
An Int16 can only hold numbers from -32,768 to 32,767.  If you indeed have 9 characters of "all numeric data" then you most likely have execeeded the max and thus are receiving the System.OverflowException error.

Change your variable type from Int16 to Int32 and you should be fine:

    Dim campNum As Int32

Regards,

Idle_Mind
0
 
LVL 3

Expert Comment

by:GrumbleBot
ID: 11970315
The error in your program is that you read strLine once then go into a do while loop. The data is never changing so it just keeps going through the loop and I'm guessing after some iterations you ge tthe overflow error. Possibly memory filling up.

Below I have changed your code slightly so that the stream read is inside the loop.


        objStreamReader = New System.IO.StreamReader("D:\data.dat")
        Do
            strLine = objStreamReader.ReadLine
            If Not strLine Is Nothing Then
                admDate = Mid(strLine, 1, 4)
                admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
                grade = Mid(strLine, 5, 2)
                esc = Mid(strLine, 7, 2)
                campNum = Convert.ToInt16(Mid(strLine, 9, 9))  '<---------------------------------- This Line gets the error message
            End If
        Loop While Not strLine Is Nothing

Hope this helps.
Good Luck
0
 
LVL 3

Expert Comment

by:GrumbleBot
ID: 11970322
OH you don't really need the Convert.ToInt16() in the campNum line either. I was using during the testing of your problem.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11970417
>> The error in your program is that you read strLine once then go into a do while loop.

You can't see the rest of the code...including the matching "Loop" for the Do While", so any assumptions about what comes after the line where the error is occuring are fruitless.  The Do...While Loop woud look like this:

        Dim objStreamReader As StreamReader
        Dim strLine As String

        objStreamReader = New StreamReader("C:\fbisd\t178914a.dat")
        strLine = objStreamReader.ReadLine
        Do While Not strLine Is Nothing
            admDate = Mid(strLine, 1, 4)
            admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
            grade = Mid(strLine, 5, 2)
            esc = Mid(strLine, 7, 2)
            campNum = Mid(strLine, 9, 9)  <---------------------------------- This Line gets the error message

            ' ... more code possibly ...

            strLine = objStreamReader.ReadLine
        Loop
        objStreamReader.Close()

>> The data is never changing so it just keeps going through the loop and I'm guessing after some iterations you ge tthe overflow error. Possibly memory filling up.

Since the data never changes and nothing in the code shown makes an ever growing string, you wouldn't run out of memory.  If the strLine = objStreamReader.ReadLine line were forgotten, then the application would simply lock up.  Running out of memory causes a StackOverFlowException error not an OverflowException error.

Regards,

Idle_Mind
0
 

Author Comment

by:kwh3856
ID: 11972491
Idle_Mind,

You Rock!!!!!!!!!!!!!

Thanks...that fixed my problem.  I found other variables that I had to do the same thing with.

Thanks
Kenny
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now