hi every 1 i am trying to make a search form . i want to display the data saved in sql server in a grid view upon hiting the search button . below is the code which i am using but i am getting an error > Conversion failed when converting the varchar value 'anees' to data type int..please can some one help or modify my code

Dim mycon As New SqlClient.SqlConnection("Data Source=SAAD-PC\SQLEXPRESS;Initial Catalog=photoshoot;Integrated Security=True")
mycon.Open()
Dim cmd As String
cmd = "select * from staff where staff.LastName=" & Val(txtEmpSearchLastName.Text)
Dim da As New SqlDataAdapter(cmd, mycon)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count = 0 Then
MsgBox("no records found.")
End If
DataGridView1.DataSource = dt
mycon.Close()
End Sub

Why are you trying to convert the value in txtEmpSearchLastName.Text to an int? I'm assuming it is a string. Change your code to:

cmd = "select * from staff where staff.LastName=" txtEmpSearchLastName.Text

basically what i wana do is i want to display all the result of staff table which is searched by last name. but when i use the above code its giving me error. is it possible if you could fix the code. so that it displays all field values into gridview. my data base contains two int values as emp_id and telephone.

And also when i use this code the one you just given me

cmd = "select * from staff where staff.LastName=" txtEmpSearchLastName.Text
it gives me an error.
column name doesnot exist. although i am giving the correct column name and correct value

If it says the column name does not exist then it doesn't. Proper capitalization matters so you will need to check it is not lastName.

This article has been dead for over six months. Start a new discussion instead.