Results 1 to 3 of 3

Thread: using parameters in query string [resolved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    using parameters in query string [resolved]

    Hi,

    My error states that I should be declaring @checkBook. I am assuming I am declaring it in [RED] so where's the mistake pls???

    Also do I need to use an adapter in here cos I had a search and found this method but I am assuming I need to state my connection somewhere right?? How's that??

    Also if someone knows of any good link where I could learn more about parameters please send me link.

    many thanks


    Code:
    Private Sub CurrentBalanceShowD()
            Dim cmd As New SqlCommand
    
            Try
                cmd.Parameters.AddWithValue("@checkBook", SqlDbType.Int).Value = cboBookD.SelectedValue            cmd.Parameters.AddWithValue("@checkBook", bookFID)
    
    
                MyConn.Open()    'opening the connection
                cmd = New SqlCommand("Select stockBalance from tb_bookbalance where bookFID = @checkBook", MyConn) 'executing the command and assigning it to connection 
    
                txtcurrentD.Text = (cmd.ExecuteScalar()).ToString   'displaying the data from the table
                MyConn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
                MyConn.Close()
            End Try
        End Sub
    Last edited by angelica; Jun 7th, 2008 at 10:54 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: using parameters in query string [resolved]

    You're doing the same thing here as you were doing in another thread I replied to. You're assigning to the same variable twice, thereby simply discarding the first object. First you create a SqlCommand:
    vb Code:
    1. Dim cmd As New SqlCommand
    then you add a parameter to it:
    vb Code:
    1. cmd.Parameters.AddWithValue("@checkBook", SqlDbType.Int).Value = cboBookD.SelectedValue
    then you simply discard that command and create a new one:
    vb Code:
    1. cmd = New SqlCommand("Select stockBalance from tb_bookbalance where bookFID = @checkBook", MyConn)
    The first command is simply thrown away, and the parameter with.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: using parameters in query string [resolved]

    Yes,

    Fixed that . Working lovely now. Mille grazie

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