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
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:
Dim cmd As New SqlCommand
then you add a parameter to it:
vb Code:
cmd.Parameters.AddWithValue("@checkBook", SqlDbType.Int).Value = cboBookD.SelectedValue
then you simply discard that command and create a new one:
vb Code:
cmd = New SqlCommand("Select stockBalance from tb_bookbalance where bookFID = @checkBook", MyConn)
The first command is simply thrown away, and the parameter with.
Re: using parameters in query string [resolved]
Yes,
Fixed that . Working lovely now. Mille grazie