Search code examples
sqlvb.netsql-server-ce

VB.NET single quotation not accepted by query


I have a database class. And I'm using SQLServerCe which will run all commands. I put my query into a string variable q. And pass into the function of Database class. I have a problem. All values are accepted. But when I put single quotation into textbox. Then it gives an error.....

Here is my code :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim q As String = "insert into student_classes (student_class) values ('" & TextBox1.Text & "')" 
    db.execute(q) 
    MsgBox("Record Added Successfully") 
    TextBox1.Clear() 
End Sub

Solution

  • I use this function on all field assignments that may contain '

    Function EscapeQuote(ByVal msData As Object) As String
        Return (Replace(msData, "'", "''"))
    End Function
    
    e.g.  values ('" & EscapeQuote(TextBox1.Text) & "')"