Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,560 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,923 people online right now. Registration is fast and FREE... Join Now!




How to fix acception

 
Reply to this topicStart new topic

How to fix acception

pfales
12 Apr, 2008 - 12:04 PM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 11

The object of this code is to extract data from a txt file and make it usable in a form. the program will actually do this HOWEVER, I get an exception error (on the line I have changed to red (I think)) the actual line that get the exception error is," _decPrice(intCount) = Convert.ToDecimal(objReader.ReadLine()) ". How do I oover come this exception error? or Can I just remove the line and still get the same results that I want?

Script in vb 2005
CODE

Dim _strLocationandNameofFile As String = "F:\MiramarBooks.txt"
        Dim strNameItem As String
        'The ListBox object is filled with the Names of the Books held in the Inventory.
        Dim objReader As IO.StreamReader
        Dim intCount As Integer
        Dim strFileErrorMessage As String = "The File is not available. Restart when it is available."

      
        If IO.File.Exists(_strLocationandNameofFile) Then
            objReader = IO.File.OpenText(_strLocationandNameofFile)

            'Read the file line by line until the file is completed
            While Not objReader.EndOfStream

                If intCount = 9 Then
                    ReDim Preserve _strInventoryItem(_intSizeOfArray + 10)
                    ReDim Preserve _strName(_intSizeOfArray + 10)
                    ReDim Preserve _decPrice(_intSizeOfArray + 10)
                    ReDim Preserve _intQuantityonHand(_intSizeOfArray + 10)
                    ReDim Preserve _intQuantityonOrder(_intSizeOfArray + 10)
                End If

                If Not objReader.EndOfStream Then

                    _strInventoryItem(intCount) = objReader.ReadLine()
                    [color=#FF0000]_decPrice(intCount) = Convert.ToDecimal(objReader.ReadLine())[/color]
                    _intQuantityonHand(intCount) = Convert.ToInt32(objReader.ReadLine())
                    _intQuantityonOrder(intCount) = Convert.ToInt32(objReader.ReadLine())
                    intCount += 1
                End If
            End While
            objReader.Close()
        End If


        Dim topIndex As Integer
        Dim n As Integer
        Dim Order As Integer
        Dim OnHand As Integer
        Dim dTotalOH As Decimal
        Dim dTotalOnO As Decimal
        topIndex = intCount - 1



        For n = 0 To topIndex
            Me.lstBooks.Items.Add(_strInventoryItem(n))

          
        Next
        Me.lstBooks.SelectedItem = 1
      

        Me.lblBooksonHand.Text = OnHand.ToString
        Me.lblBooksonOrder.Text = Order.ToString
        Me.lblCostonHand.Text = dTotalOH.ToString
        Me.lblCostonOrder.Text = dTotalOnO.ToString

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: How To Fix Acception
12 Apr, 2008 - 02:28 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Try taking a look at the TryParse method of Decimal. This method takes a string and parses it to a Decimal for you, which you provide as a parameter. The return value is true/false to let you know if the parse worked.

Code compliments of MSDN...

vb

Dim value As String
Dim number As Decimal

' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If


Notice that they give it the variable "number" which if the parse is successful it stashes the value in that variable. It is then used in an if statement to test if the parse was successful.

More information can be found at the link below...

Decimal.TryParse method - MSDN

Some of the other classes/structs have the same property for parsing to their respective data types.

Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Fix Acception
12 Apr, 2008 - 03:11 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.Net smile.gif
User is offlineProfile CardPM
+Quote Post

pfales
RE: How To Fix Acception
12 Apr, 2008 - 09:15 PM
Post #4

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 11

Martyr2,

Thank you for your reply but I am obviously not bright enough to figure this one out. The decimals I am working with are not a set number but vary such as 24.95, 18.95, 35.65. My other question is would it require me to change the rest of my code if I do this?


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: How To Fix Acception
12 Apr, 2008 - 09:23 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Uhhh everything requires you to change a little of your code. If you mean drastically, no...

vb

' Old line
_decPrice(intCount) = Convert.ToDecimal(objReader.ReadLine())

' New line
Decimal.TryParse(objReader.ReadLine(),_decPrice(intCount))


Then of course you test the return value to see if the parse was successful.

smile.gif
User is offlineProfile CardPM
+Quote Post

pfales
RE: How To Fix Acception
12 Apr, 2008 - 09:33 PM
Post #6

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 11

Martyr2,

You mean that is all I needed to change was one line? I have been pulling my hair out trying to figure how to rewrite everything to include the Tryparse.
I can see I have alot to learn before I get beyind the grasshopper stage.

Thank you again.

Oh why was my original post moved from its location do you know?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Fix Acception
12 Apr, 2008 - 09:35 PM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
pfales, I moved it to the VB.Net forum since this is a VB.Net problem smile.gif
User is offlineProfile CardPM
+Quote Post

pfales
RE: How To Fix Acception
13 Apr, 2008 - 12:59 PM
Post #8

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 11

Oh.... Okay, sorry I was not aware of that. Thank you for the explination.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:34PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month