Answered by:
Can't add a newline to a string
Question

I'm having the hardest time simply adding a newline to a string. Here is the code I have now:
namespace CloudGlassFull.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
System.Data.SqlClient.SqlConnection cnn;
string connectionString = "Server=172.22.20.103;Database=CMDB;User Id=jenkins;Password=jenkins123;";
cnn = new System.Data.SqlClient.SqlConnection(connectionString);
cnn.Open();
string sql = "SELECT * FROM VMSizes";
SqlCommand command = new SqlCommand(sql, cnn);
SqlDataReader dataReader = command.ExecuteReader();
StringBuilder output = new StringBuilder();
while (dataReader.Read())
{
output.Append("size:" + dataReader.GetValue(1)).AppendLine();
//output += "size:" + dataReader.GetValue(1) + ",";
}
string returnValue = output.ToString();
//output = "[" + output + "]";
return new string[] { returnValue };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}The problem is here:
while (dataReader.Read())
{
output.Append("size:" + dataReader.GetValue(1)).AppendLine();
//output += "size:" + dataReader.GetValue(1) + ",";
}I've tried using Environment.Newline, adding "\r\n", everything I can think of, but my return ALWAYS looks like this:
"size:Standard_D1_v2\r\nsize:Standard_D2_v2\r\nsize:Standard_D3_v2\r\nsize:Standard_D4_v2\r\nsize:Standard_D11_v2\r\nsize:Standard_D12_v2\r\nsize:Standard_D13_v2\r\nsize:Standard_D14_v2\r\nsize:Standard_DS1_v2\r\nsize:Standard_DS2_v2\r\nsize:Standard_DS3_v2\r\nsize:Standard_DS4_v2\r\nsize:Standard_DS5_v2\r\nsize:Standard_DS11_v2\r\nsize:Standard_DS12_v2\r\nsize:Standard_DS13_v2\r\nsize:Standard_DS14_v2\r\nsize:Standard_F8s\r\n"
What am I doing wrong? I just need a newline in there instead of the \r\n.
Thanks!
Answers

Try a different value:
List<string> list = new List<string>();
while (dataReader.Read())
{
list.Add( /*"size:" +*/ dataReader.GetString(1));
}
return list.ToArray();
 Edited by Viorel_MVP Monday, January 7, 2019 7:29 PM
 Proposed as answer by Andrew B. Painter Monday, January 7, 2019 8:24 PM
 Marked as answer by BonnieBMVP, Moderator Sunday, February 3, 2019 11:16 PM

Much closer!!! Here's the output, the size: isn't listed, just the database results:
[
"Standard_D1_v2",
"Standard_D2_v2",
"Standard_D3_v2",
"Standard_D4_v2",
"Standard_D11_v2",
"Standard_D12_v2",
"Standard_D13_v2",
"Standard_D14_v2",
"Standard_DS1_v2",
"Standard_DS2_v2",
"Standard_DS3_v2",
"Standard_DS4_v2",
"Standard_DS5_v2",
"Standard_DS11_v2",
"Standard_DS12_v2",
"Standard_DS13_v2",
"Standard_DS14_v2",
"Standard_F8s"
] Marked as answer by Farslayer Monday, January 7, 2019 7:57 PM
All replies

dataReader.GetValue(1) : Gets the value of the specified column in its native format.
try to add .ToString()
dataReader.GetValue(1)
.ToString();
 Proposed as answer by El Andaloussi Ishrak Monday, January 7, 2019 3:29 PM
 Unproposed as answer by BonnieBMVP, Moderator Monday, January 7, 2019 4:27 PM

