Results 1 to 5 of 5

Thread: Having a problem with a database not loading properly

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014

    Having a problem with a database not loading properly

    Public Class frmMainForm

    Public Structure database
    Public Name As String
    Public JagNumber As Object
    Public Age As Double
    Public YearInScool As String
    Public Sex As String
    Public ParkingSticker As String
    Public FootballTicket As String
    Public CostofAllFees As Double

    End Structure

    Public filename As String
    Public items() As database

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddS.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFile.Click


    Dim filename As String = OpenFileDialog1.FileName
    Dim lines() As String
    lines = IO.File.ReadAllLines(filename)
    Catch ex As IO.FileNotFoundException

    End Try

    Dim data() As String


    ReDim items(lines.Count - 1)
    For i As Integer = 0 To lines.Count - 1
    data = lines(i).Split(","c)

    items(i).Name = data(0)
    items(i).JagNumber = data(1)
    items(i).Age = data(2)
    items(i).YearInScool = data(3)
    items(i).Sex = data(4)
    items(i).ParkingSticker = data(5)
    items(i).FootballTicket = data(6)


    Catch ex As ArgumentNullException

    End Try


    basically the file I choose to load is not loading due to line not being "declared"

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014

    Re: Having a problem with a database not loading properly

    Show a screenshot of your error.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002

    Re: Having a problem with a database not loading properly

    ReDim items(lines.Count - 1)

    1) You cannot redim something you haven't dimed in the first place.
    2) Why are you sizing it to the number of lines?
    3) it should be jsut dim, no sizing... the split will re-size it correctly.
    4) you can drop the Dim data() as string - you're not using it

    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Hyperactive Member
    Join Date
    Mar 2012

    Re: Having a problem with a database not loading properly

    Your code is re-inventing all sorts of wheels... If this is for a class, it may just be that you are supposed to use these "primitive" structures, but if you are able to use more robust classes, I'd suggest a few changes that may save you some headaches in the end... Firstly, it looks like you are just parsing a CSV file and importing it into a "database" (although more accurately this is just a DataTable). The first suggestion would therefore be to use a TextFieldParser to help you parse the file. Here is an example of parsing a file into an array of Strings where each is a field for a particular row / tuple of data. In this example:
     CurrentRecord = afile.ReadFields
    'Now we have our line in an array CurrentRecord
    'Do what we wish with it
    would be the place where you'd put in your code for adding the data into your data-structure. Which brings me to issue #2... Instead of creating an array of your database structure, I'd suggest using a List(Of database). Lists differ from arrays in that they can be built dynamically without having to Dim your array to the appropriate size. Thus you'd have
    ' Instead of Public items() As database
    Public items As New List(Of database)
    So then combining your code with the TextFieldParser example, the interior code (that was stubbed with a comment in the example) may look like:
    CurrentRecord = afile.ReadFields ' From example...
    Dim row As database
    row.Name = CurrentRecord(0)
    row.JagNumber = CurrentRecord(1)
    row.Age = CurrentRecord(2)
    row.YearInScool = CurrentRecord(3)
    row.Sex = CurrentRecord(4)
    row.ParkingSticker = CurrentRecord(5)
    row.FootballTicket = CurrentRecord(6)
    A third suggestion is more for trying to incorporate a more OO-style to your program... Instead of a database Structure, I'd actually make this into a full fledged Class complete with its own constructor that takes a comma delimited String, parses that into its specific fields, and adds it to your data-structure. This way if at a later date your data structure changes (eg you add a new field), you can easily change the code in that Class and leave the rest of your code alone. A final comment is more cosmetic in aiding readability... In general, a database is a collection of records and each record is a collection of fields. However, your "database" structure is actually a structure for a data row / record / tuple and the "items" array is more of a data table / database. Thus I'd rename your elements as such: rename your structure as "record" and your array of record (or List(Of record)) as database. This would help when reading your code know what the different elements do or what they are for.
    Last edited by Pyth007; Dec 9th, 2014 at 03:10 PM. Reason: Forgot to change data() to CurrentRecord from example

  5. #5
    Hyperactive Member
    Join Date
    Mar 2012

    Re: Having a problem with a database not loading properly

    just my 2 cents worth but here's what i see....

     If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim filename As String = OpenFileDialog1.FileName
                Dim lines() As String
                   '''' It can't add to the array if it doesn't know where the line breaks are
                   '''' lines = IO.File.ReadAllLines(filename)
                    lines = IO.File.ReadAllLines(filename).ToString.Split(vbCrLf)
                Catch ex As IO.FileNotFoundException
                End Try
            End If
    Last edited by M@dH@tter; Dec 9th, 2014 at 11:21 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width