jhstinson
asked on
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(By Val sender As Object, ByVal e As System.Windows.Forms.KeyPr essEventAr gs) 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
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(By
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
thats Replace("singlequote", "3xsinglequote")
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
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
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="SELEC T * FROM Customers WHERE lname=@Lname"
in the query you no longer need the ' ' around strings, and it will accept a ' in the value
try this
txt Lname.text = O'Neil
MyCommand.Parameters.Add("
MyCommand.Parameters.Item(
my command.commandtext="SELEC
in the query you no longer need the ' ' around strings, and it will accept a ' in the value
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.KeyPr essEventAr gs) 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(SQ LConn)
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()
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPr
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(SQ
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()
ASKER
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.
thanks all for your reply.
Please post your code that sends the data to the db
Dave