0

I am trying to build a string that includes a newline character and is getting the weirdest result. This string is going to be stored in a SQL database to be later used as part of an email. My code is the following:

Dim strBody As String = "Andy," & Environment.NewLine

When I inspect the value of strBody during a debugging session, it is the following:

"Andy," & vbCrlf

I am obviously expecting is to be more like:

"Andy,"

Knowing that what is after the , is a hidden character.

Ultimately, the problem is... when I include strBody as part of my SQL insert statement, it literally shows up as the following within my SQL insert statement:

'Andy," & vbCrLf & "'

I was using this code yesterday and it worked fine. I am using similar code within another function of the same asp.net project and it works fine. I have tried using + instead of &, I have tried to use vbCrLf instead of Environment.NewLine, I have tried using stringbuilder, I have tried using string.concat. All with the same results where the & vbCrLf is included in strBody.

Is there a setting that I accidentally changed?

I know this is a weird one... Any help is greatly appreciated.

wscourge
  • 10,657
  • 14
  • 59
  • 80
Andy
  • 3
  • 1
  • 1
    That's literally impossible! If this is really true then you've broken the universe! -- Are you sure that it is _**really**_ part of the string? Have you tried writing it to a file or showing it in a message box? Keep in mind that what is shown when you inspect your variables in _**debug mode**_ is not the same as the final string. – Visual Vincent Oct 19 '17 at 05:20
  • 1
    Could you give us a screenshot showing the problem? – Visual Vincent Oct 19 '17 at 05:24

3 Answers3

0

I believe you will need to use some combination of Char(10) and Char(13):

Dim strBody As String = "'Andy,'" & " + CHAR(13)+CHAR(10) + "

There is a discussion about these and other methods on this thread.

WalkerMx
  • 1
  • 2
0

This is only Visual Studio showing you that there is new line character (vbCrLf or Environment.NewLine). If you use that string anywhere, it will be stored correctly.

Ondřej
  • 1,645
  • 1
  • 18
  • 29
  • Thanks. After some investigation, I was able to confirm that Visual Studio displays the string like I mentioned above just so that the hidden characters are visible. When it is actually stored in the SQL database, it is correct. My problem was that my string had an unexpected single quote in it and it was causing the SQL command to fail. When I started investigating, I focused on the VbCrlf because it looked wrong; I removed the single quote and the sting stored in the database correctly. – Andy Oct 21 '17 at 10:56
0

You can do like this if you just need to insert Environment.NewLine inside database.

Dim a As String = """ & Environment.NewLine & """
Dim strBody As String = String.Format("Andy,{0}", a)

'"Andy," & Environment.NewLine & ""
Tuan Zaidi
  • 343
  • 1
  • 14