Gcode
Gcode

Reputation: 81

Replace single quote to double using vb.net

I really need to know this, it is possible to address all the textbox in vb.net and replace single quotes to double quotes? because im having problem on inserting data in ms access.. this is my current code:

Public Module Functions
    Public Sub singleqoute()
        Dim ctrl As Control
        Dim txt As TextBox

        For Each ctrl In ManageItems.Controls
            If TypeOf ctrl Is TextBox Then
                txt = CType(ctrl, TextBox)
                txt.Text = txt.Text.Replace("'", "''")
            End If
        Next
        For Each ctrl In ManageBorrowers.Controls
            If TypeOf ctrl Is TextBox Then
                txt = CType(ctrl, TextBox)
                txt.Text = Replace(txt.Text, "'", "''")
            End If
        Next
        For Each ctrl In ManageTransactions.Controls
            If TypeOf ctrl Is TextBox Then
                txt = CType(ctrl, TextBox)
                txt.Text = Replace(txt.Text, "'", "''")
            End If
        Next

    End Sub

End Module

Upvotes: 2

Views: 22609

Answers (2)

Yusuf
Yusuf

Reputation: 160

Instead of ', you can write chr(39).

39 is the ASCII value of '

Upvotes: 2

Jim Wooley
Jim Wooley

Reputation: 10418

You should be able to use ControlChars.Quote in this case. Also, consider using the .Replace method on string rather than the replace operator with VB.Net:

txt.Text = txt.Text.Replace("'", ControlChars.Quote)

That being said, I suspect the reason you are trying to do the replacement is to avoid malformed SQL statements that you are creating by using string concatenation. This is a potential security issue called SQL Injection. It's better to use parameterized queries and avoid the concatenation issues.

Upvotes: 8

Related Questions