• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Undo/delete ' (single quote) in a textbox

Hi,
 I have a multiline textbox, and i store the content of the textbox in a sql server DB. But problem arises when someone types ' (single quote). What i want to do is pop up a msgbox and then delete the ' (single quote) but keep the other content untouched.
I tried to use keypress event handler with textbox.undo property but the event undoes all text but the ' (single quote).
Below is my code:

Private Sub txtDescription_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDescription.KeyPress
        If e.KeyChar = ("'"c) Then
            txtDescription.undo
            MsgBox("Error: Single Quote is not Allowed in this field!", MsgBoxStyle.OKOnly, " ' Not Allowed")
            Exit Sub
        End If
End Sub
Is there any other way to disable single quotes in the whole form??

Thanks in advance
0
jhstinson
Asked:
jhstinson
1 Solution
 
flavoCommented:
You should still be able to store the '

Please post your code that sends the data to the db

Dave
0
 
jhstinsonAuthor Commented:
 below is the code i use to store the values.
DBMS is sql server.


sql = "Insert into Administrator (IncidentNumber,.....,  Description, .....) Values('" & txtIncident.Text & "', .....,'" & txtDescription.Text & "',..... "')"
               Dim cmd As New SqlCommand
                cmd.Connection = con
                cmd.CommandText = sql
                cmd.ExecuteNonQuery()

It will be great if i could store the ' . but still is there a way to restrict the typing of '.
thanks
0
 
flavoCommented:
I would use a Stored proc to do it... but with your inline query...

you need to make the ' = ''' for it to work

ie
txtIncident.Text.Replace("'", "'''")

Dave
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
flavoCommented:
thats Replace("singlequote", "3xsinglequote")
0
 
S-TwilleyCommented:
you can restrict the typing of ' in a textbox by blocking it on the key events but.. then you've also got to think about when someone pastes some text in from the clipboard which contains the ' character.

How often do you store the lines in into the database?

If it's on a button press or when you exit the textbox, try using the validate event to make sure the textbox contents is ok
0
 
burakiewiczCommented:
in order to use the ' in a sql command for the sql server you have to make it into a parameter

try this


txt Lname.text = O'Neil

MyCommand.Parameters.Add("@Lname", SqlDbType.NVarChar, 50)
MyCommand.Parameters.Item("@Lname").Value = strLname or txtLname.text
 

my command.commandtext="SELECT * FROM Customers WHERE lname=@Lname"

in the query you no longer need the ' ' around strings, and it will accept a ' in the value
0
 
arif_eqbalCommented:
This is How you can stop a Single quote being entered in a TextBox
 
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = "'" Then
            e.Handled = True
            MsgBox("Single Quotes Not Allowed")
        End If
    End Sub



And this is how you can store a Single Quote in the DataBase

Dim Cn As New SqlClient.SqlConnection(SQLConn)
        Dim Qry As String
        Qry = String.Format("Insert into Customers (CustomerID,CompanyName) values ('{0}','{1}')", "ID001", "A Single '' Quoted String")
       'NOTE THERE ARE TWO SINGLE QUOTES ONE WILL ACT AS THE ESCAPE CHARACTER
        Cn.Open()
        Dim Cmd As New SqlClient.SqlCommand(Qry, Cn)
        Try
            MsgBox(Cmd.ExecuteNonQuery)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Cn.Close()
        Cn.Dispose()

0
 
jhstinsonAuthor Commented:
Sorry for the delay. i accepted flavo's answer even tho it wasnt completely correct. i used 2x quotes instead of 3x quotes. 2x doesnt work.
thanks all for your reply.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now