String wierdness...

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I send out a daily email to technicians regarding nightly backup logs. I
use the following code to generate the body of the email:

tmpBody += vbCrLf
tmpBody += "----- " & SupTechs.Item(j) & " (" & tmpCount & " servers
monitored) -----"
tmpBody += vbCrLf
tmpBody += <Specific Server Error Message>

However, I get emails that sometimes have the linebreak before the
Specific Server Error Message, and some that don't. Here's a sample
email:

----- npl2bjl (1 servers monitored) ----- NEOMASVR0000 (Daily Backup):
Canceled by NEOMASVR0000\US\R05D53BESvc,Error - Mount failed. User
canceled a Physical Volume Library operation.

Why is there no linebreak between "-----" and "NEOMASVR0000"? When I
look at the email in a hex editor, it's just a space. Some of the emails
look correct, such as:

----- neb1jwg (3 servers monitored) -----
NECOLSVR0005 (W Drive): Failed,Storage device "COMPAQ 1" reported an
error on a request to write data to media.Error reported:Data error
(cyclic redundancy check). Warning - A severe error occurred while
reading or writing data. This job may not have been successfully
completed.Robotic Library: Drive: COMPAQ 1Slot: A communications
failure has occurred between the Backup Exec job engine and the remote
agent.

Any ideas?
 
Not sure this will help with your problem, but I would rewrite your code like
this:

tmpBody = vbCrLf
tmpBody &= "----- " & SupTechs.Item(j) & " (" & tmpCount & " servers
monitored) -----"
tmpBody &= vbCrLf
tmpBody &= <Specific Server Error Message>

When dealing with Strings it's good practice to avoid "+" and use "&" instead
since the former is an arithmetic operator which can cause problems when there
are numbers in your string.
 
Terry Olsen said:
I send out a daily email to technicians regarding nightly backup logs. I
use the following code to generate the body of the email:

tmpBody += vbCrLf
tmpBody += "----- " & SupTechs.Item(j) & " (" & tmpCount & " servers
monitored) -----"
tmpBody += vbCrLf
tmpBody += <Specific Server Error Message>

However, I get emails that sometimes have the linebreak before the
Specific Server Error Message, and some that don't. Here's a sample
email:

I would use String.Format :)

Const BODY_FORMAT As String = _
vbNewLine & "----- {0} ({1} server{2} monitored) -----" & _
vbNewLine & "{3}"

Dim tmpBody As String = String.Format( _
BODY_FORMAT, _
SupTechs.Item(j), _
tmpCount, _
IIf(tmpCount = 1, String.Empty, "s"), _
<specific server error message here> _
)

HTH :0

Mythran
 
Terry Olsen said:
I send out a daily email to technicians regarding nightly backup logs. I
use the following code to generate the body of the email:

tmpBody += vbCrLf
tmpBody += "----- " & SupTechs.Item(j) & " (" & tmpCount & " servers
monitored) -----"
tmpBody += vbCrLf
tmpBody += <Specific Server Error Message>

However, I get emails that sometimes have the linebreak before the
Specific Server Error Message, and some that don't. Here's a sample
email:

----- npl2bjl (1 servers monitored) ----- NEOMASVR0000 (Daily Backup):
Canceled by NEOMASVR0000\US\R05D53BESvc,Error - Mount failed. User
canceled a Physical Volume Library operation.

Why is there no linebreak between "-----" and "NEOMASVR0000"? When I
look at the email in a hex editor, it's just a space. Some of the emails
look correct, such as:

----- neb1jwg (3 servers monitored) -----
NECOLSVR0005 (W Drive): Failed,Storage device "COMPAQ 1" reported an
error on a request to write data to media.Error reported:Data error
(cyclic redundancy check). Warning - A severe error occurred while
reading or writing data. This job may not have been successfully
completed.Robotic Library: Drive: COMPAQ 1Slot: A communications
failure has occurred between the Backup Exec job engine and the remote
agent.

Any ideas?

Oh, and I forgot to mention, it seems like your email viewer may be
translating the text as html, therefore removing carriage-returns and
linefeeds. Are you setting the body format type to text, specifically?

