Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have some logic behind a web form I'm building. The logic handles whether a user entered a valid value for required field. Code below:

C#
var operationResult = new BO.OperationResult();

         // Physical Location Required
         if (physicalLocationDesc.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "A location is required as part of the entry";
         }
         // Type of Connection Desc. Required
         if (typeOfConnectionID.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "A description for the connection type is required.";
         }
         // Password change frequency desc. required
         if (passwordChangeFrequency.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "Please enter a description for the Password Change Frequency";
         }


The issue that I'm having is that the operationResult.Message is returning:

"A location is required as part of the entryA description for the connection type is required.Please enter a description for the Password Change Frequency"

I'd like to add a line break after each individual error message. Any ideas on how to go about this? (Please note the code above works fine if 1 field is not entered but becomes harder to read 2 or more fields are not entered.)
Posted

Use the Environment.NewLine method:
http://www.dotnetperls.com/newline[^]
 
Share this answer
 
Comments
Member 11820531 30-Jul-15 10:19am    
I've attempted to use:
operationResult.Message += "Message" + Environment.NewLine; // still did not work
operationResult.Message += "Message" + "\r\n" // also did not work
ZurdoDev 30-Jul-15 10:40am    
That will put a new line at the very end. I don't think that is what you want, is it?
System.Envirement.NewLine is your solution. See example:
http://www.dotnetperls.com/newline[^]
 
Share this answer
 
Two ways:
1. Keep the individual message separate as long as possible and then combine them once at "the end":
C#
var operationResult = new BO.OperationResult();
 List<string> messages = new List<string>();
// Physical Location Required
if (string.IsNullOrWhiteSpace(physicalLocationDesc))
{
  operationResult.Successful = false;
  messages.Add("A location is required as part of the entry");
}
// Type of Connection Desc. Required
if (string.IsNullOrWhiteSpace(typeOfConnectionID))
{
  operationResult.Successful = false;
  messages.Add("A description for the connection type is required.");
}
// Password change frequency desc. required
if (string.IsNullOrWhiteSpace(passwordChangeFrequency))
{
  operationResult.Successful = false;
  messages.Add("Please enter a description for the Password Change Frequency");
}
// etc...
operationResult.Message = string.Join(Environment.NewLine, messages);

2. Use a StringBuilder:
C#
var operationResult = new BO.OperationResult();
 StringBuilder messages = new StringBuilder();
string sep = string.Empty;
// Physical Location Required
if (string.IsNullOrWhiteSpace(physicalLocationDesc))
{
  operationResult.Successful = false;
  messages.Append(sep).Append("A location is required as part of the entry");
  sep = Environment.NewLine;
}
// Type of Connection Desc. Required
if (string.IsNullOrWhiteSpace(typeOfConnectionID))
{
  operationResult.Successful = false;
  messages.Append(sep).Append("A description for the connection type is required.");
  sep = Environment.NewLine;
}
// Password change frequency desc. required
if (string.IsNullOrWhiteSpace(passwordChangeFrequency))
{
  operationResult.Successful = false;
  messages.Append(sep).Append("Please enter a description for the Password Change Frequency");
  sep = Environment.NewLine;
}
// etc...
operationResult.Message = messages.ToString();
 
Share this answer
 
v2
What the previous solutions have missed is the comment "a web form". That suggests that your messages are being output to an HTML document.

HTML ignores line breaks and most white-space. To force the message to appear on multiple lines, you'll need to use a <br> tag[^].
C#
var operationResult = new BO.OperationResult();
var messages = new List<string>(3);

// Physical Location Required
if (physicalLocationDesc.Trim().Length <= 0)
{
    operationResult.Successful = false;
    messages.Add("A location is required as part of the entry");
}
// Type of Connection Desc. Required
if (typeOfConnectionID.Trim().Length <= 0)
{
    operationResult.Successful = false;
    messages.Add("A description for the connection type is required.");
}
// Password change frequency desc. required
if (passwordChangeFrequency.Trim().Length <= 0)
{
    operationResult.Successful = false;
    messages.Add("Please enter a description for the Password Change Frequency");
}

if (messages.Count != 0)
{
    operationResult.Message = string.Join("<br>", messages);
}
 
Share this answer
 
v3
Even though it's been a while since I posted this question. I thought I'd post my solution for future purposes.

You can add the "

" syntax within the quotes. As an example I posted the exact code that worked for my situation.

C#
var operationResult = new BO.OperationResult();
         // Physical Location Required
         if (physicalLocationDesc.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += " <br /> <br /> A location is required as part of the entry";
         }
         // Type of Connection Desc. Required
         if (typeOfConnectionID.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "<br /> <br />A description for the connection type is required.";
         }
         // Password change frequency desc. required
         if (passwordChangeFrequency.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "<br /><br />Please enter a description for the Password Change Frequency";
         }


I beleive Richards was the closest to my solution but there were differences. thanks for the responses!
 
Share this answer
 
v2
The effect of line break depends on where you output a string. If, for example, you show text in controls like text boxes, you generally need to use end-of-line character(s): http://en.wikipedia.org/wiki/End-of-line[^].

As you can see, the exact character(s) depend on the platform, so you should always use a platform-independent System.Environment.NewLine:
https://msdn.microsoft.com/en-us/library/68ah771x%28v=vs.110%29.aspx[^].

By the way, never use repeated string concatenations; strings are immutable (do I even have to explain the consequences? this is performance) — always use mutable type System.Text.StringBuilder; for fixed string format, use string.Format.

—SA
 
Share this answer
 
C#
shippedItems.AddRange(OrderItem.FetchLastWeeksOrderItems(MARKET, serverTime));

public class OrderItem
{
    public static IEnumerable<OrderItem> FetchLastWeeksOrderItems(
        string market, DateTime serverTime)
    {
        return OrderItem.Fetch(
            market: market,
            shipConfirmState: ORDERITEMSHIPCONFIRMSTATE.NONE,
            orderPlacedAfter: serverTime.AddDays(-7),
            orderPlacedBefore: serverTime.AddHours(-85));
    }

    private static IEnumerable<OrderItem> Fetch( ... )
    {
        ...
    }
}
 
Share this answer
 
Comments
Matt T Heffron 30-Jul-15 14:55pm    
Nothing in this solution addresses the question at hand!
Environment has Property NewLine for this purpose
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900