Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
i am using visual studio 2010 and sql server 2008..
My problem is that how can i write the following queries using parameterized query

VB
Call openconnection()
       Dim temp As String
       temp = "pending for client approval"
       Dim a As Integer
       str = "select Count(*) a from Mastertable where CaseStatus='" & temp & "'  "
       cmd = New SqlCommand(str, cn)
       dr = cmd.ExecuteReader
       If dr.Read Then
           tb_countNumber.Text = dr.Item(a).ToString
       End If
       cn.Close()


Can i write this code using parameterized query and for that what changes should i have to do..?
Thankx in advance..
Posted

This is a very common question, and the answer can easily be found on google.[^]

Basically it will look something like this:
VB
str = "select Count(*) a from Mastertable where CaseStatus=@parmCaseStatus"
cmd = New SqlCommand(str, cn)
cmd.Parameters.Add(New SqlParamater("@parmCaseStatus", temp))

Then, instead of a reader you can use the cmd.ExecuteScalar like Kuthuparakkal suggested in the other solution. like this

VB
Dim strResult as String = cmd.ExecuteScalar()
 
Share this answer
 
Comments
Maciej Los 18-Sep-12 12:24pm    
Yep, 5!
ExecuteScalar will do...
VB
tb_countNumber.Text = Convert.ToString(cmd.ExecuteScalar())
 
Share this answer
 
Comments
Kuthuparakkal 18-Sep-12 10:41am    
why downvote, plz add some comments!
Kschuler 18-Sep-12 12:28pm    
Oops...I mean to give that a 3 stars not 2. I fixed. And it was just because the question was how to do parameters. I agree that the execute scalar is a lot easier to use than the reader.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900