Solved

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

Posted on 2004-09-02
5
263 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

10 Experts available now in Live!

Get 1:1 Help Now