Results 1 to 10 of 10

Thread: storing multiple userdetails from a textfile

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42

    storing multiple userdetails from a textfile

    hey, I'm stuck again

    I have been given a textfile that contains names, departments, email addresses and phone numbers in the format...

    firstname, lastname, department, email, phone
    firstname2, lastname2, department2, email2, phone2

    etc etc...

    each persons details are on a different line of the file.

    I need to read this data into a vb.net program then do different stuff with it (such as display names of all users in a certain department in a listview etc)

    My question is, what is the best way of storing this data in the program? I can't just set up arrays (if that would make sence anyway) for each person as I have no way of knowing how many users details will be supplied in the textfile, but it is likely to be in the hundreds but rising to the thousands later on. I can't just check the textfile each time i need the information as I will need to change data without it changing the textfile.

    I'm not looking for the code to do this, just a point in the direction of a method of doing it.

    Many thanks for your help.

  2. #2
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    Grandad,

    I don't fool around with a lot of code for things like this. I found a free program called AutoIt that I use. It can be integrated as a COM/DLL file and used with your projects. There is also a version 2 of the program that is a little more suited for reading and writing to a textfile in diffrent formats.

    Hope this helps.

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    If I were facing this, I would probably create a DataTable that has the same structure as your text file, then I would open the file, read it a line at a time, strip out the spaces, split the result into an array using the comma character, then add a row to your table from the values in the array. I could probably spend half an hour writing this up if you really need it.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42
    Originally posted by CyberHawke
    If I were facing this, I would probably create a DataTable that has the same structure as your text file, then I would open the file, read it a line at a time, strip out the spaces, split the result into an array using the comma character, then add a row to your table from the values in the array. I could probably spend half an hour writing this up if you really need it.
    If you have the time, further details on this would be really good... The file does not have spaces between items so the format is

    firstname,lastname,department,email,phone

    etc
    etc

    Many thanks

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Here is a simple example that should provide a starting point for you.

    VB Code:
    1. Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    2.     Dim dt As New DataTable("Customers")
    3.     dt.Columns.Add("firstname", System.Type.GetType("System.String"))
    4.     dt.Columns.Add("lastname", System.Type.GetType("System.String"))
    5.     dt.Columns.Add("department", System.Type.GetType("System.String"))
    6.     dt.Columns.Add("email", System.Type.GetType("System.String"))
    7.     dt.Columns.Add("phone", System.Type.GetType("System.String"))
    8.     Dim oReader As New StreamReader("C:\Customers.txt")
    9.     Dim oCust As String
    10.     Dim oData() As String
    11.     Dim Index As Int32
    12.     Dim drItem As DataRow
    13.     Do
    14.         oCust = oReader.ReadLine
    15.         oData = oCust.Split(",".ToCharArray)
    16.         For Index = oData.GetLowerBound(0) To oData.GetUpperBound(0)
    17.             drItem = dt.NewRow
    18.             drItem(Index) = oData(Index).ToString
    19.             dt.Rows.Add(drItem)
    20.             drItem = Nothing
    21.         Next
    22.     Loop Until oCust Is Nothing
    23. End Sub

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Actually, there is a much simpler way to do this, I just wasn't thinking
    You can use an ODBC connection to connect directly to your text file then fill a table from an adapter, really simple, but I'll have to respond (if you need help with that) in about an hour.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42
    Originally posted by CyberHawke
    Actually, there is a much simpler way to do this, I just wasn't thinking
    You can use an ODBC connection to connect directly to your text file then fill a table from an adapter, really simple, but I'll have to respond (if you need help with that) in about an hour.
    That sounds good, but yeah I'm afraid I am going to need help when you have a chance

    Thanks very much

  8. #8
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I would just create two custom classes

    VB Code:
    1. Option Explicit On
    2.  
    3. Public Class Person
    4.  
    5.     Private m_strFirstName As String
    6.     Private m_strLastName As String
    7.     Private m_strDepartment As String
    8.     Private m_strEmail As String
    9.     Private m_strPhone As String
    10.  
    11.     Public Property FirstName() As String
    12.         Get
    13.             Return m_strFirstName
    14.         End Get
    15.         Set(ByVal Value As String)
    16.             m_strFirstName = Value
    17.         End Set
    18.     End Property
    19.     Public Property LastName() As String
    20.         Get
    21.             Return m_strLastName
    22.         End Get
    23.         Set(ByVal Value As String)
    24.             m_strLastName = Value
    25.         End Set
    26.     End Property
    27.     Public Property Department() As String
    28.         Get
    29.             Return m_strDepartment
    30.         End Get
    31.         Set(ByVal Value As String)
    32.             m_strDepartment = Value
    33.         End Set
    34.     End Property
    35.     Public Property Email() As String
    36.         Get
    37.             Return m_strEmail
    38.         End Get
    39.         Set(ByVal Value As String)
    40.             m_strEmail = Value
    41.         End Set
    42.     End Property
    43.     Public Property Phone() As String
    44.         Get
    45.             Return m_strPhone
    46.         End Get
    47.         Set(ByVal Value As String)
    48.             m_strPhone = Value
    49.         End Set
    50.     End Property
    51.  
    52.  
    53.     Public Sub New(ByVal strFirstName As String, ByVal strLastName As String, ByVal strDepartment As String, ByVal strEmail As String, ByVal strPhone As String)
    54.  
    55.         FirstName = strFirstName
    56.         LastName = strLastName
    57.         Department = strDepartment
    58.         Email = strEmail
    59.         Phone = strPhone
    60.  
    61.     End Sub
    62. End Class
    63.  
    64. Public Class PersonCollection
    65.     Inherits System.Collections.CollectionBase
    66.  
    67.     ' Restricts to Person types, items that can be added to the collection.
    68.     Public Sub Add(ByVal aPerson As Person)
    69.         ' Invokes Add method of the List object to add a Person.
    70.         List.Add(aPerson)
    71.     End Sub
    72.  
    73.     Public Sub Remove(ByVal index As Integer)
    74.         ' Check to see if there is a Person at the supplied index.
    75.         If index > Count - 1 Or index < 0 Then
    76.             ' If no Person exists, a messagebox is shown and the operation is
    77.             ' cancelled.
    78.             System.Windows.Forms.MessageBox.Show("Index not valid!")
    79.         Else
    80.             ' Invokes the RemoveAt method of the List object.
    81.             List.RemoveAt(index)
    82.         End If
    83.     End Sub
    84.  
    85.     ' This line declares the Item property as ReadOnly, and
    86.     ' declares that it will return a Person object.
    87.     Public ReadOnly Property Item(ByVal index As Integer) As Person
    88.         Get
    89.             ' The appropriate item is retrieved from the List object and
    90.             ' explicitly cast to the Person type, then returned to the
    91.             ' caller.
    92.             Return CType(List.Item(index), Person)
    93.         End Get
    94.     End Property
    95.  
    96.     Public Sub Load(ByVal strTextFile As String)
    97.         'Loads the information in the comma delimited text file
    98.         'Format: "firstname, lastname, department, email, phone"
    99.  
    100.         Dim sr As New IO.StreamReader(strTextFile)
    101.  
    102.         Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
    103.  
    104.         sr.Close()
    105.         sr = Nothing
    106.  
    107.         For I As Integer = 0 To strFile.Length - 1
    108.  
    109.             Dim strTmp() As String = Split(strFile(I), " ,")
    110.  
    111.             Me.Add(New Person(strTmp(0), strTmp(1), strTmp(2), strTmp(3), strTmp(4)))
    112.  
    113.         Next
    114.  
    115.         'Job Done
    116.     End Sub
    117.  
    118. End Class

    Just do:

    VB Code:
    1. Dim Data as New PersonCollection
    2.  
    3. Data.Load(TextFile)
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42
    Originally posted by CyberHawke
    Here is a simple example that should provide a starting point for you.

    VB Code:
    1. Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    2.     Dim dt As New DataTable("Customers")
    3.     dt.Columns.Add("firstname", System.Type.GetType("System.String"))
    4.     dt.Columns.Add("lastname", System.Type.GetType("System.String"))
    5.     dt.Columns.Add("department", System.Type.GetType("System.String"))
    6.     dt.Columns.Add("email", System.Type.GetType("System.String"))
    7.     dt.Columns.Add("phone", System.Type.GetType("System.String"))
    8.     Dim oReader As New IO.StreamReader("C:\Customers.txt")
    9.     Dim oCust As String
    10.     Dim oData() As String
    11.     Dim Index As Int32
    12.     Dim drItem As DataRow
    13.     Do
    14.         oCust = oReader.ReadLine
    15.         oData = oCust.Split(",".ToCharArray)
    16.         For Index = oData.GetLowerBound(0) To oData.GetUpperBound(0)
    17.             drItem = dt.NewRow
    18.             drItem(Index) = oData(Index).ToString
    19.             dt.Rows.Add(drItem)
    20.             drItem = Nothing
    21.         Next
    22.     Loop Until oCust Is Nothing
    23. End Sub
    Hi, I have been playing with this but am getting the following error...

    ---------------------

    An unhandled exception of type 'System.NullReferenceException' occurred in textfiletest.exe

    Additional information: Object reference not set to an instance of an object.

    ---------------------

    The line highlighted is "oData = oCust.Split(",".ToCharArray)"

    I expect its something simple but what am I doing wrong?
    Last edited by grandad; Jun 24th, 2004 at 07:10 PM.

  10. #10
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I wrote that while I was trying to do 5 other things and didn't test it before posting it . . . I won't make that mistake again

    This code is tested and works very well
    VB Code:
    1. Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    2.     Dim dt As New DataTable("Customers")
    3.     dt.Columns.Add("firstname", System.Type.GetType("System.String"))
    4.     dt.Columns.Add("lastname", System.Type.GetType("System.String"))
    5.     dt.Columns.Add("department", System.Type.GetType("System.String"))
    6.     dt.Columns.Add("email", System.Type.GetType("System.String"))
    7.     dt.Columns.Add("phone", System.Type.GetType("System.String"))
    8.     Dim oReader As New StreamReader("C:\bin\Customers.txt")
    9.     Dim oCust As String
    10.     Dim Delim As String = ","
    11.     Dim oData() As String
    12.     Dim Index As Int32
    13.     Dim drItem As DataRow
    14.     oCust = oReader.ReadLine
    15.     Do
    16.         oData = oCust.Split(Delim.ToCharArray)
    17.         drItem = dt.NewRow
    18.         For Index = oData.GetLowerBound(0) To oData.GetUpperBound(0)
    19.             drItem(Index) = oData(Index).ToString
    20.         Next
    21.         dt.Rows.Add(drItem)
    22.         drItem = Nothing
    23.         oCust = oReader.ReadLine
    24.     Loop Until oCust Is Nothing
    25. End Sub

    I hope this helps you, I'm trying to make the time to connect using an ODBC text driver connection to the file, but that's one thing I have not yet done in .NET so give me a few...

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