Solved

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

Posted on 2004-09-02
5
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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