Using an apostrophe in a textbox

D

Doug

I am creating an ASP.Net application that has a textbox
populated with a store name field from a SQL Server
database. I need to allow the user to change the name
string, and update the database. In several instances,
the name will contain an apostrophe (i.e. Macy's). In
the code behind, using the assignment: str =
textbox.text, the variable will contain the string
(Macys) without the apostrophe. How do I get the
apostrophe to show up? Thanks.

Doug
 
S

susie

Use string manipulation functions
'---
Sub ChangeBox( s As Object, e As EventArgs )
Dim a As String = txtBox.Text
Dim b As String
dim c as String
dim d as String
b = right(a, 1)
c=left(a,len(a)-1)
d=c+"'"+b
txtBox.Text=d
End Sub

'---------
<asp:TextBox
ID="txtBox"
AutoPostBack="True"
onTextChanged="ChangeBox"
Runat="Server"/>

The above code should work. You will need to change to
datasource for the textbox if it is populated from SQL
server.

Good luck.
 
D

Doug

Thanks for the reply. That will work for the case I
mentioned, but there are other scenarios that are
different: A'Chen, Frank's Foods, Linens 'N Things, etc.
I cannot know how the users are going to want the store
names displayed, so I must be able to replicate an
apostrophe at any position in the string.
 
D

Doug

I looked at that but don't see how that is going to
help. Somehow I am losing the apostrophe between the
textbox and the code behind .vb page. It is missing in
the locals window for textbox.text as soon as it comes up
there. If I type in 2 apostrophes they show up in locals.
 
J

jose

so after assignment, str has Macys
without the apostrophe?
when debugging, does textbox.text contain the apostrophe?
I don't know if this will work for your case but I had
a somewhat similar problem when trying to update a text
field in a MS Access table. Access was reading the
apostrophe as a single quote and generating an error.
I used something like this to "double-up" the single quote.
string str = textbox.text.Replace("'", "''");
 
D

Doug

The apostrophe exists in the textbox, but does not in
debugging when it will be assigned to a variable. I had
written almost that exact code to double up the
apostrophe to add to my update query, but without the
first apostrophe, I can't get a second one. Weird...
 
D

Doug

It looks like either the page or project has become
corrupt. If I duplicate the functionality in a new
project, it works fine. Thanks for everyone's input.

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top