Prevent 'Chr(13) + Chr(10)' from Being Executed in String

C

crjunk

I have the following code in my web page.

Dim tmpReplace As String
'Giving tmpReplace the value from the textbox on the
webform.
tmpReplace = txtComments.Text.Trim
'Prevents report from crashing when a single quote is in
string
tmpReplace = tmpReplace.Replace("'", "''")
'Replace carriage return with the appropriate CharCodes.
tmpReplace = tmpReplace.Replace("\n", "chr(13) + chr(10)")

I want to display the following text on my report: chr(13) + chr(10)
Instead what is happening is that .Net is executing the chr function
and creating a carriage return. What do I need to do to prevent the
chr function from being launched?
 
M

Michal A. Valasek

| tmpReplace = tmpReplace.Replace("'", "''")
| 'Replace carriage return with the appropriate CharCodes.
| tmpReplace = tmpReplace.Replace("\n", "chr(13) + chr(10)")
|
| I want to display the following text on my report: chr(13) + chr(10)
| Instead what is happening is that .Net is executing the chr function
| and creating a carriage return. What do I need to do to prevent the
| chr function from being launched?

It's not executing the Chr function. "\n" is not special character i VB.NET.
If you want to replace CRLF, use:

tmpReplace = tmpReplace.Replace(vbCrLf, "chr(13) + chr(10)")

But if you're doing this only for storing in database, use parameters
instead and everything - quotes, line ends etc. - would be escaped
automatically.
 
C

crjunk

I removed the "\n" from the code. The example I was using to go by
said this would remove the return, but obviously it wont. Here is what
I changed it to.

Here is code from my report options page:

'Setup user's comments in correct format so that it can be
'passed to the comment formula on the report.

Dim tmpReplace As String

tmpReplace = txtComments.Text.Trim

'Replace double quotes in the string with 2 double quotes.
tmpReplace=tmpReplace.Replace(Chr(34), Chr(34) + Chr(34))

'Replace carriage returns & line feed with double quotes &
'text that represents a return and line feed.
tmpReplace = tmpReplace.Replace(Chr(13) + Chr(10), """ +
chr(13) + chr(10) + """)

'Add double quotes to tmpReplace.
tmpReplace = """" + tmpReplace + """"

Session("UComments") = tmpReplace

This allows me to pass the text to my web form that contains my
CrystalReport Viewer. On this page, I have the following text:

oRpt.DataDefinition.FormulaFields.Item("UserComments").Text
= Trim(Session("UComments"))
CrystalReportViewer1.ReportSource = oRpt

By doing this I can pass the value to a blank formula field named
UserComments on my report. Took forever, but I finally got everything
to work.
 

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

Top