It took me ages to figure this one so I said i would share. Just a note its much better to avoid using sql directly from asp e.g. “select * from table 1 where name =’” & name & ‘” or “insert into table1 (name) values (‘” & name & “’). User Stored procedures and parameters
Problem
When passing a single quote to select statement e.g. username = O’Neill
Solution
1. Create a stored procedure in SQL
CREATE PROCEDURE GetUserDetails(@value nvarchar(100))
as
SELECT *
FROM dbo.users
where username like '%' + @value + '%'
2. Setup 2 Global functions (can use replace but I like to know exactly whats going on
(a) works when performing insert
Public Function RemoveQuote(ByVal TheString As String)
RemoveQuote = ""
Dim z
z = Split(TheString, "'")
If UBound(z) > 0 Then
Dim i As Integer
Do While i <= UBound(z)
RemoveQuote = RemoveQuote & "'" & z(i) & "'"
i = i + 1
Loop
Else
RemoveQuote = "'" & TheString & "'"
End If
End Function
(b) works when performing select
Public Function RemoveQuoteForSelect(ByVal TheString As String)
RemoveQuoteForSelect = ""
Dim z
z = Split(TheString, "'")
If UBound(z) > 0 Then
Dim i As Integer
Do While i <= UBound(z)
RemoveQuoteForSelect = RemoveQuoteForSelect & "'" & z(i) & "'''"
i = i + 1
Loop
Else
RemoveQuoteForSelect = "'" & TheString & "'"
End If
End Function
Step 3
From asp.net here is the select
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim cnn As Data.SqlClient.SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("myConnString").ConnectionString)
cnn.Open()
Dim reader As Data.SqlClient.SqlDataReader
Dim SQL As String
Dim x As New Globals
Dim un = x.RemoveQuoteForSelect(Me.TextBox1.Text)
SQL = "GetUserDetails " & un
Dim cmd As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand(SQL, cnn)
reader = cmd.ExecuteReader()
If reader.HasRows() Then
reader.Read()
Me.TextBox1.Text = reader(2)
End If
reader.Close()
cnn.Close()
End Sub
Step 4
From asp.net here is the insert (setup a parameter insert procedure) just reference the global variable like this
Dim x as new Globals
cmd.Parameters.Add("@Username", SqlDbType.VarChar, 255).Value = x.RemoveQuote(me.txtUsername.text)
Of Course you will need to perform checks etc this is just the main steps
Happy Coding
Problem
When passing a single quote to select statement e.g. username = O’Neill
Solution
1. Create a stored procedure in SQL
CREATE PROCEDURE GetUserDetails(@value nvarchar(100))
as
SELECT *
FROM dbo.users
where username like '%' + @value + '%'
2. Setup 2 Global functions (can use replace but I like to know exactly whats going on
(a) works when performing insert
Public Function RemoveQuote(ByVal TheString As String)
RemoveQuote = ""
Dim z
z = Split(TheString, "'")
If UBound(z) > 0 Then
Dim i As Integer
Do While i <= UBound(z)
RemoveQuote = RemoveQuote & "'" & z(i) & "'"
i = i + 1
Loop
Else
RemoveQuote = "'" & TheString & "'"
End If
End Function
(b) works when performing select
Public Function RemoveQuoteForSelect(ByVal TheString As String)
RemoveQuoteForSelect = ""
Dim z
z = Split(TheString, "'")
If UBound(z) > 0 Then
Dim i As Integer
Do While i <= UBound(z)
RemoveQuoteForSelect = RemoveQuoteForSelect & "'" & z(i) & "'''"
i = i + 1
Loop
Else
RemoveQuoteForSelect = "'" & TheString & "'"
End If
End Function
Step 3
From asp.net here is the select
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim cnn As Data.SqlClient.SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("myConnString").ConnectionString)
cnn.Open()
Dim reader As Data.SqlClient.SqlDataReader
Dim SQL As String
Dim x As New Globals
Dim un = x.RemoveQuoteForSelect(Me.TextBox1.Text)
SQL = "GetUserDetails " & un
Dim cmd As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand(SQL, cnn)
reader = cmd.ExecuteReader()
If reader.HasRows() Then
reader.Read()
Me.TextBox1.Text = reader(2)
End If
reader.Close()
cnn.Close()
End Sub
Step 4
From asp.net here is the insert (setup a parameter insert procedure) just reference the global variable like this
Dim x as new Globals
cmd.Parameters.Add("@Username", SqlDbType.VarChar, 255).Value = x.RemoveQuote(me.txtUsername.text)
Of Course you will need to perform checks etc this is just the main steps
Happy Coding