Add double quotes to the datatable column - c#

In one of my datatable column, I want the value to be shown in double quotes
AS:- "My value"
Below is my code:-
string StrPriBody = "Dear User, <br><br> The Number of days revised by you from " +
" " + table.Rows[0]["LAST_ACTION_DAYS"] + " days to " +
" " + table.Rows[0]["CURRENT_ACTION_DAYS"] + " days. <br /> " +
" with Remark <b> " + table.Rows[0]["REMARKS"] + "</b><br /><br />";
I want to show REMARK value in double quotes.
How to achieve that ?

Add extra quotes with backslash:
string StrPriBody = "Dear User, <br><br> The Number of days revised by you from " +
" " + table.Rows[0]["LAST_ACTION_DAYS"] + " days to " +
" " + table.Rows[0]["CURRENT_ACTION_DAYS"] + " days. <br /> " +
" with Remark <b> \"" + table.Rows[0]["REMARKS"] + "\"</b><br /><br />";

Use \ to print the escape sequence characters in a string
" with Remark <b> \"" + table.Rows[0]["REMARKS"] + "\" </b><br /><br />";

For better readability I would use verbatim string literal, as it allows to avoid concatenation and easily expand on multiple lines. Also, String.Format would make your string more readable:
string StrPriBody = String.Format(#"
Dear User,
<br><br>
The Number of days revised by you from {0} days to {1} days. <br />
with Remark <b> ""{2}""</b>
<br /><br />",
table.Rows[0]["LAST_ACTION_DAYS"],
table.Rows[0]["CURRENT_ACTION_DAYS"],
table.Rows[0]["REMARKS"]);
Also, C# 6.0 (Visual Studio 2015) has introduced interpolated strings, that makes string construction even more reader friendly:
string StrPriBody = $#"
Dear User,
<br><br>
The Number of days revised by you from {table.Rows[0]["LAST_ACTION_DAYS"]} days to {table.Rows[0]["CURRENT_ACTION_DAYS"]} days. <br />
with Remark <b> ""{table.Rows[0]["REMARKS"]}""</b>
<br /><br />";

Related

How to pass a variable to another ASP.net page

Okay so I have some c# that generated href anchor tags styled as list items and throws it onto an aspx page like so;
html += "<a href='../InspectionView.aspx' class='list-group-item' id=''>Inspection ID: " + inspectionID + " - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";
Now this is in a loop, the variables are pulled from a SQL database and used to populate that html string.
Now, what I'm trying to do is have it so when the user clicks on one of the generated hrefs, and is redirected to the next page, the variable inspectionID is passed forward. I thought there might be someway of storing it in the ID of the href tag but I dont know where to go from there.
Thanks a lot.
Add a query string parameter.
html += "<a href='../InspectionView.aspx?inspectionID='" + inspectionID + " class='list-group-item' id=''>Inspection ID: " + inspectionID + " - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";
For reading on the receiving page:
string inspectionID = Request.QueryString["inspectionID"];
See
https://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring(v=vs.110).aspx
a very simple way is to stick into a query string. Since this isn't a server control it might be the only way to it.
something like...
html += "<a href='../InspectionView.aspx?InspectionID="+HttpUtility.UrlEncode(Inspection_ID.ToString())+"&anyotherQSField="+HttpUtility.UrlEncode(anyotherQSFieldVariable) + "' class='list-group-item'> - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";
Then in InspectionView.aspx,get values with something like:
String strInspection_ID = Request.QueryString["InspectionID"];
You likely need to convert to string for this to work for the ID.
You dont have to use HttpUtility.UrlEncode for Inspection_ID but if you have other strings you want to use in QS that might contain spaces or other odd characters - it would be wise.

Better way to give space other than

I am using the following code to give blank space so that the three elements "label, then dropdown and then a button for action on dropdown" are right aligned in a panel in a web page.
Now, I know I can do with padding/margin, however, it all works only with respect to the element at right side and not from the right hand side of the browser.
However, I was talented enough to achieve what I want using but I find it weird to write the code this way:
LiteralSpecial.Text = " " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"Select page ";
Is there are way to refine this please, folks?
Have you tried using CSS?
<div style="text-align:right">Select page</div>
See it in action: http://jsfiddle.net/vhxchyrj/2/
<div style="width:600px;padding-left:550px;">Select page</div>

Concatenating multiple strings with nullables

