Search code examples
sql-servervb.net

"Type character does not declare data type" error


I am trying to view an image in a picturebox from a stored path in a SQL Server table column. I am getting error messages:

Type character '& does not declare data type 'String'

comma, ')" or a valid expression continuation expected

')' expected

I searched YouTube and had no luck.

  • Table Data
  • Column Path

Form Info

TextBox - txtpicPath
PictureBox - PicBox1

Code to load is

Private Sub Showimage1()
    If Con.State = ConnectionState.Open Then
        Con.Close()

    End If
    Con.Open()

    Dim cmd As New SqlCommand("Select Path from Data where id = '" & TxtPicPath.Text&"'"
    Dim img As String = cmd.ExecuteScalar()
        Dim pathstring As String = Path.Combine(img)
        PicBoxStaff.Image = Image.FromFile(pathstring)

        Con.Close()
End Sub

Can someone please point me in the right direction?


Solution

  • You have lost spaces near the second & and parenthesis at the end in the line

    Dim cmd As New SqlCommand("Select Path from Data where id = '" & TxtPicPath.Text&"'"
    

    It should be

    Dim cmd As New SqlCommand("Select Path from Data where id = '" & TxtPicPath.Text & "'")
    

    With the space missing, you have accidentally used a deprecated VB.NET feature called "Type characters": https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/type-characters