I am creating a form in ASP.NET C# so it can be filled out and emailed to multiple recipients. Part of the form is a checkbox section with multiple options. I can only get the first option selected to be emailed back to the recipients, so if the user selects two or more checkboxes, it only emails the first option. Below is my code sheet
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress From = new MailAddress(mailTextBox.Text);
message.To.Add(new MailAddress("email#domain.com"));
message.Subject = (companyTextBox.Text);
message.IsBodyHtml = true;
message.Body = "<html><head></head><body>" +
"<p></p>" +
"<p>Business Type: " + typeDropDownList.Text + "</p>" +
"<p>Company: " + companyTextBox.Text + "</p>" +
"<p>Name: " + nameTextBox.Text + "</p>" +
"<p>Address: " + addressTextBox.Text + "</p>" +
"<p>City: " + cityTextBox.Text + "</p>" +
"<p>State: " + stateDropDownList.Text + "</p>" +
"<p>Zip Code: " + zipcodeTextBox.Text + "</p>" +
"<p>Phone Number: " + phoneTextBox.Text + "</p>" +
"<p>Email: " + mailTextBox.Text + "</p>" +
"<p>Number Of Locations: " + locationsDropDownList.Text + "</p>" +
**// This is my problem area //**
"<p>Interested In: " + interestedCheckBoxList.Text + "</p>" +
"<p>Interested In: " + interestedCheckBoxList.Text + "</p>" +
"<p>Interested In: " + interestedCheckBoxList.Text + "</p>" +
**// This is my problem area //**
"<p>Message: " + messageTextBox.Text + "</p>" +
"</body></html>";
smtpClient.Send(message);
Response.Redirect("http://www.domain.com");
Thank you in advance.
Jim
You will need to loop through the Items in the CheckBoxList and add them individually.
Example:
foreach(ListItem li in interestedCheckBoxList.Items)
{
//add your stuff
if(li.Selected)
{
//should be using string builder here but....
message.Body += "<p>Interested In: " + li.Text + "</p>";
}
}
You need to iterate through your CheckBoxList and find all the checked items and get the Text property for each item and append to you email text.
string yourSelectedList = "";
foreach (ListItem i in chklst.Items)
{
if (i.Selected)
yourSelectedList += (i.Text + ", ");
}
Then remove the extra comma at the end :)
"<p>Interested In: " + yourSelectedList + "</p>" +
Try to use StringBuilder when concatentating many strings together as it will make a big difference.
Try replacing the code in your "problem area" with the following:
string InterestedIn = "";
foreach (ListItem li in interestedCheckBoxList.Items)
{
if (li.Selected)
InterestedIn += "<p>Interested In: " + li.Text + "</p>";
}
Of course, you can't concatenate this as part of your original string concatenation, so build an "InterestedIn" string of sorts and concatenate the email body with that.
Related
protected void Button1_Click(object sender, EventArgs e)
{
result.Text = "Hello! Here is your school information:" <br />
"Full Name: " + fname.Text + " " + mname.Text + " " + lname.Text +
"Course, Year and Section: " + cour.Text + " " + yr.Text + "" + sec.Text +
"Address: " + add.Text +
"Age: " + age.Text +
"Contact Information: " + num.Text + " / " + email.Text + "";
}
How can I add a newline here? For example;
Full Name: Bill Gates
Course, Year and Section:
Just like that format
I usually use Environment.NewLine, or you can add string "\r\n" or "\n" (it depends on system). More here
Edit:
As mentioned in comments, if it is web page output, html tag for new line is <br>. But for C# it is just string, so your code should be
protected void Button1_Click(object sender, EventArgs e)
{
result.Text = "Hello! Here is your school information:" + "<br />" +
"Full Name: " + fname.Text + " " + mname.Text + " " + lname.Text +
"Course, Year and Section: " + cour.Text + " " + yr.Text + "" + sec.Text +
"Address: " + add.Text +
"Age: " + age.Text +
"Contact Information: " + num.Text + " / " + email.Text + "";
}
I am trying to insert a table in email but it only shows the syntax on the message body of the email.
Attached is the output;
mail.Subject = "Blah Service Alert - " + application + " Upgrade (Day, Month, Year)";
mail.Body = "Dear Customer," + Environment.NewLine + Environment.NewLine +
"I would like to provide notice that we will be carrying out an Upgrade on the " + application + " on (Day, Month, Year)." + Environment.NewLine + Environment.NewLine +
"Blah Essential Maintenance Downtime Window 2019" + Environment.NewLine + Environment.NewLine +
"System: " + application + Environment.NewLine + Environment.NewLine +
"<table><tr>" +
"<th> Downtime Start </th>" +
"<th> Downtime Start </th>" +
"<th> Downtime End </th>" +
"<th> Downtime End </th>" +
"</tr> <tr>" +
"<td> Date: </td" +
"<td> Time: </td>" +
"<td> Date: </td>" +
"<td> Time: </td>" +
"</tr></table>" + Environment.NewLine + Environment.NewLine +
You need to set MailMessage.IsBodyHtml Property to true, assuming that you're using MailMessage class.
Assuming you are using the Outlook Object Model, set the MailItem.HTMLBody property rather than the plain text MailItem.Body.
i would like to make each element of textblockresult visible only when the value is not null.
Intent, ROKEntity, ProcedureName etc are strings that are defined as empty and then receive their value from a json :
string intent = "";
if (!string.IsNullOrEmpty(root.XPathSelectElement("//intent").Value))
{
intent = root.XPathSelectElement("//intent").Value;
}
resultToDisplay = "Action: " + intent
+ Environment.NewLine + "Rok Entity: " + ROKEntity
+ Environment.NewLine + "Rok Entity: " + ROKEntity2
+ Environment.NewLine + "Rok Entity: " + ROKEntity3
+ Environment.NewLine + "Procedure Name: " + ProcedureName
+ Environment.NewLine + "Folder Name: " + FolderName
+ Environment.NewLine + "Task Name: " + TaskName
+ Environment.NewLine + "Worker Name: " + WorkerName
+ Environment.NewLine + "Risk Name: " + RiskName
+ Environment.NewLine + "File Name: " + FileName
+ Environment.NewLine + "Workflow Name: " + WorkflowName
+ Environment.NewLine + "Model Name: " + ModelName
+ Environment.NewLine + "Position Name: " + PositionName
+ Environment.NewLine + "Created by: " + CreatedBy
+ Environment.NewLine + "Modified by: " + ModifiedBy
+ Environment.NewLine + "From: " + From
+ Environment.NewLine + "To: " + To
+ Environment.NewLine + "Display Mode: " + DisplayMode
+ Environment.NewLine + "Data Tracking: " + DataTracking
+ Environment.NewLine + "Details: " + Details
+ Environment.NewLine + "Export Format: " + ExportFormat
+ Environment.NewLine + "Ged Files: " + GedFiles
+ Environment.NewLine + "Ged Folders: " + GedFolders
+ Environment.NewLine + "Map: " + Map
+ Environment.NewLine + "Kpi: " + Kpi
+ Environment.NewLine + "Procedure Type: " + ProcedureType
TextBlockResult.Text = resultToDisplay;
ie i want "Action: " + intent to be visible only when the value of intent is not null nor empty, the same for "Rok Entity: " + ROKEntity and so forth ..
For the moment, i have the following xaml that only allows me to set the visibility of the whole lot to collapsed or hidden or visible:
<TextBlock x:Name="TextBlockResult" Visibility="Collapsed" TextWrapping="Wrap" TextAlignment="Center" FontFamily="Segoe UI" FontSize="16" Foreground="Black"><Run Text="Result"/><InlineUIContainer>
What are the different steps to achieve that please ?
if I understand correctly what you are trying to do, you can just skip the elements with empty or null value. So have a method like
private void AddToDisplay(StringBuilder sb, string title, string value)
{
if (!string.IsNullOrEmpty(value))
{
if (sb.Length > 0)
sb.Append(Environment.NewLine);
sb.Append(title);
sb.Append(value);
}
}
then build your result like
var sb = new StringBuilder();
AddToDisplay(sb, "Action: ", intent);
AddToDisplay(sb, "Rok Entity: ", ROKEntity);
.
.
resultToDisplay = sb.ToString();
that way your results show only non-null non-empty elements and optionally hide the entire TextBlock if resultToDisplay is empty after you are done building it
I've already got the the point where I can save all inputs to a text file but now I would like to create another feature where I can update that file but only through the form itself
private void button_Click(object sender, RoutedEventArgs e)
{
string lb1 = "";
string lb = "";
//Removes listboxtitem object name
ListBoxItem mySelectedItem0 = listBox.SelectedItem as ListBoxItem;
ListBoxItem mySelectedItem1 = listBox1.SelectedItem as ListBoxItem;
if (mySelectedItem0 != null && mySelectedItem1 != null)
{
lb1 = mySelectedItem1.Content.ToString();
lb = mySelectedItem0.Content.ToString();
}
TextWriter saveText = new StreamWriter(#"D:\blah.txt");
saveText.Write("===================================[Receipt]===================================" + "\r\n");
saveText.Write("|Title:" + " " + comboBox.Text + "\r\n");
saveText.Write("|Surname:" + " " + textBox1.Text + "\r\n");
saveText.Write("|Forname:" + " " + textBox2.Text + "\r\n");
saveText.Write("|Passanger[s]:" + " " + textBox6.Text + "\r\n");
saveText.Write("|Group:" + " " + checkBox.IsChecked + " " + "No: " + textBox4.Text + "\r\n");
saveText.Write("|Class:" + " " + comboBox1.Text + "\r\n");
saveText.Write("|Luggage:" + " " + textBox7.Text + "\r\n");
saveText.Write("|Suitcase:" + " " + checkBox1.IsChecked + " " + "No: " +textBox5.Text + "\r\n");
saveText.Write("|Departure On:" + " " + DatePicker.Text + " " + "At:" + " " +comboBox2.Text + "\r\n");
saveText.Write("|Destination From:" + " " + lb + " " + "To:" + " " + lb1 + "\r\n");
saveText.Write("===================================[Receipt]===================================");
saveText.Close();
File.AppendAllText(#"D:\blah.txt", Environment.NewLine);
}
so here it safes all inputs and values as strings into a blah.txt file, how can I now retrieve all information from the file into the form and update it?
You can read the entire content of a text file by using ReadAllText function. Also, you can condense your text construction:
String ticketText = String.Format(#"
===================================[Receipt]====================================
|Title: {0}
|Surname: {1}
|Forname: {2}
...
...
", comboBox.Text, textBox1.Text, textBox2.Text, ...);
saveText.Write(ticketText);
This way of constructing your text is better since everything is done at once and it is easier to read. This will be made even easier when with interpolated strings.
Also I recommend naming your control by their meaning: e.g. txtTitle or TitleText for Title.
In your code you are using 2 different methods to write to a file in one process. You use a StreamWriter to save the receipt. And then static File.AppendAllText to attach an empty line. No need to do that. I think it would be better to generate the message in a separate method and then use the static File.AppendAllText to save everything in one go:
private string GetReceipt()
{
StringBuilder receiptBuilder = new StringBuilder();
receiptBuilder.AppendLine("===================================[Receipt]===================================");
receiptBuilder.AppendLine("|Title:" + " " + comboBox.Text);
receiptBuilder.AppendLine("|Surname:" + " " + textBox1.Text);
receiptBuilder.AppendLine("|Forname:" + " " + textBox2.Text);
receiptBuilder.AppendLine("|Passanger[s]:" + " " + textBox6.Text);
receiptBuilder.AppendLine("|Group:" + " " + checkBox.IsChecked + " " + "No: " + textBox4.Text);
receiptBuilder.AppendLine("|Class:" + " " + comboBox1.Text);
receiptBuilder.AppendLine("|Luggage:" + " " + textBox7.Text);
receiptBuilder.AppendLine("|Suitcase:" + " " + checkBox1.IsChecked + " " + "No: " + textBox5.Text);
receiptBuilder.AppendLine("|Departure On:" + " " + DatePicker.Text + " " + "At:" + " " + comboBox2.Text);
receiptBuilder.AppendLine("|Destination From:" + " " + lb + " " + "To:" + " " + lb1);
receiptBuilder.AppendLine("===================================[Receipt]===================================");
receiptBuilder.AppendLine();
return receiptBuilder.ToString();
}
And then save to the file:
File.AppendAllText(filePath, GetReceipt());
With this small refactoring you can now easily achieve first part of your request: Updating the existing file.
When you need to update the file with new data you simply invoke the GetReceipt() and append the results.
Or if you need the old file to be erased then you can use WriteAllText instead:
File.WriteAllText(filePath, GetReceipt());
Now the reading of the file. Depends what you need. If you just read the text and display it in a text box then it's fairly easy. Just use ReadAllText
myTextBox.Text = File.ReadAllText(filePath);
However if you want to display it in appropriate combo boxes and text boxes then you would have to manually read the file. But if that's the case then I'd encourage you to refactor your code a little bit:
Create a class that holds all the information of the receipt. Each time you want to save your data create a new instance of this class and then serialize it (or save to a database) so it's easy to retrieve specific fields. If you still want to have a nice way to display a receipt you can override the ToString method.
I am using Silverlight4 and Telerik Reporting Q3 2011. I am trying to Generate Reports of all Outlets.
I used Table to display its fields. And I want to Display Full Address in same cell.
How could I?
I Used following Experession to display Full Address in same cell.
= Fields.AddressLine1
+ ", " + Fields.AddressLine2
+ ", " + Fields.Suburb
+ ", " + Fields.City
+ ", " + Fields.State
+ ", " + Fields.Country
I want do display this in same cell but want output like below..
=====================
Address
=====================
15A,
xxxx xxxRoad,
xxxxxx,
xxxxxxxx
====================
But I m getting this
=====================
Address
=====================
15A, xxxx xxxRoad, xxxxxx, xxxxxxxx
=====================
How do I get Output Which I want?
Use Environment.NewLine for getting into new line, see below
=Fields.AddressLine1 + ", "
+ Environment.NewLine + Fields.AddressLine2 + ", "
+ Environment.NewLine + Fields.Suburb + ", "
+ Environment.NewLine + Fields.City + ", "
+ Environment.NewLine + Fields.State + ", "
+ Environment.NewLine + Fields.Country;
EDIT
Refer to the link how to insert a newline in Textblock in silverlight
Have you tried using \n or \r\n in your strings wherever you want to do line breaks?
Yes Got it.....
I made the Function NewLineFeeds() in this function I used Replace() Method to replace the specific string to newLine(\n) .
public static string NewLineFeeds(string text)
{
string result = text.Replace(", ", "\n");
result = result.Replace(", ", "\n");
result = result.Replace(", ", "\n");
return result;
}
and my Expression is
=NewLineFeeds(Fields.AddressLine1
+ ", " + Fields.AddressLine2
+ ", " + Fields.Suburb
+ ", " + Fields.City
+ ", " + Fields.State
+ ", " + Fields.Country)
This call the Function NewLineFeeds() and replace ", " to \n (new Line).
Add this will work Fine..
Use complete name System.Environment.NewLine and it will work.
I just confirmed it in my report.