I am currently creating a win form based tool in Visual Studio 2019 that reads data from a SQL database. I am having difficulty when pulling a field from the form I am working in into the sqlCommand query.
This is part of my VB script. Note that this works fine without the Surname like filter, it also works if i write it with a hard-coded surname, such as 'Adams'. I can also get the same logic to work using variables directly in SQL. The message pop-up displays 'Adams' as expected but nothing is returned in the data-grid.
Screen-shots below of the result of running the form as desired and when hard-coded to 'Adams'.
Thanks in advance for any help :)
Public Class Form4
Public Sub BtnFetchAdastraUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFetchAdastraUser.Click
Dim connetionString As String = "Data Source=xxxxxx;Initial Catalog=xxxxxx;User ID=xxxxx;Password=xxxxx"
Dim dt As New DataTable()
Using connection As New SqlConnection(connetionString)
Dim command As New SqlCommand(
"select
[u].[UserRef],
[u].[UserName],
[u].[FullName]
from dbo.[Users][u]
where
[Obsolete] = 0
and [Surname] like '%" + Surname.Text + "%'", connection
)
command.Connection.Open()
Dim sqlAdaptr As New SqlDataAdapter(command)
Dim ds As New DataTable
sqlAdaptr.Fill(ds)
DataGridView1.DataSource = ds
MsgBox(Surname.Text)
command.Connection.Close()
End Using
End Sub
End Class


