-
Oct 17th, 2017, 01:12 PM
#1
Thread Starter
New Member
HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
This is a fingerprint verification application. When verifying,it loops through the table. if the current fingerprint template is the first one on the table row it verifies it correctly. But if the current fingerprint template is located on the 2nd or later rows, it loops through the table and at the first row it rejects the template as not verified, but subsequently after looping through the rows the fingerprint is verified, if indeed it has been stored in the table. My question is how can l loop through all the rows in the table and before l can get the result l desire if verified or not. Below is the code snippet. Please help
Code:
Private Sub VerifyBiometric_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim sqlCommand As MySqlCommand = New MySqlCommand()
Dim cn As New MySqlConnection(ConnectString())
cn.Open()
sqlCommand.CommandText = "Select * from ecms.fingerprint"
sqlCommand.CommandType = CommandType.Text
sqlCommand.Connection = cn
Dim sqlCommand As MySqlCommand = New MySqlCommand()
Dim cn As New MySqlConnection(ConnectString())
cn.Open()
sqlCommand.CommandText = "Select * from ecms.fingerprint"
sqlCommand.CommandType = CommandType.Text
sqlCommand.Connection = cn
Dim dataSet As DataSet = New DataSet()
Dim adapter As MySqlDataAdapter = New MySqlDataAdapter(sqlCommand)
Dim dt As New DataTable
Dim dr As DataRow
Try
adapter.Fill(dt)
For Each dr In dt.Rows
Dim bytes As Byte() = Nothing
bytes = dr.Item("FingerP")
Dim template = New DPFP.Template()
template.DeSerialize(bytes)
'Perform match
matcher.Verify(FeatureSet, template, matchResult)
If matchResult.Verified Then
EventHandlerStatus = Gui.EventHandlerStatus.Success
MessageBox.Show("Verified!")
Exit For 'success
End If
If Not matchResult.Verified Then EventHandlerStatus = Gui.EventHandlerStatus.Failure
MessageBox.Show("Verification failed, User does not exist")
'Exit For 'failure
Next
Finally
adapter.Dispose()
cn.Close()
End Try
End Sub
Last edited by Shaggy Hiker; Oct 17th, 2017 at 03:49 PM.
Reason: Added CODE tags.
-
Oct 17th, 2017, 03:55 PM
#2
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
I edited the post to add [CODE][/CODE] tags, which you can do by pressing the # button and pasting the code between the resulting tags.
One thing you will want to do sooner or later (so make it sooner) is to turn Option Strict ON. You are doing a bunch of implicit conversions, which aren't safe. Some will always work, such as dr as the iterator in the For loop. That will be type Object, but it HAS to be a datarow, so the fact that it gets implicitly converted whenever it is used will only slow things down a bit. Other implicit conversions can hide subtle bugs.
As to the question. What you need to do is have a Boolean variable outside of the loop. If a match is found, set the Boolean to true (and exit the loop if you want). If it isn't found, go on to the next row. When the loop has finished, the Boolean will be true if a match was found, or false if one was not found. At that time, you can say whether or not success was achieved. Prior to the completion of the loop, you can never be sure whether the next record would provide a match, so you really can't say what the overall outcome will be.
My usual boring signature: Nothing
-
Oct 18th, 2017, 02:33 PM
#3
Lively Member
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
Originally Posted by Shaggy Hiker
One thing you will want to do sooner or later (so make it sooner) is to turn Option Strict ON. You are doing a bunch of implicit conversions, which aren't safe. Some will always work, such as dr as the iterator in the For loop. That will be type Object, but it HAS to be a datarow, so the fact that it gets implicitly converted whenever it is used will only slow things down a bit. Other implicit conversions can hide subtle bugs.
I'm not trying to derail this thread but if I could ask a question about this. I'm also not looking to start an argument either, it's something that I probably should know. For whatever reason I don't. Why do you have to turn on Option Strict at all? If it's so important to have it on, why does VB default to having it off? Why can it even be turned off?
-
Oct 18th, 2017, 02:57 PM
#4
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
That's a question that I'd kind of like to know the answer to, as well. You can change the options in VS such that it is on by default for all new projects, but why isn't it on by default anyways, and why is it necessary?
You do need to be able to turn Option Strict OFF, but only rarely. It has to be turned off to use late binding, which has some significant advantages when working with things like Office components (excel, Word, etc..). However, in those cases you turn it off just for the code page that needs to use late binding by adding Option Strict OFF to the top of the page. Otherwise, you leave it on. Late binding is rare, but when it is necessary, there's no way around it.
As for why it is off by default....I can only speculate....no, actually, I'm not sure that I can even speculate. Some things were done to make VB.NET appear more familiar to people coming from VB6, and this may be one of those things, but I don't remember VB6 acting like Option Strict OFF. Maybe it did, I just don't recall.
VB also has Option Explicit, which is ON by default, and there is NEVER any good reason to turn that one off, so it would be fair to ask why it was even included, except that Option Explicit existed back in VB6, whereas I'm not even sure that Option Strict existed back then.
So, I really don't know why Option Strict isn't on by default. Perhaps MS thought it made the language more accessible, but I'm just making things up at this point.
My usual boring signature: Nothing
-
Oct 18th, 2017, 03:01 PM
#5
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
Originally Posted by Garrett!
I'm not trying to derail this thread but if I could ask a question about this. I'm also not looking to start an argument either, it's something that I probably should know. For whatever reason I don't. Why do you have to turn on Option Strict at all? If it's so important to have it on, why does VB default to having it off? Why can it even be turned off?
It should be on by default, but because VB for far so long had it off, people got used to it being off, and wrote code accordingly. Sadly, it's led to some very sloppily written code that relies on the looseness of it being off. When .NET rolled around, it became worse as people tried to simply update their code w/o really updating it, relying once again on it being off by default... and well... now here we are. Those that wanted it off by default were louder and more noisier than those that wanted it left on by default. That said, you can turn it off, because there are some cases where you'll want it off, but those should be rare cases. Vary rare.
BTW: You can set it to default to on for future projects, forcing you to explicity turn it off by setting OPTION STRICT OFF at the top of the code module where you need it off... that'll contain it to just that group of code and no where else.
-tg
-
Oct 18th, 2017, 03:05 PM
#6
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
Originally Posted by Garrett!
I'm not trying to derail this thread but if I could ask a question about this. I'm also not looking to start an argument either, it's something that I probably should know. For whatever reason I don't. Why do you have to turn on Option Strict at all? If it's so important to have it on, why does VB default to having it off? Why can it even be turned off?
I agree with how dumb this feels. It's a pain every VB dev has to carry.
Short answer: MS is very dogmatic about maintaining compatibility with older code.
Longer answer:
VBA behavior, and maybe VB6 behavior, worked as if Option Strict is off. (I think maybe VB6 didn't even have Option Strict? I'm not sure.) MS wanted these developers to be comfortable when they moved to VB .NET, and also wanted to make sure as much of their code worked as-is as possible, so the decision was made when adding Option Strict that it would be Off by default.
This seems much sillier nearly 15 years later. Most professional developers turn it on and assume it's on, but almost any student or new user will have it off. I can't imagine there's still an awful lot of people moving their projects forward, and it seems like it'd be OK to ask them to toggle the option off if they need it. But it is what it is.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Oct 19th, 2017, 01:49 PM
#7
Lively Member
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
Thanks for the replies, guys. So it basically sounds like the answer I give my wife when she asks me why they do something they way the do in baseball. "Because they've always done it that way."
-
Oct 19th, 2017, 01:57 PM
#8
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
Sort of... in this case it's because there was a small VERY NOISY minority that cried foul when things tried to be changed. Fun Fact - Default form instances did NOT exist in the first couple versions of VB.NET... until that small noisy bunch made themselves heard an in VB2005, it was added back in, and millions of VB developers cried out "WHAYYYYY?" And that's why we still have problems with form default instances to this day.
-tg
-
Oct 19th, 2017, 03:53 PM
#9
Re: HOW TO LOOP THROUGH ROWS IN A TABLE in vb.net
The best part is it was always, "Oh, I'll convert my project from VB6 to VB .NET if you just make default instances."
So MS did. Then they didn't.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
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
|