Results 1 to 11 of 11

Thread: [RESOLVED] Need an Expert! ExecuteReader haywire

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Resolved [RESOLVED] Need an Expert! ExecuteReader haywire

    Good morning!

    I have been staring at this code for two days now and I absolutely cannot figure out why it throws an error at bomRdr = bomCmd.ExecuteReader. The error thrown is: No value given for one or more required parameters. However, I have tried inserting a messagebox before the ExecuteReader to show all necessary values at runtime and everything looks good. Furthermore, most of this code was copy and pasted from another of my subroutines that functions perfectly. Please help!

    Code:
    Sub CheckForSufficientInventory(ByRef proceed As Boolean, ByRef bomList As StringCollection, _
            ByRef qtyList As StringCollection)
    
            ' CHECK STRINGCOLLECTION ARRAY AGAINST INVENTORY
            ' PROCEED IF SUFFICIENT INVENTORY OR
            ' NOTIFY USER OF DEFICIENCIES
            ' GIVE OPTION TO OVERRIDE
    
            Dim canBuild As Integer
            Dim itemsShort As Integer = 0
            Dim itemNumber As Integer
            Dim itemToFind As String
            Dim buildQty As Integer = Integer.Parse(Me.uiQtyTextBox.Text)
            Dim totalCanBuild As Integer = buildQty
            Dim bomConString As String = _
                "Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
                "Source=….mdb;" & _
                "Jet OLEDB:Database Password=******;"
    
            ' CREATE CONNECTION AND OPEN
            bomCon = New OleDbConnection(bomConString)
            bomCon.Open()
    
            Try
    
                ' CHECK INVENTORY
                For itemNumber = 1 To bomList.Count
    
                    ' CREATE COMMAND AND SET PARAMETERS
                    itemToFind = Convert.ToString(bomList.Item(itemNumber - 1))
                    bomCmd = New OleDbCommand("SELECT Inventory.invMain FROM invMain WHERE (PartName = @comp)", bomCon)
                    bomCmd.Parameters.Add("@comp", OleDb.OleDbType.Char)
                    bomCmd.Parameters("@comp").Value = itemToFind
    
                    ' EXECUTE COMMAND
                    bomRdr = bomCmd.ExecuteReader
    
                    While bomRdr.Read
    
                        Dim invQty As Integer = Integer.Parse(Convert.ToString(bomRdr(0)))
    
                        ' COMPARE QUANTITIES
                        If invQty < Integer.Parse(qtyList.Item(itemNumber - 1)) * buildQty Then
                            itemsShort = itemsShort + 1
                            canBuild = invQty \ Integer.Parse(qtyList.Item(itemNumber - 1))
                            If canBuild < totalCanBuild Then
                                totalCanBuild = canBuild
                            End If
                        End If
                    End While
    
                    ' CLOSE READER FOR NEXT ITEM
                    bomRdr.Close()
    
                Next itemNumber
    
                If itemsShort > 0 Then
                    MessageBox.Show("Insufficient inventory exists for " & itemsShort & " item(s)" & vbCrLf & _
                                    "at the selected quantity. You can build " & totalCanBuild & vbCrLf & _
                                    "units with current inventory levels.")
                    proceed = False
                Else
                    proceed = True
                End If
    
                bomCon.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                proceed = False
            End Try
    
        End Sub

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Need an Expert! ExecuteReader haywire

    you need to set the connection for bomCmd
    VB Code:
    1. bomCmd.Connection = New OleDbConnection(bomConString)
    2. bomCmd.Connection.Open()

    This is what i use to return a set with SQL Server, however the principles should be the same

    VB Code:
    1. Dim oCmd As SqlClient.SqlCommand
    2.                 Dim intVal As Int32 = -1
    3.                 Dim dr As SqlClient.SqlDataReader
    4.  
    5.                 oCmd = New SqlClient.SqlCommand
    6.  
    7.                 Try
    8.                     With oCmd
    9.                         .Connection = New SqlClient.SqlConnection(connString)
    10.                         .Connection.Open()
    11.                         .CommandType = CommandType.Text
    12.                         .CommandText = strSQL
    13.                         dr = .ExecuteReader
    14.                     End With
    15.                     dr.Read()
    16.                     userID = dr.Item(0)
    17.                     userLevel = dr.Item(1)
    18.                     dr.Close()
    19.                     getUserInfo = True
    20.                 Catch ex As Exception
    21.                     intVal = -1
    22.                 Finally
    23.                     If Not oCmd Is Nothing Then
    24.                         oCmd.Dispose()
    25.                     End If
    26.                 End Try
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Need an Expert! ExecuteReader haywire

    The connection is set through bomCon and then related at the end of the bomCmd = statement. Is this not syntactically correct?

    bomCon = Connection
    bomConString = Connection String
    bomCmd = Command

  4. #4

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Need an Expert! ExecuteReader haywire

    It also appears as though you are using VB 6. Is this correct?

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Need an Expert! ExecuteReader haywire

    no, I am using VB 2005, and you need to define the connection for the command. It the reader has no defined connection it will just error. In your example you are opening a connection, but you are not telling the command what connection to use.
    Last edited by bmahler; Sep 25th, 2006 at 09:31 AM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Need an Expert! ExecuteReader haywire

    I defined the connection through statement bomCmd = New OleDbCommand (Select Statement, Connection) as shown in code.

  7. #7
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Need an Expert! ExecuteReader haywire

    try this

    VB Code:
    1. For itemNumber = 1 To bomList.Count
    2.                 Dim bomCmd As OleDb.OleDbCommand
    3.  
    4.                 ' CREATE COMMAND AND SET PARAMETERS
    5.                 itemToFind = Convert.ToString(bomList.Item(itemNumber - 1))
    6.                 bomCmd.Connection = New OleDb.OleDbConnection(bomConString)
    7.                 bomCmd.Connection.Open()
    8.                 bomCmd.CommandType = CommandType.Text
    9.                 bomCmd.CommandText = "SELECT Inventory.invMain FROM invMain WHERE (PartName = @comp)"
    10.                 bomCmd.Parameters.Add("@comp", OleDb.OleDbType.Char)
    11.                 bomCmd.Parameters("@comp").Value = itemToFind
    12.  
    13.                 ' EXECUTE COMMAND
    14.                 bomRdr = bomCmd.ExecuteReader
    15.  
    16.                 While bomRdr.Read
    17.  
    18.                     Dim invQty As Integer = Integer.Parse(Convert.ToString(bomRdr(0)))
    19.  
    20.                     ' COMPARE QUANTITIES
    21.                     If invQty < Integer.Parse(qtyList.Item(itemNumber - 1)) * buildQty Then
    22.                         itemsShort = itemsShort + 1
    23.                         canBuild = invQty \ Integer.Parse(qtyList.Item(itemNumber - 1))
    24.                         If canBuild < totalCanBuild Then
    25.                             totalCanBuild = canBuild
    26.                         End If
    27.                     End If
    28.                 End While
    29.  
    30.                 ' CLOSE READER FOR NEXT ITEM
    31.                 bomRdr.Close()
    32.                 bomCmd.Dispose()
    33.  
    34.             Next itemNumber
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  8. #8
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Need an Expert! ExecuteReader haywire

    What is your error when it errors?
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  9. #9

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Need an Expert! ExecuteReader haywire

    I tried adding your code from above and still get the same error. This leads me to believe that both syntax formats, my original and your suggested, are correct.

    The error I get is "No value given for one or more required parameters". However, if I add a messagebox that prints to screen all possible parameters, I get good values.

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Need an Expert! ExecuteReader haywire

    The error seems to point to a problem in your SQL statement. By that I mean that the Inventory.invMain FROM invMain WHERE (PartName = @comp) is wrong. If PartName is the correct field name in the DB then is invMain a field in Inventory. Shouldn't the paramter field be inclosed withing single qoutes if you are testing for a string value.

    Also post the SQL value that is being passed as it is rexaclty in the app.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Need an Expert! ExecuteReader haywire

    MY HERO! The problem was the single quotes around my parameter. THANK YOU BOTH!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width