Hi guys,
am trying to populate textboxes with data from sql server database.
below is the code I am trying to use in Visual studio 2012, Vb.net.

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

        Dim query As String
        Dim MyConnection As SqlConnection = New SqlConnection("Initial Catalog=PayRoll;" & _
                                                              "Data Source=localhost;Integrated Security=SSPI;")
        query = "SELECT [Surname], [OtherNames], [NRC], [DOB] " & " FROM tblEmployees " & " WHERE EmpNo =?"

        Dim cmd As New SqlCommand(query, MyConnection)
        cmd.Parameters.AddWithValue("@badge", cmbBadgeNo.Text)
        Dim rdr As SqlDataReader = cmd.ExecuteReader()

        If rdr.Read Then
            txtSurname.Text = rdr.GetString(0)
            txtOtherNames.Text = rdr.GetString(1)
            txtNRC.Text = rdr.GetString(2)
            txtDateofbirth.Text = rdr.GetString(3)

        End If

    End Sub

When I run the program, it is displaying the following error message.

Incorrect syntax near

What is it that am missing?

Edited 1 Year Ago by Reverend Jim: changed PULATE to POPULATE in title

For SqlCommand you can't use ? for parameters. Replace it with @badge as in

query = "SELECT [Surname], [OtherNames], [NRC], [DOB] " & " FROM tblEmployees " & " WHERE EmpNo =@badge"

Edited 1 Year Ago by Reverend Jim

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