I Have a messagebox to display some text and data (if existing) within database. The current Issue is trying to show nulls and trying to convert to ShortDate. I've taken two approach but none quite work in the way I need.
The first approach uses Ternary concatenation within the string but it behaves really weird.
DialogResult DuplicateMessage = MessageBox.Show("A contact name " + DuplicateName.Forename + " " + DuplicateName.Surname + " already exists within the System."
+ "\n Existing Client: " + DuplicateName.Forename + " " + DuplicateName.Surname
+ "\n Date of Birth: " + DuplicateName.DOB != null ? Convert.ToDateTime(DuplicateName.DOB).ToString("yyyy-mm-dd") : " ",
,"Possible Duplicate Client", MessageBoxButtons.YesNo);
Currently The message box only shows the line breaks and the Date Of birth. Not even the text "Date of Birth"
If I remove Tertiary and conversion and simply have
DialogResult DuplicateMessage = MessageBox.Show("A contact name " + DuplicateName.Forename + " " + DuplicateName.Surname + " already exists within the System."
+ "\n Existing Client: " + DuplicateName.Forename + " " + DuplicateName.Surname
+ "\n Date of Birth: " + DuplicateName.DOB
,"Possible Duplicate Client", MessageBoxButtons.YesNo);
This works, shows everything. Only issue is that the Date of birth is in the wrong format. Was wondering how do I make it so the date is in short date format and will show everything.
all Properties Of 'DuplicateName' are nullable,
I suspect this is a problem with operator precedence using the conditional operator. It's likely including string concatenations as part of the condition being tested, rather than as part of the result. You can explicitly enclose the elements of that operator with parentheses to identify which strings belong therein and which do not:
"\n Date of Birth: " + (DuplicateName.DOB != null ? Convert.ToDateTime(DuplicateName.DOB).ToString("yyyy-mm-dd") : " ")
Additionally, if DOB is a DateTime? then you can simplify your code a little:
"\n Date of Birth: " + (DuplicateName.DOB.HasValue ? DuplicateName.DOB.Value.ToString("yyyy-mm-dd") : " ")
There's no need to use Convert on Nullable<T> types, you can more easily (and safely) make use of the HasValue and Value properties.
You can fix it by using another pair of parentheses:
(DuplicateName.DOB != null ? Convert.ToDateTime(DuplicateName.DOB))
In your first case, you're concatenating a huge string together (because you don't use any parentheses) and then testing that for null. It's equivalent to this:
var stringToTest = "A contact name " + DuplicateName.Forename + " " + DuplicateName.Surname + " already exists within the System."
+ "\n Existing Client: " + DuplicateName.Forename + " " + DuplicateName.Surname
+ "\n Date of Birth: " + DuplicateName.DOB;
DialogResult DuplicateMessage =
MessageBox.Show(stringToTest != null ? Convert.ToDateTime(DuplicateName.DOB).ToString("yyyy-mm-dd") : " ",
,"Possible Duplicate Client", MessageBoxButtons.YesNo);

C# sending e-mail with html link to file - unrecognized escape sequence

I have a C# app that is sending an e-mail. I wish to send a link in the mail for users to click and open a file.
However where I have the link to my workbook I have issues. I have 5 errors all the same 'unrecognized escape sequence' where every "/" is. How do I get round this?
string htmlHeader = "<table style='font-size: 12pt;'>" +
"<tr><a href='file:///G:\Shared\Team\New\Corporate%20Actions\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
"<tr><th align='left'>Status</th><th> </th>" +
"<th align='left'>Sedol</th><th> </th>" +
"<th align='left'>Name</th><th> </th>" +
"<th align='left'>Date Effective</th><th> </th>" +
"<th align='left'>Event Code</th><th> </th>" +
"<th align='left'>Terms</th><th> </th></tr>";
C# Escape characters using the letter \ followed by another letter, example: a newline escape: \n. Since there is no \S escape character in C# (see the list here: http://msdn.microsoft.com/en-us/library/h21280bw.aspx) the compiler can't parse it. To solve it us \\, the escape sequnce followed by the backslash so the compiler will know you mean to print \.
Exmaple:
string htmlHeader = "<table style='font-size: 12pt;'>" +
"<tr><a href='file:///G:\\Shared\\Team\\New\\Corporate%20Actions\\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
"<tr><th align='left'>Status</th><th> </th>" +
"<th align='left'>Sedol</th><th> </th>" +
"<th align='left'>Name</th><th> </th>" +
"<th align='left'>Date Effective</th><th> </th>" +
"<th align='left'>Event Code</th><th> </th>" +
"<th align='left'>Terms</th><th> </th></tr>";
Notice the second line, on the path part, there is a double left-backslash instead of one.
Try adding a # in front of your string(part) containing the escape sequence
like:
string htmlHeader = "<table style='font-size: 12pt;'>" +
#"<tr><a href='file:///G:\Shared\Team\New\Corporate%20Actions\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
"<tr><th align='left'>Status</th><th> </th>" +
"<th align='left'>Sedol</th><th> </th>" +
"<th align='left'>Name</th><th> </th>" +
"<th align='left'>Date Effective</th><th> </th>" +
"<th align='left'>Event Code</th><th> </th>" +
"<th align='left'>Terms</th><th> </th></tr>";
Not the most clean solution but its a working solution.I would advise your to look at UnTraDe response aswell.

How to covert shortened html string into actual html link in label

Hey guys I am trying to print a list of responses from an API into labels and one issue I have is that the response (from JSON) is a string with a shortened link in it. When I put that string into a label the link is not recognized as a link the browser just think it is HTML so there is no underline or pointer. How can I solve this? I have already tried to HtmlEncode it and that did not help.
Here is what I am trying to do.
lblResponse.InnerHtml += "<strong>created_at:</strong> " + item.created_at
+ "<strong>&nbsp text:</strong> " + HttpUtility.HtmlEncode(item.text) + "<br />";
Which returns this into the label. Though in my browser the shortened link is not recognized as a link. Advice?
created_at: Tue Apr 16 20:30:32 +0000 2013 text: Here is some social media news for the week... http://t.co/RR5DKvqUjd
Thanks in advance for the help.
var date = "Tue Apr 16 20:30:32 +0000 2013";
var text = "Here is some social media news for the week... http://t.co/RR5DKvqUjd";
var textwithanchor = Regex.Replace(text, #"\(?\bhttp://[-A-Za-z0-9+&##/%?=~_()|!:,.;]*[-A-Za-z0-9+&##/%=~_()|]", delegate(Match match)
{
return string.Format("<a href='{0}'>{0}</a>", match.ToString());
});
var html = "<strong>created_at:</strong> " + date + "<strong>&nbsp text:</strong> " + textwithanchor + "<br />";
Regex gracefully borrowed from here: http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html but please take note of the caveats.
Try something like this:
lblResponse.InnerHtml += "<strong>created_at:</strong> " +
item.created_at +
"<strong>&nbsp text:</strong> " +
"<a href=\"" + item.text + "\"" +
HttpUtility.HtmlEncode(item.text) +
"</a><br />";

Categories

Resources