Hi
I have below code and i want if there r 4 records then each record should get displayed as separate line in a message.
C#
if (Result != null)
{
ShowMessage("Information : ", ddlBook.SelectedItem.Text + " has been done by " + Result.Name , "info");
}Thanks

Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Apr 21, 2023, 2:34 AM EDT
To display each record as a separate line in the message, you can modify the code like this:
if (Result != null) { string message = "Information : \n"; foreach(var result in Result) { message += ddlBook.SelectedItem.Text + " has been done by " + result.Name + "\n"; } ShowMessage(message, "info"); }Tuhin PaulRank #7 · 71.9K reputationPosted Apr 22, 2023, 10:22 PM EDT
Providing the code snippet in a larger context:
protected void btnProcess_Click(object sender, EventArgs e) { // Perform some action and get a list of results List Result = DoSomeAction(); if (Result != null) { string message = "Information : " + Environment.NewLine; for (int i = 0; i < Result.Count; i++) { message += ddlBook.SelectedItem.Text + " has been done by " + Result[i].Name; if (i < Result.Count - 1) { message += Environment.NewLine; } } ShowMessage(message, "info"); } } private void ShowMessage(string message, string messageType) { // Display the message to the user in some way (e.g. using a popup, label, or notification) }when the user clicks a button to process some action, the btnProcess_Click event handler method is called. Inside this method, the DoSomeAction method is called to perform some action and return a list of Result objects. If the Result list is not null, a message is constructed using the ddlBook.SelectedItem.Text value (which is assumed to be a selected book from a dropdown list), along with the name of each Result object in the list. The Environment.NewLine method is used to add a line break after each record. The ShowMessage method is called with the constructed message and a message type (info in this case) to display the message to the user.
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 3, 2023, 2:50 PM EDT
Hello Ramco,
Are you getting 4 records in Result. IF yes then you can use loop and iterate through all element and append in string.
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 3, 2023, 10:59 PM EDT
Hi Jignesh
I have written below code but it is not appending new line
StringBuilder sb = new StringBuilder(); foreach (var colum in Result) { sb.Append(ddlBook.SelectedItem.Text + " has been done by " + colum.Name + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.Append("\n"); }Thanks
Vishal YelveRank #178 · 13.6K reputationPosted Apr 4, 2023, 2:40 AM EDT
Hi Ramco,
for new line use
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 2:46 AM EDT
Hi Jignesh
I want to add new line after every record. IT is returning all records
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 4, 2023, 3:04 AM EDT
Hello,
Can you please check this one, string builder has AppendLine() function which add line for you,
StringBuilder sb = new StringBuilder(); foreach (var colum in Result) { sb.AppendLine(ddlBook.SelectedItem.Text + " has been done by " + colum.Name + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.AppendLine(); }Vishal YelveRank #178 · 13.6K reputationPosted Apr 4, 2023, 3:10 AM EDT
Try this
StringBuilder sb = new StringBuilder(); foreach (var colum in Result) { sb.Append(ddlBook.SelectedItem.Text + " has been done by " + colum.Name + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.Append(Environment.NewLine); }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 3:15 AM EDT
Hi
It is displaying in continuation
foreach (var colum in Result) { sb.AppendLine("Book has been done by " + colum.Name + " in Session : " + colum.SessionDeliveryNo + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.AppendLine(); }Thanks
Vishal YelveRank #178 · 13.6K reputationPosted Apr 4, 2023, 3:20 AM EDT
Ramco,
Try this.
StringBuilder sb = new StringBuilder(); foreach (var colum in Result) { sb.Append(ddlBook.SelectedItem.Text + " has been done by " + colum.Name + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.Append(Environment.NewLine); }Jignesh KumarRank #91 · 22.9K reputationPosted Apr 4, 2023, 3:22 AM EDT
last provided should work for you, How are you displaying on UI ?
One more you can try it out
,
StringBuilder sb = new StringBuilder(); foreach (var colum in Result) { sb.Append(ddlBook.SelectedItem.Text + " has been done by " + colum.Name + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.Append(" "); }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 3:34 AM EDT
Hi
Below is the complete code
foreach (var colum in Result) { sb.AppendLine("Book done by " + colum.Name + " in Session : " + colum.SessionDeliveryNo + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.AppendLine(" "); } ShowMessage("Information : ", sb.ToString(), "info"); public void ShowMessage(String Title, String Text, String Type) { Text = Text.Replace("'", "").Replace("/", " ").Replace(";", " ").Replace(":", " ").Replace(",", " "); Text = System.Text.RegularExpressions.Regex.Replace(Text, @"\t|\n|\r", " "); if (Utility.IE(Request.Browser.Browser)) { MessageBox.Show(Text); } else { ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + Title + "', '" + Text + "','" + Type + "');", true); } }Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 4:09 AM EDT
Hi
In text Visualizer it is showing correctly but in display it is not working
1. Kushaan Chilumuri in Session : SD-3511 dated : 18-02-2023 2. Ranveir Rana in Session : SD-3511 dated : 18-02-2023 sb.Append(" " + sr + ". " + colum.Name + " in Session : " + colum.SessionDeliveryNo + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sr++; sb.Append("\n");string s = sb.ToString();
ShowMessage("Book has been read by : ", s, "info");
Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Apr 4, 2023, 4:56 AM EDT
As you are appending new line (\n) so modify
Text = System.Text.RegularExpressions.Regex.Replace(Text, @"\t|\n|\r", " ");to
Text = System.Text.RegularExpressions.Regex.Replace(Text, @"\t|\r", " ");Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 5:17 AM EDT
Hi Sachin
I have written like below . Message not getting displayed
StringBuilder sb = new StringBuilder(); foreach (var colum in Result) { sb.Append(" " + sr + ". " + colum.Name + " in Session : " + colum.SessionDeliveryNo + " dated : " + Convert.ToDateTime(colum.SessionDate).ToString("dd-MM-yyyy")); sb.Append("\n"); sr++; } string s = sb.ToString(); s = s.Replace("'", "").Replace("/", " ").Replace(";", " ").Replace(":", " ").Replace(",", " "); s= System.Text.RegularExpressions.Regex.Replace(s, @"\t|\r", " "); if (Utility.IE(Request.Browser.Browser)) { MessageBox.Show(s); } else { ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + "Book has been read by : " + "', '" + s + "','" + "info" + "');", true); }Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Apr 4, 2023, 5:27 AM EDT
debug show popup function. inspect in browser and check the response.
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 5:32 AM EDT
Hi Sachin
In debug it is showing - Uncaught SyntaxError: Invalid or unexpected token.
Secondly when i write like this then it shows message but line break is not inserted
s= System.Text.RegularExpressions.Regex.Replace(s, @"\t|\n|\r", " ");
Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Apr 4, 2023, 5:46 AM EDT
use
sb.Append(Environment.NewLine);
and
Text = System.Text.RegularExpressions.Regex.Replace(Text, @"\t|\r", " ");
if doesn't work share you JS function
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 6:03 AM EDT
Hi Sachin
not working . Message does not show up
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 6:19 AM EDT
Hi Sachin
Below r the codes
function ShowPopup(heading, body, type) { swalInit.fire({ title: heading, text: body, type: type, position: 'top', allowOutsideClick: false, timer: 6000 }); } function ShowPopup_Redirect(heading, body, type, url) { swalInit.fire({ title: heading, text: body, type: type, position: 'top', allowOutsideClick: false, onClose: function () { location.href = url; } }); }Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 4, 2023, 7:38 AM EDT
Hi Jignesh
what will be your ClientScript.RegisterStartupScript line
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 4, 2023, 9:16 AM EDT
Hello Ramco,
Use this one,
string msg = "Book has been read by : " + s + ClientScript.RegisterStartupScript(this.GetType(), "Popup", string.Format("ShowPopup('{0}');", msg);", true);Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 4, 2023, 9:40 AM EDT
You can modify the code to concatenate the message with a line break after each record. Here is an example:
if (Result != null) { string message = "Information : "; for (int i = 0; i < Result.Count; i++) { message += ddlBook.SelectedItem.Text + " has been done by " + Result[i].Name; if (i < Result.Count - 1) { message += Environment.NewLine; } } ShowMessage(message, "info"); }Cr BhargaviRank #420 · 5.9K reputationPosted Sep 20, 2023, 12:17 PM EDT
Anandu G NathRank #681 · 3.5K reputationPosted Dec 27, 2023, 3:31 AM EST
if (Result != null) { string message = "Information: " + ddlBook.SelectedItem.Text + " has been done by "; // Assuming Result is a collection of objects, iterating through each object foreach (var item in Result) { message += item.Name + " "; // Use " " for HTML line breaks } ShowMessage("Information", message, "info"); }Anandu G NathRank #681 · 3.5K reputationPosted Dec 27, 2023, 3:32 AM EST
if (Result != null) { string message = "Information : \n"; foreach(var result in Result) { message += ddlBook.SelectedItem.Text + " has been done by " + result.Name + "\n"; } ShowMessage(message, "info"); }