views:

70

answers:

1

Using VB.Net and SQL Server

I want to compare the textbox value with table row value by using if condition

Code

If textbox1.text = cmd
cmd = New SqlCommand("Select name from table1", con) Then
cmd.ExecuteNonQuery()

cmd = SqlCommand

The above code is showing error in if condition. I don't know how to compare the textbox value with table row value by using if condition.

What is the proper way to use if condition.

+3  A: 

It seems like you're trying to compare a textbox to a database value. You need to retrieve the value from the database first.

cmd = New SqlCommand("Select name from table1", con)
Name = cmd.ExecuteScalar()

If textbox1.Text.Equals(Name) Then
   ... do something

Edit If you want to match against multiple names, it's probably best to put it as a sql query

Dim Name as object

cmd = New SqlCommand("Select top 1 name from table1 where name = @name", con)
cmd.Parameters.AddWithValue("@name", textbox1.Text);
Name = cmd.ExecuteScalar()

if Name = textbox1.Text Then
    ... do something
Jimmy
@Jimmy. Dim Name as string or ?
Gopal
Suppose I have n number of names, So all the names will save in "Name". Or I have to use while loop.
Gopal
oh, for multiple names, it would be a little bit different. I'll update my answer
Jimmy
`Textbox1.Text.Equals(Name)` – or `TextBox1.Text = Name` for short.
Konrad Rudolph
@Jimmy - top 1 name it will select only one name or all the name, because i want to compare with all the names. if it is equal or not
Gopal
@Konrad Rudolph: thanks :) I wasn't sure of VB.NET behavior for operator resolution. @Gopal: the top 1 is there to let you know if any of the names matched. It actually checks all the names.
Jimmy