tags:

views:

90

answers:

3

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
+2  A: 

Just to be absolutely sure, try this:

with ds.Tables("patientprofile")
         For Each dr In .Rows
             if String.Equals(.rows(i).Item("name").ToString(), TextBox1.Text) then
                   textbox1.text=.rows(i).item("age").ToString()
             End If
             i = i + 1
         Next
      end with
Matthew Jones
Yes. Without the ".ToString()" it's comparing the a db field to the Textbox1.Text,. A DB Field != a string.
David Stratton
+4  A: 

why not

If .rows(i).Item("name").ToString() = TextBox1.Text Then
  'Other Stuff
End If
Scott Anderson
+1 because this will work as well.
David Stratton
A: 

How about

If (String.Compare(.rows(i).Item("name"), TextBox1.Text) = 0) Then
   ' do something
Else
  ' do Something Else
End If
DevByDefault
This will do the "do Something Else" all the time.
Moayad Mardini
Why. I am running this code and seems to work fine to me. If (String.Compare("abc", "abc") = 0) Then MessageBox.Show("Doing something") Else MessageBox.Show("Doing Something else") End If
DevByDefault