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?

Recommended Answers

All 2 Replies

near what?

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"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.