HTH again,
Mythran
 
:I send out a daily email to technicians regarding nightly backup logs. I
: use the following code to generate the body of the email:
:
: tmpBody += vbCrLf
: tmpBody += "----- " & SupTechs.Item(j) & " (" & tmpCount & " servers
: monitored) -----"
: tmpBody += vbCrLf
: tmpBody += <Specific Server Error Message>
:
: However, I get emails that sometimes have the linebreak before the
: Specific Server Error Message, and some that don't. Here's a sample
: email:
:
: ----- npl2bjl (1 servers monitored) ----- NEOMASVR0000 (Daily Backup):
: Canceled by NEOMASVR0000\US\R05D53BESvc,Error - Mount failed. User
: canceled a Physical Volume Library operation.
:
: Why is there no linebreak between "-----" and "NEOMASVR0000"? When I
: look at the email in a hex editor, it's just a space. Some of the emails
: look correct, such as:
:
: ----- neb1jwg (3 servers monitored) -----
: NECOLSVR0005 (W Drive): Failed,Storage device "COMPAQ 1" reported an
: error on a request to write data to media.Error reported:Data error
: (cyclic redundancy check). Warning - A severe error occurred while
: reading or writing data. This job may not have been successfully
: completed.Robotic Library: Drive: COMPAQ 1Slot: A communications
: failure has occurred between the Backup Exec job engine and the remote
: agent.
:
: Any ideas?
:
:


Does your mail reader strip what it considers "extra" line feeds?

Ralf
 
Oh, and I forgot to mention, it seems like your email viewer may be
translating the text as html, therefore removing carriage-returns and
linefeeds. Are you setting the body format type to text, specifically?

Yes, I figured it out. Outlook was "removing extra linefeeds in plain-text
messages." I cleared that and everything looks fine. But I do appreciate
the pointers about the strings. Would a StringWriter or StringBuilder be a
better choice? For that matter, what's the difference between the two?
 
Terry,

The first thing to do is in my idea taking the advice of Mike Lowery.

With Option Strict of your code as now can give very unpredictable results.

Cor
 
Terry said:
Yes, I figured it out. Outlook was "removing extra linefeeds in plain-text
messages." I cleared that and everything looks fine. But I do appreciate
the pointers about the strings. Would a StringWriter or StringBuilder be a
better choice? For that matter, what's the difference between the two?

A StringBuilder would be the better choise.

A StringBuilder is used to put together a string without making new
copies of the string for each operation.

The += operator might give the impression that a string is added at the
end of an existing string, but that is not so. Strings in .NET are
immutable, e.g. they can not be changed. The += operator concatenates
the strings into a new string, and the reference to the new string
replaces the original string.

For a few short strings the += operator works fine, but the larger the
string grows, the more data has to be copied for each operation. You
should use a StringBuilder when there are more than a few strings, and
always if you don't know beforehand how many strings there will be.

A StringWriter is a TextWriter wrapper around a StringBuilder. One
specific advantage of the StringWriter is when you write different data
types to it. You can specify a culture when you create the StringWriter,
and that cuture is then used to convert things like numbers and dates to
strings when you write them to the StringWriter. When you do the same
using a StringBuilder, it always uses the default culture, or you have
to specify the culture for each value you add using the AppendFormat method.
 
: >
: > Oh, and I forgot to mention, it seems like your email viewer may be
: > translating the text as html, therefore removing carriage-returns and
: > linefeeds. Are you setting the body format type to text, specifically?
:
: Yes, I figured it out. Outlook was "removing extra linefeeds in
: plain-text messages." I cleared that and everything looks fine. But
: I do appreciate the pointers about the strings. Would a StringWriter
: or StringBuilder be a better choice? For that matter, what's the
: difference between the two?


It's my understanding string concatenation is fine as long as the number of
concatenation operations is small. I saw a graph once comparing times for
multiple concatenations using the two methods and with small concatentation
counts, the "&" was actually somewhat faster. It wasn't until the number of
concatenations hit around 25 (if I remember correctly) that the string
builder came into its own and then the difference from that point forward
was dramatic.


Ralf
 
Back
Top