|
-
Jun 24th, 2004, 12:22 PM
#1
Thread Starter
Member
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.
-
Jun 24th, 2004, 12:57 PM
#2
Lively Member
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.
-
Jun 24th, 2004, 01:15 PM
#3
Hyperactive Member
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.
-
Jun 24th, 2004, 01:27 PM
#4
Thread Starter
Member
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
-
Jun 24th, 2004, 02:03 PM
#5
Hyperactive Member
Here is a simple example that should provide a starting point for you.
VB Code:
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim dt As New DataTable("Customers")
dt.Columns.Add("firstname", System.Type.GetType("System.String"))
dt.Columns.Add("lastname", System.Type.GetType("System.String"))
dt.Columns.Add("department", System.Type.GetType("System.String"))
dt.Columns.Add("email", System.Type.GetType("System.String"))
dt.Columns.Add("phone", System.Type.GetType("System.String"))
Dim oReader As New StreamReader("C:\Customers.txt")
Dim oCust As String
Dim oData() As String
Dim Index As Int32
Dim drItem As DataRow
Do
oCust = oReader.ReadLine
oData = oCust.Split(",".ToCharArray)
For Index = oData.GetLowerBound(0) To oData.GetUpperBound(0)
drItem = dt.NewRow
drItem(Index) = oData(Index).ToString
dt.Rows.Add(drItem)
drItem = Nothing
Next
Loop Until oCust Is Nothing
End Sub
-
Jun 24th, 2004, 02:10 PM
#6
Hyperactive Member
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.
-
Jun 24th, 2004, 03:30 PM
#7
Thread Starter
Member
-
Jun 24th, 2004, 03:33 PM
#8
I would just create two custom classes
VB Code:
Option Explicit On
Public Class Person
Private m_strFirstName As String
Private m_strLastName As String
Private m_strDepartment As String
Private m_strEmail As String
Private m_strPhone As String
Public Property FirstName() As String
Get
Return m_strFirstName
End Get
Set(ByVal Value As String)
m_strFirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return m_strLastName
End Get
Set(ByVal Value As String)
m_strLastName = Value
End Set
End Property
Public Property Department() As String
Get
Return m_strDepartment
End Get
Set(ByVal Value As String)
m_strDepartment = Value
End Set
End Property
Public Property Email() As String
Get
Return m_strEmail
End Get
Set(ByVal Value As String)
m_strEmail = Value
End Set
End Property
Public Property Phone() As String
Get
Return m_strPhone
End Get
Set(ByVal Value As String)
m_strPhone = Value
End Set
End Property
Public Sub New(ByVal strFirstName As String, ByVal strLastName As String, ByVal strDepartment As String, ByVal strEmail As String, ByVal strPhone As String)
FirstName = strFirstName
LastName = strLastName
Department = strDepartment
Email = strEmail
Phone = strPhone
End Sub
End Class
Public Class PersonCollection
Inherits System.Collections.CollectionBase
' Restricts to Person types, items that can be added to the collection.
Public Sub Add(ByVal aPerson As Person)
' Invokes Add method of the List object to add a Person.
List.Add(aPerson)
End Sub
Public Sub Remove(ByVal index As Integer)
' Check to see if there is a Person at the supplied index.
If index > Count - 1 Or index < 0 Then
' If no Person exists, a messagebox is shown and the operation is
' cancelled.
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
' Invokes the RemoveAt method of the List object.
List.RemoveAt(index)
End If
End Sub
' This line declares the Item property as ReadOnly, and
' declares that it will return a Person object.
Public ReadOnly Property Item(ByVal index As Integer) As Person
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the Person type, then returned to the
' caller.
Return CType(List.Item(index), Person)
End Get
End Property
Public Sub Load(ByVal strTextFile As String)
'Loads the information in the comma delimited text file
'Format: "firstname, lastname, department, email, phone"
Dim sr As New IO.StreamReader(strTextFile)
Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
sr.Close()
sr = Nothing
For I As Integer = 0 To strFile.Length - 1
Dim strTmp() As String = Split(strFile(I), " ,")
Me.Add(New Person(strTmp(0), strTmp(1), strTmp(2), strTmp(3), strTmp(4)))
Next
'Job Done
End Sub
End Class
Just do:
VB Code:
Dim Data as New PersonCollection
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
-
Jun 24th, 2004, 05:01 PM
#9
Thread Starter
Member
Originally posted by CyberHawke
Here is a simple example that should provide a starting point for you.
VB Code:
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim dt As New DataTable("Customers")
dt.Columns.Add("firstname", System.Type.GetType("System.String"))
dt.Columns.Add("lastname", System.Type.GetType("System.String"))
dt.Columns.Add("department", System.Type.GetType("System.String"))
dt.Columns.Add("email", System.Type.GetType("System.String"))
dt.Columns.Add("phone", System.Type.GetType("System.String"))
Dim oReader As New IO.StreamReader("C:\Customers.txt")
Dim oCust As String
Dim oData() As String
Dim Index As Int32
Dim drItem As DataRow
Do
oCust = oReader.ReadLine
oData = oCust.Split(",".ToCharArray)
For Index = oData.GetLowerBound(0) To oData.GetUpperBound(0)
drItem = dt.NewRow
drItem(Index) = oData(Index).ToString
dt.Rows.Add(drItem)
drItem = Nothing
Next
Loop Until oCust Is Nothing
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.
-
Jun 25th, 2004, 06:44 AM
#10
Hyperactive Member
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:
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim dt As New DataTable("Customers")
dt.Columns.Add("firstname", System.Type.GetType("System.String"))
dt.Columns.Add("lastname", System.Type.GetType("System.String"))
dt.Columns.Add("department", System.Type.GetType("System.String"))
dt.Columns.Add("email", System.Type.GetType("System.String"))
dt.Columns.Add("phone", System.Type.GetType("System.String"))
Dim oReader As New StreamReader("C:\bin\Customers.txt")
Dim oCust As String
Dim Delim As String = ","
Dim oData() As String
Dim Index As Int32
Dim drItem As DataRow
oCust = oReader.ReadLine
Do
oData = oCust.Split(Delim.ToCharArray)
drItem = dt.NewRow
For Index = oData.GetLowerBound(0) To oData.GetUpperBound(0)
drItem(Index) = oData(Index).ToString
Next
dt.Rows.Add(drItem)
drItem = Nothing
oCust = oReader.ReadLine
Loop Until oCust Is Nothing
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|