none
problem with execute reader in VB visual studio

    Question

  • hi

    when I run the following code I get an error where I declare dr the data reader

    on line 'dr = cmd.ExecuteReader()' I am getting the following error:

    System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.'

    just wondering if anyone knows how to solve this?

    Private Sub carFinder(b As String, m As String, s As String, d As String, e As String)
            myConnection.ConnectionString = My.Forms.Main.connString
            myConnection.Open()
            Dim match As String
            match = "SELECT * FROM CxDetails WHERE (CBrand = '" & b & "' and CModel = '" & m & "' 
                    and CSpec ='" & s & "'and CNoOfDoors = '" & d & "' and CEngineSize ='" & e & "')"
            Dim cmd As OleDbCommand = New OleDbCommand(match, myConnection)
            dr = cmd.ExecuteReader()
            myConnection.Close()
    
    
            Dim CO2e As String = dr("CCO2")
            Dim COTR As String = dr("COTR")
    
    
    

    Tuesday, April 04, 2017 9:50 AM

All replies

  • I don't see the problem, but use a little bit more from todays VB, I changed your code in that style, of course not tested, I don't have your database. 

        Private Sub carFinder(b As String, m As String, s As String, d As String, e As String)
            Dim CO2e As String
            Using myConnection.ConnectionString = My.Forms.Main.connString
                myConnection.Open()
                Dim match As String
                match = "SELECT  * FROM CxDetails WHERE (CBrand = @b and CModel = @m  
                    and CSpec = @s and CNoOfDoors = @d and CEngineSize = @e)"
                Using cmd As New OleDb.OleDbCommand(match, myConnection)
                    cmd.Parameters.AddWithValue("@b", b)
                    cmd.Parameters.AddWithValue("@m", m)
                    cmd.Parameters.AddWithValue("@s", s)
                    cmd.Parameters.AddWithValue("@d", d)
                    cmd.Parameters.AddWithValue("@e", e)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        CO2e = dr("CC2E") 'be aware this is case sensitive
                        'The rest
                    End While
                End Using
            End Using
        End Sub
    Therefore watch typeos and small mistakes in the code above. 


    Success
    Cor


    Tuesday, April 04, 2017 10:14 AM
  • hi I checked for typos and checked everything over in my code, that is all fine but im still running the error any other ideas?

    many thanks

    Tuesday, April 04, 2017 10:37 AM
  • actually now sorry ive stopped running the error but I am not getting any values returned from the match query, even though there is one record in the database, does it matter if they aren't next to one another in the database? or do the have to be entered in exactly the same order??

    also doe this line

                While dr.Read
                    CO2e = dr("CCO2")

    take the value from the chosen record and read to the cco2 value and save the cars emission value to the variable name CO2e?

    • Edited by coust99 Tuesday, April 04, 2017 10:48 AM
    Tuesday, April 04, 2017 10:41 AM
  • Did you try my code?

    In the deleted message Viorel noted that your (way shown in your question) can be caused by special  characters as the question mark. 

    I assume he deleted it because my reply covered this as well. 


    Success
    Cor


    Tuesday, April 04, 2017 11:29 AM