Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

c# line break not working

I have the following code segement in c#

public static string ApprovedEmailForNonSAPBody =
            @"
Request ID: <RequestID> \n
Requestor: <Requestor> \n
Request Type: <RequestType> \n
Customer: <Customer> \n
Plant: <Plant> \n
Total: <Total> \n
Damages: <Damages> \n
Reason: <Reason> \n
Corrective Action: <CorrectiveAction> \n

Debit Credit Form <testlink>";

Open in new window


Each of the <   > also inserts text data.

When this renders to the page the \n are included in the output versus breaking the line.

How can I ensure each of these items breaks where I have put the \n?

For extra context here is the code that would fill the  < >

     public string GetRejectEmailBody(int requestId)
        {
            var detailsLink = serverPath + "DrcrRequest/Details/" + requestId;
            var drcrRequest = _drcrDbContext.DrCrRequest.FirstOrDefault(x => x.Id == requestId);
            var rejectEmailBody = EmailConstants.RejectBody;
            var plant = _drcrDbContext.Plants.FirstOrDefault(x => x.PlantId == drcrRequest.PlantId).Name;
            var decimalTotal = drcrRequest.DrCrTotal;
            var formattedCurrencytotal = String.Format("{0:C}", decimalTotal);
            var decimalDamages = drcrRequest.DrCrTotalDowntime;
            var formattedCurrncyDamages = String.Format("{0:C}", decimalDamages);

            var result = rejectEmailBody.Replace("<RequestID>", requestId.ToString())
                                        .Replace("<RequestType>",
                                                 drcrRequest.RequestType.GetDescription()
                                                 [(int)drcrRequest.RequestType])
                                        .Replace("<Requestor>", drcrRequest.RequesterName)
                                        .Replace("<Reason>", drcrRequest.Reason)
                                        .Replace("<Customer>", drcrRequest.PayeeCustomerName)                                      
                                        .Replace("<CorrectiveAction>", drcrRequest.CorrectiveActionRequired)
                                        .Replace("<ApproverRole>", getRejectedByRoleName(requestId))
                                        .Replace("<ApproverComments>", getRejectionComments(requestId))
                                        .Replace("<Plant>", plant)
                                        .Replace("<Total>", formattedCurrencytotal)
                                        .Replace("<Damages>", formattedCurrncyDamages)
                                        .Replace("<DetailsLink>", detailsLink);

            return result;
        }

Open in new window

SOLUTION
Avatar of Alex [***Alex140181***]
Alex [***Alex140181***]
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robb Hill

ASKER

so in doing that here is the strange situation I get.

Doing it the way you suggest if I do this...many times the lines run together...then if I change Corrective Action to CorrectiveAction it works.   I am not sure how to make sense of this but I do need to esure nice formatting as this is used as the body of an email to customers.

Reason: <Reason> 
Corrective Action: <CorrectiveAction>
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I remove the @ cannot I still do the replace on <RequestID> 
Please rephrase your last comment, I don't understand what you mean.

ps.  Does the adjustment I suggested result in the line breaks you want.
@Andy

I am testing now.   I had alot of strings to refactor.


@Alex your scenario kind of works but not not allow for the control needed for dynamic inserts of the string.   Not knowing how much text will be there creates a bad display.  But thank you for your answer as that clarifes the string difference.
@Andy:

refactoring as suggested...I still get items on same line.

        public static string ApprovalBody =

            "Request ID: <RequestID> \n " +
            "Requestor: <Requestor> \n " +
            "Request Type: <RequestType> \n " +
            "Customer: <Customer> \n " +
            "Plant: <Plant> \n " +
            "Total: <Total> \n " +
            "Damages: <Damages> \n " +
            "Reason: <Reason> \n\n " +
            "Corrective Action: <CorrectiveAction> \n\n\n" ;




It still seems that the last two lines...Reason and Corrective action run together....when using the @ if I manually broke 2 or 3 lines it was working.   So you can see here i added two line breaks...

Eitherway I was hoping to prevent any runon regardless of how large "REASON" is



It seems that putting the \n\n after Reason and making to string was the best solution.  That section was inserting a max 5000 character limit.  My test was successful.

Thank you both for you help and knowledge on this subject.