2009-11-13 4 views
0

Comment puis-je comparer deux valeurs de chaîne dans VB.NET?Comparer les valeurs de chaîne dans VB.NET

J'ai essayé de comparer et d'égaler des fonctions, mais cela ne me donne pas le bon résultat. Ce que j'essaie de comparer est la suivante. Aussi, le code est-il correct?

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 

Répondre

2

Juste pour être absolument sûr, essayez ceci:

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 
+0

Oui. Sans le ".ToString()", il compare le champ a db au Textbox1.Text. Un champ DB! = Une chaîne. – David

5

pourquoi ne pas

If .rows(i).Item("name").ToString() = TextBox1.Text Then 
    'Other Stuff 
End If 
+0

+1 parce que cela fonctionnera aussi bien. – David

-2

Utilisez ce code:

If (String.Compare(.rows(i).Item("name").ToString(), TextBox1.Text) = 0) Then 
    ' Do something 
Else 
    ' Do something else 
End If 

Je fixe le code maintenant. Merci @Mayayad Mardini.

+0

Cela fera le "faire quelque chose d'autre" tout le temps. –

Questions connexes