Last post Aug 23, 2008 05:57 AM by haggis999
Aug 20, 2008 11:51 AM|haggis999|LINK
In a VB.Net code loop in my ASP.NET 3.5 app I want to select a specific row from a text file and save it in a database record. I would be incrementing the row number with each successive pass through the loop. My searches to find suitable code have failed.
Can anyone please help me out on the VB code required to do this?
If it makes the coding any easier, I could save the text file in a variety of different formats (probably via Save As in Excel). Any assistance would be greatly appreciated.
Aug 20, 2008 02:21 PM|BlueJam|LINK
The following will read a text file line by line:-
sBuffer = oReader.ReadLine
Aug 20, 2008 02:45 PM|haggis999|LINK
Thanks for the suggestion. I had already found code (though slightly different to yours) that would read a text file line by line and I might be able to create a solution that uses such code. However, I was really hoping to find code that would let me select
a specific line from the text file so that I had total control over which line is used during each iteration of my main loop.
Aug 20, 2008 03:01 PM|BlueJam|LINK
You could use a similar approach to the one described in this thread:-
This would allow you to run sql select statements against a excel file to select specific lines of text.
Aug 20, 2008 05:33 PM|haggis999|LINK
I had a look at your referenced thread but I didn't understand your final post there ("You basically wrap the data you want to query in a named data range (Insert Menu -> Name -> Define) and then you can select the info using a SQL style select statement
I've no idea what you meant by "Insert Menu -> Name -> Define". What software are you using? I'm using Visual Web Developer 2008 Express Edition, which doesn't appear to have an Insert Menu. I also don't know what a named data range is, and Googling this
phrase didn't lead me anywhere useful. I'm an ASP.NET newbie so a lot of things confuse me....
Aug 21, 2008 06:10 AM|BlueJam|LINK
You're right, the last post wasn't very clear, sorry about that.
You'd need to set up the data range up in the excel file you are trying to connect too. The Insert menu is in the MS Excel software. A data range is basically a selection of cells in a excel worksheet you can highlight and give a name to, you can then refer
to the range in excel formulas or programatically.
If you feed the words "excel range" into google you should be able to find some pages with tutorials to run through.
Aug 21, 2008 06:52 AM|haggis999|LINK
Ah, I didn't realise you were talking about Excel in that post. While your solution may well do the job, it is a somewhat involved process with dependencies on the configuration of other software, i.e. Excel, rather than just using the text file. I'm slightly
surprised that no solution can be found entirely within the VB.Net coding.
As it happens, I went ahead last night with a solution that uses StreamReader coding similar to your own for reading the text file line by line and placed my For Each dataset loop within the While loop for reading the text file. A test version of this (that
displayed relevant info rather than updating my database) worked up to a point but had serious problems. The loop for the text file continued until the last line of the text file. However, my Linq to SQL dataset had fewer records than the text file and I ended
up with a lot of redundant data.
I fixed this problem by commenting out the While and End While statements (lines 12 and 16 below) so that the code would stop running as soon as it finished stepping through the dataset. However, this was a very crude solution. Is there any way to retain
the While loop (to enable the Peek test) but exit from it as soon as the For Each loop ends? In other words, I am looking for a way for the code to come to an end as soon as the first of the two loops finishes.
Using myDbContext As CompDataSQLDataContext = New CompDataSQLDataContext()
Dim EntrantsWithUsername = From ent In myDbContext.Entrants Where ent.UserName IsNot Nothing Select ent
'Open a file for reading
Dim PwdList As String = Server.MapPath("App_Data\Random phonetic passwords.txt")
'Get a StreamReader class that can be used to read the file
Dim objStreamReader As StreamReader
objStreamReader = File.OpenText(PwdList)
'While objStreamReader.Peek() <> -1
For Each Entrant In EntrantsWithUsername
Entrant.InitPassword = objStreamReader.ReadLine()
Aug 21, 2008 08:54 AM|BlueJam|LINK
Would it be acceptable to generate random passwords on the fly or do they need to come from that text file?
Aug 21, 2008 12:19 PM|haggis999|LINK
I have hundreds of users of an earlier system that will be replaced by my new ASP.NET application. In order to retain the links with historical data, I need to pre-register these people and will email them their usernames and passwords for the new system.
My original plan was to auto-generate passwords using Membership.GeneratePassword(6,0) but a test showed that even with the zero setting for the minimum number of special characters in these passwords they were still riddled with all sorts of crazy characters.
Secure, yes, but not at all user friendly!
Since I don't require cast-iron security controls on my website, I used the Random Phonetic Password Generator at
http://themolotov.net/projects/passwords/ to create a long list of much less frightening passwords. These were transferred to the text file under discussion in this thread. I generated far more passwords
than were actually needed because, at that stage, I didn't know the exact number of users that were involved.
As my current requirement is a one-off, I will probably be able to muddle through with the code quoted above. However, on the basis that such code might prove useful in the future for another purpose, I am still interested to know:
Aug 22, 2008 07:29 AM|BlueJam|LINK
1) Not that I know of. You could create yourself a helper function that took a file name and a line number as parameters and then looped through the file until the appropriate line and passed back the resulting string.
2) The following might work :-
For Each Entrant
If Not (objStreamReader.EndOfStream) Then
Entrant.InitPassword = objStreamReader.ReadLine()
The problem with that is, if you have more entrants than passwords it will leave a bunch of records with blank InitPassword fields.
Aug 23, 2008 05:23 AM|haggis999|LINK
I think old age must be dimming my brain, because your suggested solution to my loop problem is really quite simple! Thanks for pointing it out.
Your assistance over the past few days has been much appreciated. I will now get on with developing my app - and will no doubt hit more problems very soon....
Aug 23, 2008 05:41 AM|BlueJam|LINK
No problem, sorry to be cheeky but could you mark my post as being an answer.
Aug 23, 2008 05:57 AM|haggis999|LINK
Done. Apologies for not doing it immediately.