PadLeft with spaces - asp.net

Have a relatively simple request.
I wish to pad left a string with spaces in an HTML page using VB on asp.net
For me the most obvious way to do it is
Response.Write(qty.PadLeft(5, " ") + " x " + part_number)
but as HTML does not render multiple spaces, this does not work
my workaround is
Response.Write(qty.PadLeft(5, "0") + " x " + part_number)
which pads the number with zero's but looks fairly unappealing on the website.
Any suggestions?
Thanks
Update:
Based on replies so far I have tried
"100".PadLeft(5, " ")
but this outputs &&100

OK, I solved it by doing this
rep = string.concat(Enumerable.Repeat(" ", 5-qty.Length))
Response.Write(rep + qty+ " x " + part_number)
Not the best but works.

There's & nbsp; or setting or the css (with white-space) to see spaces. If you want to align information, you might want to also look at having a fixed font.

Related

Dynamic Query with MATCHES in Progress 4GL

I have to create a query in which people can input some values or not. If they don't input them, the program will search for everything. And people can put just a part of the data of the code. My code became something like this.
cQuery = "" +
" FOR EACH table1 " +
" WHERE table1.status_ = 1 " +
" AND table1.section MATCHES " + QUOTER("*"+ pc-section + "*").
So my question.
1 - I read a lot that MATCHES uses a table scan and we should avoid it so I thought about putting MATCHES in an if variable. Is it really necessary?
2 - is the QUOTER(""+ pc-section + "") the correct way of using MATCHES in a dynamic query? My results seem wrong and I don't know if it was because of the MATCHES or if there is another problem in my code.
Sorry for any wrong ideas and thanks for your time.
EDIT:
My IF statement is like this:
DEF VAR ifSection AS CHAR.
IF pc-section <> "" THEN ifSection = " AND table1.section MATCHES " + QUOTER("*"+ pc-section + "*").
And in my code i use:
" FOR EACH table1 " +
" WHERE table1.status_ = 1 " + ifSection.
I managed to make it work now. The problem in my results was because I forgot to add 'ifSection' after THEN. But I still am not sure if this is the best way to do it. Is there another better way to use MATCHES in a dynamic query? Thanks for the help
Don't use IF statements in a WHERE clause - you will never, ever use an index that way.
You can put the IF around the outside:
cQuery = " FOR EACH table1 WHERE table1.status_ = 1 ".
IF <something> THEN
// no index
cQuery = cQuery + " AND field-name MATCHES " + QUOTER("*"+ pc-section + "*").
ELSE
IF <something-else> THEN
// may be indexed
cQuery = cQuery + " AND field-name BEGINS " + QUOTER(pc-section).
ELSE
IF <something-else> THEN
// may be indexed
cQuery = cQuery + " AND field-name = " + input-value.
etc, which will help you at least try to use indexes where possible.
Replacing MATCHES with indexed searching is more complex. You could look at word indexes and the CONTAINS keyword as a start.

Page Header overlaping content when using Rotativa to generate PDF from HTML

I am try to convert an HTML file to PDF using "Rotativa" lib, but i am facing some problems when using custom headers, here is my code:
string customSwitches = string.Format("--header-html \"{0}\" " +
"--header-spacing \"0\" " +
"--footer-html \"{1}\" " +
"--footer-spacing \"10\" " +
"--footer-font-size \"10\" " +
"--header-font-size \"10\" ", header, footer);
var invoice = new ViewAsPdf("Index", _invoiceRepository.GetInvoice(1))
{
FileName = "MyPDF.pdf",
CustomSwitches = customSwitches,
PageMargins = margin,
PageSize = size,
PageOrientation = orientation
};
return invoice;
The headers and Footers are being successfully injected on the top and bottom of each page like expected but the problem is that the header is overlapping that rest of the content.
how can i reserve space for the header?
I faced similar trouble when setting --header-spacing \"0\" once. So the solution that worked for me was to set higher spacing between header and content.
Please try setting, for example, spacing to 7 (measure is given in mm, as you can review in WkHtmlToPdf documentation). This way, you can check current PDF with one you have generated before, and verify if change solves the problem. Once there, you should be ablo to adjust the measure to the value that is suitable for you.
So, your code should look like:
string customSwitches = string.Format("--header-html \"{0}\" " +
"--header-spacing \"7\" " +
"--footer-html \"{1}\" " +
"--footer-spacing \"10\" " +
"--footer-font-size \"10\" " +
"--header-font-size \"10\" ", header, footer);
I tried your code in one of my projects and seems to work fine.
Hope it would be of help. If trouble continues, it should be useful to inlcude some samples os your HTML header and PDF body content, to chek the specific case.

