none
Can't add a newline to a string RRS feed

  • 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!

    Monday, January 7, 2019 3:03 PM

Answers

  • Try a different value:

     

       List<string> list = new List<string>();

       while (dataReader.Read())

       {

          list.Add( /*"size:" +*/ dataReader.GetString(1));

       }

       return list.ToArray();

     


    Monday, January 7, 2019 7:29 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
    Monday, January 7, 2019 7:51 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();
    Monday, January 7, 2019 3:29 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://geek-goddess-bonnie.blogspot.com


    Monday, January 7, 2019 4:26 PM
    Moderator
  • 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]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 7, 2019 4:29 PM
    Moderator
  • Try cheating using:

    output.Replace(@"\r\n", Environment.NewLine);

    Monday, January 7, 2019 4:33 PM
  • 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.
    Monday, January 7, 2019 5:31 PM
  • 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]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 7, 2019 5:43 PM
    Moderator
  • 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]" ]
    Monday, January 7, 2019 6:56 PM
  • 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]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 7, 2019 7:00 PM
    Moderator
  • It's being displayed in postman at the moment, so the user can call the API and should see pretty JSON

    

    Monday, January 7, 2019 7:22 PM
  • Monday, January 7, 2019 7:23 PM
  • Try a different value:

     

       List<string> list = new List<string>();

       while (dataReader.Read())

       {

          list.Add( /*"size:" +*/ dataReader.GetString(1));

       }

       return list.ToArray();

     


    Monday, January 7, 2019 7:29 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
    Monday, January 7, 2019 7:51 PM
  • Got it!!!  I just removed the comments around "size" and it worked, thank you so much!!!!
    Monday, January 7, 2019 7:56 PM
  • Glad you got it working! But, I think that you should mark Viorel's reply as an Answer!!

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 7, 2019 8:22 PM
    Moderator