The "\r\n" is a new line … well, technically, it's a combination of "Carriage Return" / "Line Feed" … but that results in a new line, which is sounds like what you want. Displaying it like you are, you'll see the "\r\n", which of course you won't see normally, you'd just see the line break.
So, no, you're not doing anything wrong. That is how it should be.
So now, the question I have for you is why do you think it's a problem? How are you using this string such that it's not giving you the results you expect?
~~Bonnie DeWitt [C# MVP]
http://geekgoddessbonnie.blogspot.com
 Edited by BonnieBMVP, Moderator Monday, January 7, 2019 4:27 PM fix typo

dataReader.GetValue(1) : Gets the value of the specified column in its native format.
try to add .ToString()
dataReader.GetValue(1)
.ToString();
Please do not Mark your own post as a Proposed Answer … it is not proper Forum etiquette. Plus, I don't think you answered the OP's question anyway. Sorry ...~~Bonnie DeWitt [C# MVP]



Hi, so the reason I need the newline is so that when I display it to the user it is formatted correctly. Right now it's just one big string and I'm trying to present a JSON output which is readable and not just a long line full of those \r\n characters.
And have you tested that part (displaying to the user)? And how is this actually getting displayed? What kind of UI (User Interface) is it?~~Bonnie DeWitt [C# MVP]

I have, it looks like this if I don't try to use newlines: [ "size:Standard_D1_v2,size:Standard_D2_v2,size:Standard_D3_v2,size:Standard_D4_v2,size:Standard_D11_v2,size:Standard_D12_v2,size:Standard_D13_v2,size:Standard_D14_v2,size:Standard_DS1_v2,size:Standard_DS2_v2,size:Standard_DS3_v2,size:Standard_DS4_v2,size:Standard_DS5_v2,size:Standard_DS11_v2,size:Standard_DS12_v2,size:Standard_DS13_v2,size:Standard_DS14_v2,size:Standard_F8s,[size:Standard_D1_v2,size:Standard_D2_v2,size:Standard_D3_v2,size:Standard_D4_v2,size:Standard_D11_v2,size:Standard_D12_v2,size:Standard_D13_v2,size:Standard_D14_v2,size:Standard_DS1_v2,size:Standard_DS2_v2,size:Standard_DS3_v2,size:Standard_DS4_v2,size:Standard_DS5_v2,size:Standard_DS11_v2,size:Standard_DS12_v2,size:Standard_DS13_v2,size:Standard_DS14_v2,size:Standard_F8s,]" ] If I do try the new lines, it looks like this: [ "size:Standard_D1_v2,\r\nsize:Standard_D2_v2,\r\nsize:Standard_D3_v2,\r\nsize:Standard_D4_v2,\r\nsize:Standard_D11_v2,\r\nsize:Standard_D12_v2,\r\nsize:Standard_D13_v2,\r\nsize:Standard_D14_v2,\r\nsize:Standard_DS1_v2,\r\nsize:Standard_DS2_v2,\r\nsize:Standard_DS3_v2,\r\nsize:Standard_DS4_v2,\r\nsize:Standard_DS5_v2,\r\nsize:Standard_DS11_v2,\r\nsize:Standard_DS12_v2,\r\nsize:Standard_DS13_v2,\r\nsize:Standard_DS14_v2,\r\nsize:Standard_F8s,\r\n[size:Standard_D1_v2,\r\nsize:Standard_D2_v2,\r\nsize:Standard_D3_v2,\r\nsize:Standard_D4_v2,\r\nsize:Standard_D11_v2,\r\nsize:Standard_D12_v2,\r\nsize:Standard_D13_v2,\r\nsize:Standard_D14_v2,\r\nsize:Standard_DS1_v2,\r\nsize:Standard_DS2_v2,\r\nsize:Standard_DS3_v2,\r\nsize:Standard_DS4_v2,\r\nsize:Standard_DS5_v2,\r\nsize:Standard_DS11_v2,\r\nsize:Standard_DS12_v2,\r\nsize:Standard_DS13_v2,\r\nsize:Standard_DS14_v2,\r\nsize:Standard_F8s,\r\n]" ]

Sorry, you still haven't answered my question … what is the user UI where this is being displayed? How about showing us a screenshot?
~~Bonnie DeWitt [C# MVP]



Try a different value:
List<string> list = new List<string>();
while (dataReader.Read())
{
list.Add( /*"size:" +*/ dataReader.GetString(1));
}
return list.ToArray();
 Edited by Viorel_MVP Monday, January 7, 2019 7:29 PM
 Proposed as answer by Andrew B. Painter Monday, January 7, 2019 8:24 PM
 Marked as answer by BonnieBMVP, Moderator Sunday, February 3, 2019 11:16 PM

Much closer!!! Here's the output, the size: isn't listed, just the database results:
[
"Standard_D1_v2",
"Standard_D2_v2",
"Standard_D3_v2",
"Standard_D4_v2",
"Standard_D11_v2",
"Standard_D12_v2",
"Standard_D13_v2",
"Standard_D14_v2",
"Standard_DS1_v2",
"Standard_DS2_v2",
"Standard_DS3_v2",
"Standard_DS4_v2",
"Standard_DS5_v2",
"Standard_DS11_v2",
"Standard_DS12_v2",
"Standard_DS13_v2",
"Standard_DS14_v2",
"Standard_F8s"
] Marked as answer by Farslayer Monday, January 7, 2019 7:57 PM


Glad you got it working! But, I think that you should mark Viorel's reply as an Answer!!
~~Bonnie DeWitt [C# MVP]
 Proposed as answer by Andrew B. Painter Monday, January 7, 2019 8:24 PM