Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i create a dataset from a fixed length file?

Status
Not open for further replies.
Oct 15, 2003
145
US
I have a file that has certain information in it that I need to pull out to create a drop down list. I think what I need to do is - read the file, then put it in a dataset. I'm not sure how to do this....I've looked up information on schema.ini and streamreader class - but I'm not understanding how to do this. Any explanations would be very helpful. No one in my office knows VB.Net or how to do this.

It's a fixed length file, I have SQL Server and I'm using VB.Net.
 
wyld,

Why not just read it as text (assuming it is) and

Dim v As String ' where v represents what you read from the file
v = "firstitem"
ComboBox1.Items.Add(v)
v = "seconditem"
ComboBox1.Items.Add(v)

phan
 
Phantom - that's a good thought - however I don't want everything that's in the file to be in the combo box. Only one part of it

example of the file:

UNAME1 123456 GARBAGE GARBAGE 00 GARBAGE
UNAME2 12ABCD GARBAGE TRASH 00 GARBAGE

-- What I want is if UNAME1 is logged in - then display in the drop down box 123456 if UNAME2 is logged in - then display 12ABCD. I have found how to get the username from the machine - but I wanted to create a SQL table with the information and build the combo box from the table.

Does that make sense? Is there a better way of doing this?
 
This code should do the trick for you. Let me know if you need any help with it.



Dim sr As IO.StreamReader
Dim outputString As String
Dim exists As Boolean = False 'Set to true if data file exists
Try
'If data file exists, then read it.
If IO.File.Exists("filename") Then
Dim data() As String 'Holds the data read from the file
sr = IO.File.OpenText("filename")


'Peed will check for the end of the file
Do While (sr.Peek() <> -1)


'Split the first line of the data file using the separator
data = sr.ReadLine().Split(&quot; &quot;c)

'Using your example your lines would be split like this

'UNAME1 123456 GARBAGE GARBAGE 00 GARBAGE
'data(0) would hold UName
'data(1) would hold 123456
'data(2) would hold Garbage
'data(3) would hold Garbage
'data(4) would hold 00
'data(5) would hold Garbage

'you could check your User against data(0) and if there is a match you could then add
' The rest like so

'Listbox1.Items.Add(data(1))
'Listbox1.Items.Add(data(2))
'Listbox1.Items.Add(data(3))
'Listbox1.Items.Add(data(4))
'Listbox1.Items.Add(data(5))




Loop
exists = True

End If
Catch exc As IO.IOException
'Display error message and set flag to False


Catch
'Display error message and set flag to False


Finally
'Close the reader no matter what happens above
Try
sr.Close()
Catch
End Try
End Try



DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

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

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Im not sure how to use the split function. In the example given:

data = sr.ReadLine().Split(&quot; &quot;c)

this would be splitting it by a space right? (what's the c for?) The file I'm using is fixed length - not delimited by anything. So I could technically have:

UName1 A CDE blah blah
UName2 12 AB blah blah

where I want 1 CDE put in the drop down box- so how do I split the line when it is fixed length - I'm thinking I have to use the substring property - is there a better way?

Thanks!!!
 
wyld,

Sub in fixed lengths for the split function:

data(0)=mid(inputstring,position, len)
data(1)=mid(inputstring,nextposition, nextlen)
etc.......

phan
 
If you cannot change it from fixed to comma delimited. (This would be the easyiest for you) then you will have to use substring to cut out the parts.

Dim sr As IO.StreamReader
Dim outputString As String
Dim exists As Boolean = False 'Set to true if data file exists
Try
'If data file exists, then read it.
If IO.File.Exists(&quot;filename&quot;) Then
Dim data As String 'Holds the data read from the file
sr = IO.File.OpenText(&quot;filename&quot;)


'Peed will check for the end of the file
Do While (sr.Peek() <> -1)


'Read the string
data = sr.ReadLine()
' You will need to change the substring start and length
Dim UName As String = data.Substring(0, 17)
Dim Field1 As String = data.Substring(18, 5)
Dim Field2 As String = data.Substring(24, 6)
Dim Field3 As String = data.Substring(31, 7)
Dim Field4 As String = data.Substring(39, 6)
Dim Field5 As String = data.Substring(36, 8)



'you could check your User against UName and if there is a match you could then add
' The rest like so

'Listbox1.Items.Add(Field1)
'Listbox1.Items.Add(Field2)
'Listbox1.Items.Add(Field3)
'Listbox1.Items.Add(Field4)
'Listbox1.Items.Add(Field5)




Loop
exists = True

End If
Catch exc As IO.IOException
'Display error message and set flag to False


Catch
'Display error message and set flag to False


Finally
'Close the reader no matter what happens above
Try
sr.Close()
Catch
End Try
End Try


DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

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

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Thanks Doc,

I always forget substring. Is it more efficient than the mid function or about the same?

I'm thinking that it should be:
dim username as string
username=textbox1.text
data = sr.ReadLine()
'You will need to change the substring start and length
Dim UName As String = data.Substring(0, 17)
if username =trim(uname) then
Dim Field1 As String = data.Substring(18, 5)
Dim Field2 As String = data.Substring(24, 6)
Dim Field3 As String = data.Substring(31, 7)
Dim Field4 As String = data.Substring(39, 6)
Dim Field5 As String = data.Substring(36, 8)
end if

No sense parsing the rest if not the user record.

phan
 
I am not sure which is more efficient but I think it is probabaly negligent.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

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

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Thanks guys! I got it working - I found in one of the many books I'm reading that mid has been replaced in .net with Substring. You can still use mid - but it's not recommended.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top