vb.net string value comparision

  • Thread starter Thread starter Preeti
  • Start date Start date
P

Preeti

How can i compare two string values in vb.net? i've tried compare, equals
functions but it's not giving me the correct result. what i'm trying to
compare is as follows. also let me know if the code is correct. i'm new to
vb.net

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Try
con.ConnectionString = "Provider=SQLOLEDB;Data
Source=HP-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=dbname"
con.Open()
Catch
MsgBox("Error in connection")
End Try
Dim da As OleDb.OleDbDataAdapter
Dim sql As String sql = "select * from patientprofile"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "patientprofile")
Dim dr As DataRow
Dim i As Integer
i = 0
with ds.Tables("patientprofile")
For Each dr In .Rows
if String.Equals(.rows(i).Item("name"), TextBox1.Text) then
textbox1.text=.rows(i).item("age")
End If
i = i + 1
Next
end with
End Sub
End Class
 
How can i compare two string values in vb.net? i've tried compare, equals
functions but it's not giving me the correct result. what i'm trying to
compare is as follows. also let me know if the code is correct. i'm new to
vb.net

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Try
con.ConnectionString = "Provider=SQLOLEDB;Data
Source=HP-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=dbname"
con.Open()
Catch
MsgBox("Error in connection")
End Try
Dim da As OleDb.OleDbDataAdapter
Dim sql As String sql = "select * from patientprofile"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "patientprofile")
Dim dr As DataRow
Dim i As Integer
i = 0
with ds.Tables("patientprofile")
For Each dr In .Rows
if String.Equals(.rows(i).Item("name"), TextBox1.Text) then
textbox1.text=.rows(i).item("age")
End If
i = i + 1
Next
end with
End Sub
End Class

String.Equals, the = operator, and str.Equals(otherStr) all perform a
combination of a reference compare and an ordinal case and culture sensitive
comparison of the string value. Which leads me to believe that one or the
other of your strings differs in case or character count (white space on the
end of your db field?)...

You might want to try something like:

If String.Compare (.Rows(i).Item("name").ToString().Trim(), TextBox1.Text.Trim(), True) = 0 Then

....
End If

Looking, you probably have option strict off... Yikes. But, try using Trim
and a non-case sensitive compare as above....
 
How can i compare two string values in vb.net? i've tried compare,
equals functions but it's not giving me the correct result.

A couple of common things:

1. You are not trimming the strings and one has a space at the end.
2. They are not 100% equivalent, as one has at least one letter of a
different case. If so, do a case insensitive compare.

Peace and Grace,
Greg

--
Vote for Miranda's Christmas Story
http://tinyurl.com/mirandabelieve

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Preeti said:
How can i compare two string values in vb.net? i've tried compare, equals
functions but it's not giving me the correct result. what i'm trying to
compare is as follows. also let me know if the code is correct. i'm new to
vb.net
i = 0
with ds.Tables("patientprofile")
For Each dr In .Rows
if String.Equals(.rows(i).Item("name"), TextBox1.Text) then
textbox1.text=.rows(i).item("age")
End If
i = i + 1
Next
end with
End Sub
End Class

That should work...

Other comments:
Why do you have dr, but use rows (i) in the loop? Better yet, just select
on the name in the textbox in your sql select statement.

It looks like your code is:

For each row in a table
If (the name value of the row equals some text box) then
Change the text box to the age field of the row

-- That means that all subsequent rows are checked
-- if their name equals the first results age, which is
-- not likely what you want)

endif
incriment i
next row of db
 
Back
Top