Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-02
5
Medium Priority
?
273 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

886 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