Hi Guys,
Kindy assist me to make this code work. I want the datagridview to populate data from SQL server 2012 database based on the employment number selected in the combobox. below is the code I have tried to generate.

Private Sub btnRecords_Click(sender As Object, e As EventArgs) Handles btnRecords.Click
        Using MyConnection As SqlConnection = New SqlConnection("Initial Catalog=PayRoll;" & _
                                                              "Data Source=localhost;Integrated Security=SSPI;")

            Using cmd As New SqlCommand("SELECT * " & " FROM tblTimesheet " & " WHERE EmpNo=@empno", MyConnection)
                cmd.Parameters.AddWithValue("@empno", cmbBadge.Text)

                cmd.CommandType = CommandType.Text

                Using sda As New SqlDataAdapter(cmd)

                    Using dt As New DataTable()

                        'Set AutoGenerateColumns False
                        dtgDeployment.AutoGenerateColumns = False

                        'set the number of columns for the control
                        dtgDeployment.ColumnCount = 8

                        'Add columns to the control
                        dtgDeployment.Columns(0).Name = "EmpNo"
                        dtgDeployment.Columns(0).HeaderText = "BADGE NUMBER"
                        dtgDeployment.Columns(0).DataPropertyName = "EmpNo"

                        dtgDeployment.Columns(1).Name = "Surname"
                        dtgDeployment.Columns(1).HeaderText = "SURNAME"
                        dtgDeployment.Columns(1).DataPropertyName = "Surname"

                        dtgDeployment.Columns(2).Name = "Othernames"
                        dtgDeployment.Columns(2).HeaderText = "OTHER NAMES"
                        dtgDeployment.Columns(2).DataPropertyName = "Othernames"

                        dtgDeployment.Columns(3).Name = "Shift"
                        dtgDeployment.Columns(3).HeaderText = "SHIFT"
                        dtgDeployment.Columns(3).DataPropertyName = "Shift"

                        dtgDeployment.Columns(4).Name = "Depdate"
                        dtgDeployment.Columns(4).HeaderText = "DATE"
                        dtgDeployment.Columns(4).DataPropertyName = "Dedate"

                        dtgDeployment.Columns(5).Name = "Location"
                        dtgDeployment.Columns(5).HeaderText = "LOCATION"
                        dtgDeployment.Columns(5).DataPropertyName = "Location"

                        dtgDeployment.Columns(6).Name = "Month"
                        dtgDeployment.Columns(6).HeaderText = "MONTH"
                        dtgDeployment.Columns(6).DataPropertyName = "Month"

                        dtgDeployment.Columns(7).Name = "Year"
                        dtgDeployment.Columns(7).HeaderText = "Year"
                        dtgDeployment.Columns(7).DataPropertyName = "Year"

                        dtgDeployment.DataSource = dt

                    End Using
                End Using
            End Using
        End Using
    End Sub

But when I run this code it is encoutering an error message "The data types text and nvarchar are incompatible in the equal to operator."

Thanks in advance

1 Year
Discussion Span
Last Post by mikeybware

I would try changing this line
Using cmd As New SqlCommand("SELECT * " & " FROM tblTimesheet " & " WHERE EmpNo=@empno", MyConnection)

Using cmd As New SqlCommand("SELECT * FROM tblTimesheet WHERE EmpNo='" & @empno & "'", MyConnection)

where there are single quotes around your variable, since it is a string.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.