Click here to Skip to main content
15,886,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys i want to get the number of rows who is the values is less than my labels when
i fix the values and doit like this it work fine:

        con.Open()
        cmd.CommandText = "select * from tblprodinfo where quantity<5 "

 but when i do this

What I have tried:

<pre>        con.Open()
        cmd.CommandText = "select * from tblprodinfo where quantity<'" & cl.Text & "' "
        rdr = cmd.ExecuteReader
        If rdr.HasRows Then
            While rdr.Read
                crit.Text = Val(crit.Text) + 1
            End While

        End If
        con.Close()


its not working propoerly
Posted
Updated 25-Mar-17 0:31am

Never do that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Start by checking the user input: use Integer.TryParse to convert it to an Integer value:
VB
Dim quant As Integer
If Not Integer.TryParse(cl.Text, quant) Then
    ' Report problem to user
    ...
    Return
End If
Then pass the converted value to SQL via a parameter:
VB
cmd.CommandText = "SELECT * FROM  tblprodinfo WHERE quantity < @QTY"
cmd.Parameters.AddwithValue("@QTY", quant)
rdr = cmd.ExecuteReader
 
Share this answer
 
Why bother with a DataReader & manually count each row?

VB
con.Open()
   cmd.CommandText = "SELECT COUNT(*) FROM  tblprodinfo WHERE quantity < @QTY"
   cmd.Parameters.AddwithValue("@QTY", quant)
   crit.Text = cmd.ExecuteScalar.Tostring
   con.Close()
 
Share this answer
 
v2
Comments
Member 12919944 25-Mar-17 6:25am    
this code is good dude but still it give me wrong ouput :/ .
Michael_Davies 25-Mar-17 6:41am    
Define "wrong output", SQL will count the number of rows matching the condition and return that count, the output cannot therefore be wrong, if it is not what you expected then the condition needs attention.

Do not just say something is wrong, show what you expect and why and the actual result.
You should refine your question !
It is not clear what work and what don't :it work fine and its not working propoerly.
My guess is that you should remove the single quotes "'" in you second query.

Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 

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