Question

i'm trying to figure out what's wrong with my code, I'm trying to extract the value of employe ID selected from a dropdown, then get all information of that employee and place it in a textbox vb.net form, but there's a error whenever i select an employee ID it's giving me an error message of : "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters."

Thus highlighting reader, i'm not sure what's the problem, i have initialized the reader... Please guide me. Thanks

Private Sub eNumText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles eNumText.SelectedIndexChanged


        Dim dbSource = "Data Source= C:\Databse\Company_db.accdb"
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"

        Dim sqlQuery As String
        Dim sqlCommand As New OleDbCommand
        Dim sqlAdapter As New OleDbDataAdapter
        Dim Table As New DataTable

        Dim empNum As String
        Dim empFname As String
        Dim empLname As String
        Dim empDept As String
        Dim empStat As String
        Dim empYears As String

        empNum = eNumText.Text
        empFname = empFnameText.Text
        empLname = empLnameText.Text
        empDept = DeptText.Text
        empStat = StatText.Text
        empYears = yearstext.Text

        sqlQuery = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = con
            .Parameters.AddWithValue("EmpID", empNum)
            With sqlAdapter
                .SelectCommand = sqlCommand
                .Fill(Table)
            End With
            With DataGridView1
                .DataSource = Table
            End With
        End With

        Dim path = "Data Source= C:\Databse\Company_db.accdb"
        Dim command = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"
        QueryData(path, Command)


        con.Close()

    End Sub
    Private Sub QueryData(PathDb As String, command As String)
        PathDb = "Data Source= C:\Databse\Company_db.accdb"
        Using connection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathDb)
            Using da As New System.Data.OleDb.OleDbCommand(command, con)
                con.Open()

                Dim reader = da.ExecuteReader()
                If reader.Read() Then
                    empFnameText.Text = reader("FirstName")
                    empLnameText.Text = reader("LastName")
                End If

                con.Close()
            End Using
        End Using
    End Sub
Was it helpful?

Solution

You need to prefix your parameter empNum with an @: @empNum. See this answer: How to pass a parameter from vb.net for an example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top