Exception writing a Seq file after reading seq file and loading into ArrayList

Joined
Oct 10, 2005
Messages
7
Programming Experience
Beginner
I am reading a Sequential file load it into ArrayList and when the user clicks mmuSaveFile_Click. I get the exception (Specified cast is not valid). Can anyone tell me what I am doing wrong ? Look for EXCEPTION below. Also when I use MS Visual Studio to step thru the code it never goes to Global.vb or Employee.vb. Is this normal? Thanks for any help.

VB.NET:
[COLOR=blue][FONT=Courier New]Code in frmMain.vb [/FONT][/COLOR]
 
[FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] mmuOpenFile_Click([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, _[/FONT]
[FONT=Courier New]       [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.EventArgs) [COLOR=blue]Handles[/COLOR] mmuOpenFile.Click[/FONT]
[FONT=Courier New]    [COLOR=green]' Open a sequential file and read it into an ArrayList.[/COLOR][/FONT]
 
[FONT=Courier New]    [COLOR=blue]Dim[/COLOR] pstrCurrent [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] [COLOR=green]' current line read [/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Dim[/COLOR] pstrFields() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] [COLOR=green]' Array of Strings Current record [/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Dim[/COLOR] pstrDelimiter() [COLOR=blue]As[/COLOR] [COLOR=blue]Char[/COLOR] = {ToChar(",")} [/FONT]
[FONT=Courier New]    [COLOR=blue]Try[/COLOR][/FONT]
[FONT=Courier New]        [COLOR=blue]Dim[/COLOR] pstrFileName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = "A:\DataIn.txt"[/FONT]
[FONT=Courier New]        [COLOR=blue]Dim[/COLOR] psrdCurrent [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] System.IO.StreamReader(pstrFileName)[/FONT]
[FONT=Courier New]        pstrCurrent = psrdCurrent.ReadLine() [/FONT]
 
[FONT=Courier New]        [COLOR=blue]Do[/COLOR] [COLOR=blue]Until[/COLOR] pstrCurrent = [COLOR=blue]Nothing[/COLOR] [COLOR=green]' Read records until EOF[/COLOR][/FONT]
[FONT=Courier New]            [COLOR=blue]Dim[/COLOR] recNew [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] Employee.Employee [COLOR=green]' Split the record into fields.[/COLOR][/FONT]
[FONT=Courier New]            pstrFields = pstrCurrent.Split(pstrDelimiter)[/FONT]
 
[FONT=Courier New]            recNew.CustId = System.Convert.ToInt32(pstrFields(0))[/FONT]
[FONT=Courier New]            recNew.CustFname = pstrFields(1)[/FONT]
[FONT=Courier New]            recNew.CustLname = pstrFields(2)[/FONT]
[FONT=Courier New]            recNew.CustBalDue = System.Convert.ToDouble(pstrFields(3))[/FONT]
[FONT=Courier New]            recNew.CustCredLimit = System.Convert.ToDouble(pstrFields(4))[/FONT]
 
[FONT=Courier New]            alRecords.Add(recNew) [COLOR=green]' Add the call record to the end of the ArrayList.[/COLOR][/FONT]
[FONT=Courier New]            pstrCurrent = psrdCurrent.ReadLine [COLOR=green]' Read the next line in the input file.[/COLOR][/FONT]
[COLOR=blue][FONT=Courier New]Loop[/FONT][/COLOR]
 
[FONT=Courier New]        psrdCurrent.Close() [COLOR=green]' Close the file when reading is complete.[/COLOR][/FONT]
[FONT=Courier New]        mintCurrent = 0 [COLOR=green]' Index for Array[/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Catch[/COLOR] ex [COLOR=blue]As[/COLOR] System.Exception[/FONT]
[FONT=Courier New]        MessageBox.Show(ex.Message)[/FONT]
[FONT=Courier New]    [COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[SIZE=3][FONT=Times New Roman]‘===============================================[/FONT][/SIZE]
[FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] mmuSaveFile_Click([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As _[/COLOR][/FONT]
[FONT=Courier New]System.EventArgs) [COLOR=blue]Handles[/COLOR] mmuSaveFile.Click[/FONT]
[FONT=Courier New]    [COLOR=blue]Dim[/COLOR] pstrFileName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = "A:\DataOut.txt"[/FONT]
[FONT=Courier New]    [COLOR=blue]Dim[/COLOR] pswriCurrent [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] System.IO.StreamWriter(pstrFileName)[/FONT]
[FONT=Courier New]    [COLOR=blue]Try[/COLOR][/FONT]
[FONT=Courier New]        [COLOR=blue]Dim[/COLOR] recCurrent [COLOR=blue]As[/COLOR] Customer[/FONT]
[FONT=Courier New]        [COLOR=blue]Dim[/COLOR] iLp [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
[FONT=Courier New]        [COLOR=blue]For[/COLOR] iLp = 0 [COLOR=blue]To[/COLOR] alRecords.Count - 1[/FONT]
 
[FONT=Courier New]            [B]recCurrent = alRecords(iLp) ‘[/B][COLOR=green][B]<==EXCEPTION[/B] [/COLOR]Specified cast is not valid[/FONT]
 
[FONT=Courier New]            pswriCurrent.Write(recCurrent.ID.ToString)[/FONT]
[FONT=Courier New]            pswriCurrent.Write(",")[/FONT]
[FONT=Courier New]            pswriCurrent.Write(recCurrent.FirstName)[/FONT]
[FONT=Courier New]            pswriCurrent.Write(",")[/FONT]
[FONT=Courier New]            pswriCurrent.Write(recCurrent.LastName)[/FONT]
[FONT=Courier New]            pswriCurrent.Write(",")[/FONT]
[FONT=Courier New]            pswriCurrent.WriteLine(recCurrent.BalanceDue.ToString)[/FONT]
[FONT=Courier New]            pswriCurrent.Write(",")[/FONT]
[FONT=Courier New]            pswriCurrent.WriteLine(recCurrent.CreditLimit.ToString)[/FONT]
[FONT=Courier New]        [COLOR=blue]Next[/COLOR] iLp[/FONT]
[FONT=Courier New]    [COLOR=blue]Catch[/COLOR] ex [COLOR=blue]As[/COLOR] System.Exception[/FONT]
[FONT=Courier New]        MessageBox.Show(ex.Message)[/FONT]
[FONT=Courier New]    [COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR][/FONT]
[FONT=Courier New]    pswriCurrent.Close() [COLOR=green]'The file was closed after loading Array  [/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[FONT=Times New Roman][SIZE=3]‘===============================================[/SIZE][/FONT]
[SIZE=3][FONT=Times New Roman]Code in Employee.vb[/FONT][/SIZE]
[COLOR=blue][FONT=Courier New]Public[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Class[/COLOR] Employee[/FONT]
[FONT=Courier New][COLOR=blue]Inherits[/COLOR] System.ComponentModel.Component[/FONT]
[FONT=Courier New][COLOR=blue]Public[/COLOR] [COLOR=blue]Class[/COLOR] Employee[/FONT]
[FONT=Courier New]    [COLOR=green]' The CustT structure is a Text File. [/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Public[/COLOR] CustId [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Public[/COLOR] CustFname [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Public[/COLOR] CustLname [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Public[/COLOR] CustBalDue [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR][/FONT]
[FONT=Courier New]    [COLOR=blue]Public[/COLOR] CustCredLimit [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Class[/COLOR][/FONT]
[COLOR=blue][FONT=Courier New]End[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Class[/COLOR][/FONT]
[FONT=Times New Roman][SIZE=3]‘===============================================[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Code in Global.vb [/SIZE][/FONT]
[COLOR=blue][FONT=Courier New]Option[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Strict[/COLOR] [COLOR=blue]On[/COLOR]    [/FONT]
[COLOR=blue][FONT=Courier New]Option[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Explicit[/COLOR] [COLOR=blue]On[/COLOR]  [/FONT]
 
[COLOR=green][FONT=Courier New]' File Layout for Customer [/FONT][/COLOR]
 
[COLOR=blue][FONT=Courier New]Public[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Structure[/COLOR] Customer[/FONT]
[FONT=Courier New][COLOR=blue]Public[/COLOR] ID [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Public[/COLOR] FirstName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Public[/COLOR] LastName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Public[/COLOR] BalanceDue [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Public[/COLOR] CreditLimit [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR][/FONT]
[COLOR=blue][FONT=Courier New]End[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Structure[/COLOR][/FONT]
 
Typically, you'd want to set employee and global's content definitions to "property" instead of public variables. It'll give you more flexibility and validation options.

The reason it steps over employee and glabal is that variable declarations aren't valid break point, so VB naturally steps over them unless they are declared as "new" and you placed a breakstatement in the constructors code.

As far as the typecast exception; we would need to see a sample of the file you're importing to know what's being read into the application.

(one thing I did notice right off, if it's a comma del'd file and you have currency in there; make sure the currency value in the file aren't in a format that still has commas. Kind of a "no duh" point, but I've made that mistaks a few times) :)

Edit: THis may be easier then I made it sound; sorry. What is "alRecords" declared as?
 
I will check the file and then I will take my code to a tutor and get an explanation of what I am doing wrong along with a copy of your reply. Thanks for the help!
 
This actually is alot simpler than I made it seem. We can help you out if you need assistance.

alRecords needs to be declared as an array of customers; if it isn't already.
 
Back
Top