-
Dec 16th, 2017, 06:02 PM
#1
Thread Starter
Junior Member
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
-
Dec 16th, 2017, 06:13 PM
#2
Re: File Formating
Can you post the contents of the textfile? I need to see how it's laid out.
-
Dec 16th, 2017, 06:20 PM
#3
Thread Starter
Junior Member
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
-
Dec 16th, 2017, 07:17 PM
#4
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:
Imports System.IO
Public Class AdminRecord
Public Property AdminID As String
Public Property Forename As String
Public Property Surname As String
Public Property Password As String
Public Property DateOfBirth As String
Public Property ContactNumber As String
Public Property EmergencyContact As String
Public Property AddressOne As String
Public Property AddressTwo As String
Public Property PostCode As String
Public Property HourSalary As Double
End Class
Public Class AdminDataAccess
Public Shared Sub WriteRecords(ByVal fileName As String, ByVal admins As IList(Of AdminRecord))
Using fs As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)
Using bw As New BinaryWriter(fs, System.Text.Encoding.UTF8)
bw.Write(admins.Count)
For Each rec As AdminRecord In admins
bw.Write(rec.AddressOne)
bw.Write(rec.AddressTwo)
bw.Write(rec.AdminID)
bw.Write(rec.ContactNumber)
bw.Write(rec.DateOfBirth)
bw.Write(rec.EmergencyContact)
bw.Write(rec.Forename)
bw.Write(rec.HourSalary)
bw.Write(rec.Password)
bw.Write(rec.PostCode)
bw.Write(rec.Surname)
Next
End Using
End Using
End Sub
Public Shared Function ReadRecords(ByVal fileName As String) As AdminRecord()
Dim records As New List(Of AdminRecord)
Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Using br As New BinaryReader(fs, System.Text.Encoding.UTF8)
'First 32 bits of the file is
'the number of records contained in the
'file
Dim numberOfRecords As Integer = br.ReadInt32
For i = 0 To numberOfRecords - 1
Dim rec As New AdminRecord
'Note, these must be read in the same
'order in which they were written and NOT
'the order in which the class is defined
rec.AddressOne = br.ReadString
rec.AddressTwo = br.ReadString
rec.AdminID = br.ReadString
rec.ContactNumber = br.ReadString
rec.DateOfBirth = br.ReadString
rec.EmergencyContact = br.ReadString
rec.Forename = br.ReadString
rec.HourSalary = br.ReadDouble
rec.Password = br.ReadString
rec.PostCode = br.ReadString
rec.Surname = br.ReadString
records.Add(rec)
Next
End Using
End Using
Return records.ToArray
End Function
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:
'
Dim adminList As New List(Of AdminRecord)
Dim a As New AdminRecord
a.AddressOne = "122 Kess Street"
a.AddressTwo = "N/A"
a.AdminID = "GH-9090"
a.ContactNumber = "566-9011"
a.DateOfBirth = "01/02/1977"
a.EmergencyContact = a.ContactNumber
a.Forename = "Jim"
a.HourSalary = 90.12
a.Password = "jimotega112"
a.PostCode = "12223"
a.Surname = "Otega"
adminList.Add(a)
a.AddressOne = "134 Blue Nights Drive"
a.AddressTwo = "6676 Hunter Ave"
a.AdminID = "GH-9091"
a.ContactNumber = "455-9090"
a.DateOfBirth = "09/15/1980"
a.EmergencyContact = a.ContactNumber
a.Forename = "Kayn"
a.HourSalary = 90.12
a.Password = "01kayn"
a.PostCode = "88971"
a.Surname = "Hanson"
adminList.Add(a)
'Write the admin records to a file
AdminDataAccess.WriteRecords("C:\ADMINS.DAT", adminList)
Dim adminsLoaded As AdminRecord()
'Reads the admin records from file.
adminsLoaded = AdminDataAccess.ReadRecords("C:\ADMINS.DAT")
-
Dec 17th, 2017, 09:10 AM
#5
Thread Starter
Junior Member
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.
-
Dec 17th, 2017, 11:04 AM
#6
Re: File Formating
Originally Posted by CreatorAxor
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?
-
Dec 17th, 2017, 11:40 AM
#7
Thread Starter
Junior Member
Re: File Formating
I managed to fix it now, however how does this way help read data to a datagridview?
-
Dec 17th, 2017, 12:27 PM
#8
Re: File Formating
Originally Posted by CreatorAxor
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?
-
Dec 17th, 2017, 12:47 PM
#9
Thread Starter
Junior Member
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
-
Dec 17th, 2017, 01:09 PM
#10
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:
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.
-
Dec 17th, 2017, 01:25 PM
#11
Thread Starter
Junior Member
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 ?
-
Dec 17th, 2017, 01:31 PM
#12
Re: File Formating
Originally Posted by CreatorAxor
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.
-
Dec 17th, 2017, 01:37 PM
#13
Thread Starter
Junior Member
-
Dec 17th, 2017, 01:45 PM
#14
Re: File Formating
Originally Posted by CreatorAxor
how would i do that ?
Pass the array or list of all records to WriteRecords.
-
Dec 17th, 2017, 10:15 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|