0

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;")
        MyConnection.Open()
        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
        rdr.Close()


        MyConnection.Close()
    End Sub

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

Incorrect syntax near

What is it that am missing?

Edited by Reverend Jim: changed PULATE to POPULATE in title

3
Contributors
2
Replies
9
Views
1 Year
Discussion Span
Last Post by Reverend Jim
Featured Replies
  • 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" Read More

1

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 by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.