Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i have the following SQL statement to search for records in a table. how should i convert it into a parameterised statement to prevent sql injection attacks? thanks

VB
    con.Open()
        Dim da As New SqlDataAdapter("Select * from Students " & _
           "where student_id like '%" & Me.srcTxt.Text.Trim & "%' " & _
           "or " & _
         "student_firstname like '%" & Me.srcTxt.Text.Trim & "%' " & _
         "or " & _
       "student_lastname like '%" & Me.srcTxt.Text.Trim & "%'", con)

       da.Fill(ds)
       con.Close()

For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
      Dim lvi As New ListViewItem
      lvi.Text = ds.Tables(0).Rows(i)(0).ToString()
           For j As Integer = 1 To ds.Tables(0).Rows(i).ItemArray.Length - 1
               lvi.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())
           Next
       lvw.Items.Add(lvi)
 Next
Posted

Read this: MSDN[^]
 
Share this answer
 
C#
public sub Page_Load()

Me.DoFunction(Me.srcTxt.Text);//call the parametarized method
end sub


public sub DoFunction(ByVal text as String)
con.Open()
                    Dim da As New SqlDataAdapter("Select * from Students " & _
    		 "where student_id like '%" & text & "%' " & _
                       "or " & _
                     "student_firstname like '%" & text & "%' " & _
                     "or " & _
                   "student_lastname like '%" & text & "%'", con)
           
                   da.Fill(ds)
                   con.Close()
end sub
 
Share this answer
 
v4

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