Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

14 Experts available now in Live!

Get 1:1 Help Now