Compare two strings

Joined
May 22, 2015
Messages
5
Programming Experience
Beginner
I have a text box that I can input multiple lines of numbers in. I want to compare each line of numbers against my dataset to find a match. If it matches it should update my database.

My pseudo code should be something like this-
Get input from textbox
Index number of inputs from text box
Get input from dataset
Index number of inputs from data set
Compare text box input to all inputs from dataset
Continue to compare all inputs from text box to all inputs from dataset until the text box max index/upper bound is reached.


My actual code is a mess and I know it needs to be fixed but....

For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            Dim dsstring As String = Integer.Parse(ds.Tables(0).Rows(i)(0).ToString())
            For Each strline As String In TxtTicketIn.Text.Split(vbNewLine)
                If StrComp(strline, dsstring) = True Then
                    da.Fill(ds, "MAIN")
                    ds.Tables("MAIN").Rows(n).Item("TBP") = "True"
                    da.Update(ds, "MAIN")
                    da.UpdateCommand = cb.GetUpdateCommand()
                End If
            Next
        Next i


I am not a professional developer, just a sysadmin. This is my fist time working with a variable length/multiline text box. Can someone suggest some techniques so I can keep track of the index of the text box input? I think if I can get that part worked out I can fix my logic on the looping through the database to compare.
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,331
Location
Norway
Programming Experience
10+
TxtTicketIn.Text.Split(vbNewLine)
Do you need to do this operation for each row in table? No? Then do this first, before loops:
Dim lines = TxtTicketIn.Text.Split(vbNewLine)

Now you have an array that you loop through with a For loop just like your rows loop, there you have your index.
 
Joined
May 22, 2015
Messages
5
Programming Experience
Beginner
Do you need to do this operation for each row in table? No? Then do this first, before loops:
Dim lines = TxtTicketIn.Text.Split(vbNewLine)

Now you have an array that you loop through with a For loop just like your rows loop, there you have your index.
OK, thanks for the feedback.

Here's what I am trying to do (and maybe I didn't good a good job explaining)- input numbers into the text box (represented by column a).
"A" should check for a match against my dataset (Column b/dsstring) and update the checkbox ("TBP") in my database/dataset if it matches. So I will have to loop through the dataset in col B every time we go to a new number from my input (col a)

Basically start on 1341189 loop through all 10 from B to compare
go to 1171415 and loop through all 10 from B to compare
go to 1171413 etc.

COL A
1341189
1171415
1171413
1070911

COL B
1070911
1070895
1070572
1341189
1519792
1171415
1171484
1171517
1171456
1171413

It is finding the match on the first loop through with 1341189 and updating the correct check box in my dataset. On the next match 1171415 my code is dropping through to next and not updating anything even though it matches. I did update my code. As of right now it looks like this-

Code:
For Each strline As String In TxtTicketIn.Text.Split(vbNewLine)
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dsstring As String = Integer.Parse(ds.Tables(0).Rows(i)(0).ToString())
                If strline = dsstring Then
                    da.Fill(ds, "MAIN")
                    ds.Tables("MAIN").Rows(i).Item("TBP") = "True"
                    da.Update(ds, "MAIN")
                    da.UpdateCommand = cb.GetUpdateCommand()
                End If
            Next
        Next
I hope this is a little more clear.
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

I would suggest that what you are doing there is a bit inefficient since you fill a DataSet for every iteration of the For Loop as well as create an update command using a CommandBuilder. If I were doing this I would have a single Update Command which uses a Where clause with the correct number of Parameters to accommodate each of the Numbers in the TextBox that needs to be updated to True. Have a Look at this example which uses SQL Server and assumes that the field to be Updated is a Bit (Boolean) field and the field to be Searched on is an Int (Integer) field.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Using sqlConn As New SqlConnection("Data Source=IANLAPTOP\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"), _
      sqlCmnd As New SqlCommand("UPDATE YourTableName SET TBP = 1 Where ID IN ({0})", sqlConn)
 
    'Create the SQL Query With the Correct Number of Parameters
    For counter As Integer = 0 To TextBox1.Lines.Count - 1
      Dim currentParam As String = String.Format("@Param{0}", counter) & " ,{0}"
      sqlCmnd.CommandText = String.Format(sqlCmnd.CommandText, currentParam)
    Next
    sqlCmnd.CommandText = String.Format("{0})", sqlCmnd.CommandText.Substring(0, sqlCmnd.CommandText.Length - 6))
 
    'Populate the Parameters of the Query
    For counter As Integer = 0 To TextBox1.Lines.Count - 1
      sqlCmnd.Parameters.AddWithValue(String.Format("@Param{0}", counter), TextBox1.Lines(counter))
    Next
 
    'Execute the Query
    sqlConn.Open()
    Dim rowsUpdated As Integer = sqlCmnd.ExecuteNonQuery
    sqlConn.Close()
 
    'Show how many rows were updated
    MsgBox(rowsUpdated)
  End Using
End Sub


Hope that helps.

Cheers,

Ian
 
Joined
May 22, 2015
Messages
5
Programming Experience
Beginner
@Ian-
Thanks that is helpful and I see what you are saying but I'm not sure that's my main problem right now. I'm trying to get the program to work as expected first and then make corrections for efficiency, security after the fact. I still don't know why my code is dropping to the next statement when it reaches the 2nd match? Or are you saying it is doing that because of the way I have it coded? Can you clarify? Thanks!
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Since you have asked the question I have taken a bit of time to find out where your specific issue is. The issue is with this statement:-

For Each strline As String In TextBox1.Text.Split(vbNewLine)


I do not know the specific reason why this is the issue other than to state that "vbNewLine" is an old, legacy VB6 constant which seems to interfere with the results of splitting a MiltiLine Textbox (which is already split by a CR/LF character). By using this code (and using example numbers) you will find that your "strline" variable is actually " 2" instead of "2" whereby the first character of the variable is a LineFeed (I think) character which means your comparison fails. If you add a breakpoint to this line:-

If strline = dsstring Then


And check the value of the variables you will then see what I am saying. If you stick with the Textbox.Lines Property and get rid of the Split Method then you should be fine.

So, knowing that, please do take to time to understand how many other issues you have with that small bit of code. There are many to mention if I had the time.

Good luck and happy coding.

Cheers,

Ian
 
Top Bottom