Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the below code but I know it doesn't work as when I try declaring it, my code says it's not defined but I don't know how to write it.
I no I need to declare is before my IF statement but not sure how. Below is what I have but I know its wrong as I keep saying
if (Session["Step01Tel"] != "")
{
var ContactDetails = Step01TelLabel.Text + " " + Session["Step01Tel"].ToString();
}
else if (Session["Step01Email"] != "")
{
var ContactDetails = Step01EmailLabel.Text + " " + Session["Step01Email"].ToString();
}
Then I'm after something like the below in my code to call it as its for the body of my email that my site sends.
msg.Body = ContactDetails.Tostring()
The reason I'm after this is that if the Tel or Email field is empty then I don't want the Tel/Email label to be displayed in the email and you can not us an If inside an email body.
The below shows how I initially had it but as I said this displayed the field label with no value.
////NEED TO ONLY DISPLAY IF VALUE IS PRESENT
// Step01TelLabel.Text + " " + Session["Step01Tel"].ToString()
// + Environment.NewLine.ToString() +
// Step01EmailLabel.Text + " " + Session["Step01Email"].ToString()
// + Environment.NewLine.ToString() +
////
Try this
var ContactDetails = string.Empty;
if (Session["Step01Tel"] != "")
{
ContactDetails = Step01TelLabel.Text + " " + Session["Step01Tel"].ToString();
}
else if (Session["Step01Email"] != "")
{
ContactDetails = Step01EmailLabel.Text + " " + Session["Step01Email"].ToString();
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i have below code where string dont get concatenated
string postCode = "1245";
string city = "Stockholm";
string postCodeAndCity = postCode ?? " " + " " + city ?? " ";
I get output as 1245. Not sure why it excludes city.
But this line works
string.Concat(postCode??" ", " ", city?? " ");
so why the first approach dont work?
The ?? operator associates like this:
string postCodeAndCity = postCode ?? (" " + " " + city ?? (" "));
So if postCode is not null, it just takes postCode. If postCode is null, then it takes (" " + " " + city ?? (" ")).
You can see this from the precedence table, where ?? has lower precedence than +. Therefore + binds more tightly than ??, i.e. a ?? b + c binds as a ?? (b + c), not as (a ?? b) + c.
However, in:
string.Concat(postCode??" ", " ", city?? " ");
The commas of course have higher precedence than the ??. The equivalent using + would be:
(postCode ?? " ") + " " + (city ?? " ");
I suspect what you might want to do is:
If both postCode and city are not null, take both with a space between them.
If one is null but the other isn't, take the non-null one.
If both are null, take an empty string.
You can write this long-hand:
if (postCode != null)
{
if (city != null)
return postCode + " " + city;
else
return city;
}
else
{
if (postCode != null)
return postCode;
else
return "";
}
You can write this a bit shorter (although slightly more expensively) with:
string.Join(" ", new[] { postCode, city }.Where(x => x != null));
You should use string interpolation for this:
var output = $"{postCode} {city}"
For more information see:
https://learn.microsoft.com/de-de/dotnet/csharp/language-reference/tokens/interpolated
This question already has answers here:
Binding Listbox to List<object> in WinForms
(8 answers)
Closed 3 years ago.
I write a program, it is necessary to switch the current status in it, as well as it is necessary to plan it when you plan an event, it is perceived as an object, the object has its own fields, such as the start time and end time of the event, I want this object to be output when generated sheet boxing.
Tell me how can this be done?
List<ChangeStatus> events = new List<ChangeStatus>();
private void toPlanButton_Click(object sender, EventArgs e)
{
string comboBoxTypeNumber = comboBoxType.SelectedItem.ToString();
DateTime Time = new DateTime();
Time = dateTimePicker1.Value;
DateTime longTime = new DateTime();
longTime = dateTimePicker2.Value;
ChangeStatus statusEvent = new ChangeStatus();
statusEvent.StartEvent = Time;
statusEvent.LongEvent = longTime;
statusEvent.TypeEvent = comboBoxTypeNumber;
events.Add(statusEvent);
TimeComparer tc = new TimeComparer();
events.Sort(tc);
}
How to display an object in listbox?
It is necessary to display a list of objects, because in the future I want to make editing objects
listBoxEvent.Items.Add("type: " + statusEvent.TypeEvent + ";" + " start: " + statusEvent.StartEvent + ";" + " long: " + statusEvent.LongEvent + " min;"); - work
You can use System.Linq Linq to get the string text and can call the AddRange() method on Items collection like
List<string> listData = events.Select(x => "type: " + x.TypeEvent + ";" + " start: " + x.StartEvent + ";" + " long: " + x.LongEvent + " min;").ToList();
listBoxEvent.DataSource = listData;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am using this in my controller:
char[] arrDate = date.ToArray();
DateTime dt = DateTime.Parse(arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]);
The error:
System.FormatException: String was not recognized as a valid DateTime.
Consider this:
var date = "11252017";
var arrDate = date.ToArray();
var strDate = arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]; // 98/25/2017
Notice that:
'1' + '1' = 98* ⇒ char + char = int
98 + "/" = "98/" ⇒ int + string = string
"98/" + '2' = "98/2" ⇒ string + char = string
The fix:
var dt = DateTime.Parse("" +
arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]);
*ASCII representation:
'1' in decimal is 49
I assume date is of type string. For parsing a string the DateTime class has several methods of which ParseExact is one. This method can parse a string given a format specifier and a culture. In your case the date can be parsed like this:
var date = "11252017";
var dt = DateTime.ParseExact(date, "MMddyyyy", CultureInfo.InvariantCulture);
By the way a string is an array of chars, so in your code arrDate[0] is exactly the same as date[0]. Just something to keep in mind for the future.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
protected void LinkButton1_Click(object sender, EventArgs e) {
string ContactID = txtContactID.Text;
string EmailMessage = txtEmailContent.Text;
string[] words = ContactID.Split(',');
foreach (string word in words)
{
DataTable dt = reference.SendMultipleEmail_Fetch(Convert.ToInt32(words)).Tables[0];
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
if (row["Email"].ToString() != "" && txtContactID.Text != "" && txtEmailContent.Text != "")
{
Process.Start("mailto:" + row["Email"].ToString() + "?subject=" + "I love you" + "&body=" + "Hi you");
lblInfo.Text = "Message successfully sent";
}
else
{
lblInfo.Text = "Email Content is required or contact not selected";
return;
}
}
}
}
I have a grid table which multiple email addresses can be checked and mailed as a group. am trying to add a link button that will open up outlook and populate with the email addresses checked on the grid and then send mail out
If you just want to open email client window for sending a message, you can use this:
Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" + body);
However, if you just want to send email without any input from the user, best bet would be to send directly via SMTP server. You can use SmtpClient class for that:
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient%28v=vs.110%29.aspx
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string "\\server\printer" and I need to change it to `"printer on server:"
please note that "server and printer can vary in lengths.
var incomingText = #"\\server\printer";
var split = incomingText.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
var decoratedText = split[1] + " on " + split[0];
String[] split = yourString.Split("\\");
Return split[1] + " on " + split[0];
Here you go
https://dotnetfiddle.net/ZsjrsN
var str = #"\\server\printer";
var matches = Regex.Match(str, #"[A-Za-z0-9]{1,}");
string str1 = matches.Value;
matches = matches.NextMatch();
string str2 = matches.Value;
string result = str2 + " on " + str1;