Results 1 to 15 of 15

Thread: File Formating

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    File Formating

    I am using random file access to store admin details. For formatting my file I have used the following code. However my records are saved in one row so that when I click the button to show the textfile data in the datagridview it shows everything in a row. Is there a way I can format it so that when there is a new ID and new admin it will go to the next line of the data grid view ?
    I am using the following code

    Code:
        With DataGridView1
                .Columns.Add("AdminID", "AdminID")
                .Columns.Add("Forename", "Forename")
                .Columns.Add("Surname", "Surname")
                .Columns.Add("Password", "Password")
                .Columns.Add("Date Of Birth", "Date Of Birth")
                .Columns.Add("Contact Number", "Contact Number")
                .Columns.Add("Emergency Contact", "Emergency Contact")
                .Columns.Add("AddressOne", "AddressOne")
                .Columns.Add("AddressTwo", "AddressTwo")
                .Columns.Add("PostCode", "PostCode")
                .Columns.Add("Hour Salary", "Hour Salary")
              
            End With
    
    
            For Each line As String In System.IO.File.ReadAllLines("C:\Users\ahmed\Documents\Visual Studio 2010\Projects\SkettySurgeryGPMedicalSystem\SkettySurgeryGPMedicalSystem\bin\Debug\Admin.dat")
                DataGridView1.Rows.Add(line.Split("AdminID;Forename;Surname;Password;DateOfBirth;ContactNumber;EmergencycContact;AddressOne;AddressTwo;PostCode;HourSalary"))
            Next

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: File Formating

    Can you post the contents of the textfile? I need to see how it's laid out.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: File Formating

    this is the text file "1111111111Ahmed AlSaied Ahmed 31/10/189907439645101017922985840 somewhere road skotty sa7 jns N 1010101010eed dddddd ddd ddd dddd ddd dd dd dd ffffff(@N 808080808011 11 11 11 11 11 11 111 11 333333(@N 0000000000test test test test test test test test test ffffff(@N 222222222222 22 22 22 22 22 22 22 22 6@N"

    As for the code
    Code:
      Private Sub AddAdmin(ByRef OneAdmin As AdminType)
            Dim NumbersOfRecords, DeletedRecordNumber As Short
            Dim Sender As System.Object
            Dim e As System.EventArgs
            'DeletedRecordNumber = FindDeletedCustomer()
            FileOpen(1, AdminFile, OpenMode.Random, , , Len(OneAdmin))
    
            If DeletedRecordNumber <> 0 Then
                FilePut(1, OneAdmin, DeletedRecordNumber)
            Else
                NumbersOfRecords = LOF(1) / Len(OneAdmin)
                FilePut(1, OneAdmin, NumbersOfRecords + 1)
    
            End If
            FileClose(1)
    
    
        End Sub
    
        Private Sub Btn_AddNewAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_AddNewAdmin.Click
    
    
            Dim OneAdmin As AdminType
            Dim AdminID As String
            Dim AdminDeleted As Boolean
            Dim Duplicate As Boolean
            Dim Responce As Short
    
            AdminFile = "Admin.dat"
            If Btn_AddNewAdmin.Text = "Add Admin" Then
                If Tbx_AdminID.Text.Length = 10 Then
                    AdminID = Tbx_AdminID.Text
                    Duplicate = ChkDpAdmin(AdminID)
    
    
                    If Not Duplicate Then
                        If (Tbx_AdminForename.Text <> "") And (Tbx_AdminSurname.Text <> "") And (Tbx_Password.Text <> "") And (Tbx_AdminDateOfBirth.Text <> "") And (Tbx_AdminContactTelephone.Text <> "") And (Tbx_EmergencyContact.Text <> "") And (Tbx_AdminAddressOne.Text <> "") And (Tbx_AdminAddressTwo.Text <> "") And (Tbx_AdminPostCode.Text <> "" And (Tbx_HourSalary.Text <> "")) Then
                            OneAdmin.AdminID = Tbx_AdminID.Text
                            OneAdmin.Forename = Tbx_AdminForename.Text
                            OneAdmin.Surname = Tbx_AdminSurname.Text
                            OneAdmin.DateOfBirth = Tbx_AdminDateOfBirth.Text
                            OneAdmin.ContactNumber = Tbx_AdminContactTelephone.Text
                            OneAdmin.AddressOne = Tbx_AdminAddressOne.Text
                            OneAdmin.AddressTwo = Tbx_AdminAddressTwo.Text
                            OneAdmin.PostCode = Tbx_AdminPostCode.Text
                            OneAdmin.Password = Tbx_Password.Text
                            OneAdmin.EmergencyContact = Tbx_EmergencyContact.Text
                            OneAdmin.HourSalary = Tbx_HourSalary.Text
                            OneAdmin.Deleted = "N"
                            Call AddAdmin(OneAdmin)
                            Tbx_AdminID.Text = ""
                            Tbx_AdminForename.Text = ""
                            Tbx_AdminSurname.Text = ""
                            Tbx_AdminDateOfBirth.Text = ""
                            Tbx_AdminContactTelephone.Text = ""
                            Tbx_AdminAddressOne.Text = ""
                            Tbx_AdminAddressTwo.Text = ""
                            Tbx_AdminPostCode.Text = ""
                            Tbx_Password.Text = ""
                            Tbx_EmergencyContact.Text = ""
                            Tbx_HourSalary.Text = ""
    
                            MsgBox("Admin Has Been Made!")
                        Else
                            MsgBox("You Have Not Filled In All Details Of The Admin")
    
    
                        End If
                    Else
                        MsgBox("Admin ID. " & AdminID & " Has Been Used. Enter A Different One")
                        Tbx_AdminID.Focus()
                    End If
                Else
                    MsgBox("Must Enther a AdminID of minimum 10 characters")
                    Tbx_AdminID.Focus()
    
                End If
            Else
                AdminID = Tbx_DeleteAdmin.Text
    
    
    
                If Tbx_AdminID.Text = "" Then
                    MsgBox("You havent entered an Admin ID")
    
                Else
                    Responce = MsgBox("Confirm you want to delete this member", vbYesNo)
                    If Responce = 10 Then
                        AdminDeleted = DeletedAdmin(AdminID)
                        Tbx_DeleteAdmin.Text = ""
                        If Not AdminDeleted Then
                            MsgBox("Admin not deleted. Admin ID" & AdminID & " Does Not Exist")
                        End If
                    End If
                End If
            End If
    
    
        End Sub
    the spacing is as follows on the module holding the structure

    Code:
    Module Module3
    
        Structure AdminType
            <VBFixedString(10)> Public AdminID As String
            <VBFixedString(20)> Public Forename As String
            <VBFixedString(20)> Public Surname As String
            <VBFixedString(20)> Public Password As String
            <VBFixedString(10)> Public DateOfBirth As String
            <VBFixedString(11)> Public ContactNumber As String
            <VBFixedString(10)> Public EmergencyContact As String
            <VBFixedString(20)> Public AddressOne As String
            <VBFixedString(20)> Public AddressTwo As String
            <VBFixedString(7)> Public PostCode As String
            <VBFixedString(10)> Public HourSalary As Double
            Dim Deleted As Char
    
    
    
        End Structure
    End Module
    Last edited by si_the_geek; Dec 17th, 2017 at 09:45 AM. Reason: fixed code tags

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: File Formating

    OMG....That FileOpen/FilePut rubbish belongs in VB6. Where did you find that horrible code? We don't write files using such archaic methods in .Net. Also, the file is not a text file as you implied.

    I'm not going to bother trying to correct that code. Instead, I'm going to show you the one correct way of doing this:-
    vbnet Code:
    1. Imports System.IO
    2.  
    3. Public Class AdminRecord
    4.     Public Property AdminID As String
    5.     Public Property Forename As String
    6.     Public Property Surname As String
    7.     Public Property Password As String
    8.     Public Property DateOfBirth As String
    9.     Public Property ContactNumber As String
    10.     Public Property EmergencyContact As String
    11.     Public Property AddressOne As String
    12.     Public Property AddressTwo As String
    13.     Public Property PostCode As String
    14.     Public Property HourSalary As Double
    15. End Class
    16.  
    17. Public Class AdminDataAccess
    18.     Public Shared Sub WriteRecords(ByVal fileName As String, ByVal admins As IList(Of AdminRecord))
    19.  
    20.         Using fs As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)
    21.             Using bw As New BinaryWriter(fs, System.Text.Encoding.UTF8)
    22.                 bw.Write(admins.Count)
    23.  
    24.                 For Each rec As AdminRecord In admins
    25.                     bw.Write(rec.AddressOne)
    26.                     bw.Write(rec.AddressTwo)
    27.                     bw.Write(rec.AdminID)
    28.                     bw.Write(rec.ContactNumber)
    29.                     bw.Write(rec.DateOfBirth)
    30.                     bw.Write(rec.EmergencyContact)
    31.                     bw.Write(rec.Forename)
    32.                     bw.Write(rec.HourSalary)
    33.                     bw.Write(rec.Password)
    34.                     bw.Write(rec.PostCode)
    35.                     bw.Write(rec.Surname)
    36.                 Next
    37.             End Using
    38.         End Using
    39.     End Sub
    40.  
    41.     Public Shared Function ReadRecords(ByVal fileName As String) As AdminRecord()
    42.         Dim records As New List(Of AdminRecord)
    43.  
    44.         Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
    45.             Using br As New BinaryReader(fs, System.Text.Encoding.UTF8)
    46.  
    47.                 'First 32 bits of the file is
    48.                 'the number of records contained in the
    49.                 'file
    50.                 Dim numberOfRecords As Integer = br.ReadInt32
    51.  
    52.                 For i = 0 To numberOfRecords - 1
    53.                     Dim rec As New AdminRecord
    54.  
    55.                     'Note, these must be read in the same
    56.                     'order in which they were written and NOT
    57.                     'the order in which the class is defined
    58.                     rec.AddressOne = br.ReadString
    59.                     rec.AddressTwo = br.ReadString
    60.                     rec.AdminID = br.ReadString
    61.                     rec.ContactNumber = br.ReadString
    62.                     rec.DateOfBirth = br.ReadString
    63.                     rec.EmergencyContact = br.ReadString
    64.                     rec.Forename = br.ReadString
    65.                     rec.HourSalary = br.ReadDouble
    66.                     rec.Password = br.ReadString
    67.                     rec.PostCode = br.ReadString
    68.                     rec.Surname = br.ReadString
    69.  
    70.                     records.Add(rec)
    71.                 Next
    72.             End Using
    73.         End Using
    74.  
    75.  
    76.         Return records.ToArray
    77.     End Function
    78.  
    79.  
    80. End Class

    The above code can be used to both read and write admin records. Here is an example of how to use them:-
    vbnet Code:
    1. '
    2.         Dim adminList As New List(Of AdminRecord)
    3.         Dim a As New AdminRecord
    4.  
    5.         a.AddressOne = "122 Kess Street"
    6.         a.AddressTwo = "N/A"
    7.         a.AdminID = "GH-9090"
    8.         a.ContactNumber = "566-9011"
    9.         a.DateOfBirth = "01/02/1977"
    10.         a.EmergencyContact = a.ContactNumber
    11.         a.Forename = "Jim"
    12.         a.HourSalary = 90.12
    13.         a.Password = "jimotega112"
    14.         a.PostCode = "12223"
    15.         a.Surname = "Otega"
    16.  
    17.         adminList.Add(a)
    18.  
    19.  
    20.         a.AddressOne = "134 Blue Nights Drive"
    21.         a.AddressTwo = "6676 Hunter Ave"
    22.         a.AdminID = "GH-9091"
    23.         a.ContactNumber = "455-9090"
    24.         a.DateOfBirth = "09/15/1980"
    25.         a.EmergencyContact = a.ContactNumber
    26.         a.Forename = "Kayn"
    27.         a.HourSalary = 90.12
    28.         a.Password = "01kayn"
    29.         a.PostCode = "88971"
    30.         a.Surname = "Hanson"
    31.  
    32.         adminList.Add(a)
    33.  
    34.         'Write the admin records to a file
    35.         AdminDataAccess.WriteRecords("C:\ADMINS.DAT", adminList)
    36.  
    37.         Dim adminsLoaded As AdminRecord()
    38.  
    39.         'Reads the admin records from file.
    40.         adminsLoaded = AdminDataAccess.ReadRecords("C:\ADMINS.DAT")
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: File Formating

    when I debug and try to input data because I set it up so that a.AddressOne = tbx_Addressone.text
    it says the following : The given path's format is not supported and highlights the following line : Using fs As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)

    As i want the admin to manually input their data. I am using two forms. One for the show on datagrid and the other to add the new admin
    Last edited by CreatorAxor; Dec 17th, 2017 at 09:22 AM.

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: File Formating

    Quote Originally Posted by CreatorAxor View Post
    when I debug and try to input data because I set it up so that a.AddressOne = tbx_Addressone.text
    it says the following : The given path's format is not supported and highlights the following line : Using fs As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)

    As i want the admin to manually input their data. I am using two forms. One for the show on datagrid and the other to add the new admin
    Given the error message I would guess that the fileName variable contains a path that isn't supported. If you look at that line in the debugger what does the variable fileName contain?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: File Formating

    I managed to fix it now, however how does this way help read data to a datagridview?

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: File Formating

    Quote Originally Posted by CreatorAxor View Post
    I managed to fix it now, however how does this way help read data to a datagridview?
    What do you mean by "read data to a datagridview"? You don't read data to anywhere. You read data from somewhere. Could you clarify your on your meaning?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: File Formating

    Currently the code above doesnt read data to anything. The trouble is not my code ( even though i am using your code now because it makes more sense ) but because I would like to output the data that i have stored to a datagridview with correct formatting with each record separated

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: File Formating

    The simplest way would be to set the DataSource property of the DataGridView to the array containing all the records like this:-
    vbnet Code:
    1. DataGridView1.DataSource = adminList

    The adminList variable in the above code could be an array or a list type object like a Collection(Of T) or List(Of T).

    There are more explicit ways to populate a DataGridView, which allow a much higher degree of control but that way is by far the simplest.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: File Formating

    This works great now however When it saves new data it whipes everything else but saves that one record. This also means it affects the datagridview as it only as well outputs 1 record as the other records have deleted for some reason , is it due to the fact that I am requesting manual input from admins using textboxes ?

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: File Formating

    Quote Originally Posted by CreatorAxor View Post
    When it saves new data it whipes everything else but saves that one record
    If it's saving one record then obviously it means you're passing only one record to WriteRecords. Even if you change one field in one record, you have to write the whole list when saving. You must always write the whole list when saving.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: File Formating

    how would i do that ?

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: File Formating

    Quote Originally Posted by CreatorAxor View Post
    how would i do that ?
    Pass the array or list of all records to WriteRecords.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: File Formating

    Please read this carefully. Don't just keep telling us that things don't work. If they don't work then it's because you're doing them wrong. If you're calling the WriteRecords method that Niya provided and it's not doing what it should then it's because you're not calling it as you should. We can tell you what to do but if you don't understand basic concepts then there's no guarantee you're going to understand the instructions. Instead of wasting all that time, you need to show us what you are doing and then we can address exactly what is wrong with that. If we see what you're doing wrong in your code then we can often determine what is wrong with your thinking and we can then correct that to ensure that you don't make similar mistakes in the future. In short, if you're going to ask us what you should do then what you are currently doing is ALWAYS relevant, so show it to us.

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