Replacing a new line character with streamwriter remove everything after it. (ASP.NET, Json, C#)

I'm having an unexpected problem which I'm hoping one of you can help me with.
I have an ASP.NET Web API with a number of end points, one of which takes user input, received as JSON, and converts it into an order object which is written to a file in .CSV format. The following is a small snippet of the full code as an example.
using (StreamWriter writer = new StreamWriter(file))
{
writer.Write(escape + order.Notes + escape + delim);
writer.Write(escape + order.Reference1 + escape + delim);
writer.Write(escape + order.Reference2 + escape + delim);
writer.WriteLine();
writer.Flush();
}
The problem I am having is that some users are inclined to add line breaks in
certain fields, and this is causing havoc with my order file. In order to remove
these new line characters, I have tried both of the following methods.
writer.Write(escape + product.Notes.Replace("\n", " ") + escape + delim);
writer.Write(escape + product.Notes.Replace(System.Environment.NewLine, " ") + escape + delim);
However, it seems that, rather than just remove the new line character and carry on writing the rest of the fields, when a new line is encountered, nothing else gets written.
Either everything else gets replace with the " " or nothing else is being written at all, but I'm not sure which.
If I remove the .Replace() the whole file is written again but with extra line breaks.
I hope somebody has experienced this one and knows the answer!

Webmatrix - formatting MSSQL database query

I am trying to format the output of a query in a WebMatrix 2 CSHTML file. This is the code I am using:
#foreach(var row in db.Query(selectQueryString))
{
#row.Firstname; + " " + #row.lastname; + " " + #row.Entry;
}
I am getting this error:
"CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
The first issue is that the semicolons could be confusing to Razor, and they are only confusing matters. So change the line in the brackets to
<text>#row.Firstname #row.lastname #row.Entry</text>
And see if that works. The < text > tags tells Razor to output this directly as HTML and not use it as code. You don't need the + " " because once you're putting out HTML, the spaces come automatically.

Writing an array as a comma separated list to screen

Now it seems like a really simple question and I may just be being thick, but what I'm trying to achieve is basically print an array to screen in the following format:
Item 1, Item 2, Item 3, Item 4
Although I say to screen, as that was the best way I could describe it, I'm actually writing it to the page inside some Javascript.
The way I'm currently going about writing it out is as follows:
for each b in theDates
Response.Write("'" + b.CallDate + "',")
next
But obviously that returns a string of Item 1, Item 2, Item 3, Item 4,.
Is there a simple way of getting rid of the last comma or am I going about this completely wrong?
You could use String.Join method. Not sure about VB but something like that:
Response.Write(String.Join(", ", theDates.Select(Function(n) "'" & n.CallDate & "'").ToArray()))
Try String.Join (documentation). My VB is rusty, but hopefully you can read this C#:
var datesInQuotes = theDates.Select(date => "'" + date.CallDate + "'");
Response.Write(String.Join(", ", datesInQuotes));
As others have said, String.Join is what you want in this case.
More generically: you've got to either detect when you're at the the last element and not append a comma, or detect when you're at the first element and not prepend a comma.
Typically it's easier to detect when you're at the first, so:
dim firstDone as bool = false
for each b in theDates
if firstDone then Response.Write (",")
Response.Write("'" + b.CallDate + "'")
firstDone = true
next
(Excuse my rusty vb)
There's no need to String.join this.
st="'"
for each b in theDates
Response.Write(st + b.CallDate + "'")
st=", '"
next
No, you aren't wrong - totally.
Loop through theDates and put the data into a string.
After you are done looping, remove the last comma:
myString.Remove(myString.Length - 1, 1);
Response.Write(myString)
A side note: You really shouldn't output to the screen with Response.Write. You should set the text of a server control as your output. A label is perfectly fine. You will notice that your text won't appear where you think it will if you don't.
You could write the comma in front of each item except the first one.
I would recommend to do just as you were doing, and then use the Response.Remove(s.Length-1, 1)
In C#:
for (int i = 0; i < theDates.Length; i++)
{
if (i > 0)
{
Response.Write(",");
}
Response.Write(theDates[i]);
}
all too complicated. Keep it simple
dim st as string ="'"
for each b in theDates
Response.Write(st + b.CallDate + "'")
st=", '"
next
response.write (st.TrimEnd(New Char() {" ", ","})

Resources