DaddyCode Team Blog
C# , ASP.NET, MVC, SQL, Sharepoint, JQuery and nowt else from a Web Developer in Leeds
29 Feb 2012
Get / Show Entity Framework (EF) Generated SQL
Before I forget AGAIN , when you have some linq from EF and you want to see the generated SQL, cast it to ObjectQuery and call ToTraceString().
20 Feb 2012
Using iTextSharp to print Avery Labels PDF with ASP.NET MVC and C#
Tasked with printing labels nicely on Avery L7992 label sheets (or any other type of labels really) from a web application, I did the usual messing around with HTML + CSS in different browsers before deciding to start again and try using PDF generation as a means of getting more control. After a few hours, a bunch of googling, finding tips here and there, I got it to work.
To produce a PDF file, your best bet is to use an existing library. iTextSharp is a powerful PDF generation library for .NET (it's a port of the Java iText library). One downfall is a paucity of good free documentation (although Mike was very helpful: http://www.mikesdotnetting.com).
So, with a reference to itextsharp.dll and a couple of usings in the code (iTextSharp.text and iTextSharp.text.pdf), this is basically all you need to generate PDF label sheets from your MVC Controller:
To produce a PDF file, your best bet is to use an existing library. iTextSharp is a powerful PDF generation library for .NET (it's a port of the Java iText library). One downfall is a paucity of good free documentation (although Mike was very helpful: http://www.mikesdotnetting.com).
So, with a reference to itextsharp.dll and a couple of usings in the code (iTextSharp.text and iTextSharp.text.pdf), this is basically all you need to generate PDF label sheets from your MVC Controller:
public ActionResult LabelsPdf()
{
// Open a new PDF document
const int pageMargin = 5;
const int pageRows = 5;
const int pageCols = 2;
var doc = new Document();
doc.SetMargins(pageMargin, pageMargin, pageMargin, pageMargin);
var memoryStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
// Create the Label table
PdfPTable table = new PdfPTable(pageCols);
table.WidthPercentage = 100f;
table.DefaultCell.Border = 0;
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
foreach (var Thing in YourCollectionOfThings)
{
#region Label Construction
PdfPCell cell = new PdfPCell();
cell.Border = 0;
cell.FixedHeight = (doc.PageSize.Height - (pageMargin * 2)) / pageRows;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
var contents = new Paragraph();
contents.Alignment = Element.ALIGN_CENTER;
contents.Add(new Chunk(string.Format("Thing #{0}\n", Thing.ThingId), new Font(baseFont, 11f, Font.BOLD)));
contents.Add(new Chunk(string.Format("Thing Name: {0}\n", Thing.Name), new Font(baseFont, 8f)));
cell.AddElement(contents);
table.AddCell(cell);
#endregion
}
table.CompleteRow();
doc.Add(table);
// Close PDF document and send
pdfWriter.CloseStream = false;
doc.Close();
memoryStream.Position = 0;
return File(memoryStream, "application/pdf");
}
27 Jan 2012
Windows Server 2008 - MVC 3 - getting 404 errors from IIS 7
Well this one got us stumped for a long time this afternoon, and not even the mighty StackOverflow could help us!
We have an MVC 3 site that was running happily in dev, but when the client asked us to install it on their server (Windows 2008 SP2 (NOT R2), IIS 7.0), it wouldn't work; every View returned a 404.
We had just installed MVC 3 and ASP.NET 4, so we went though all the usual suspects:
Eventually a random comment on some forum led us to the KB980368 Hotfix. That solved the problem!
We have an MVC 3 site that was running happily in dev, but when the client asked us to install it on their server (Windows 2008 SP2 (NOT R2), IIS 7.0), it wouldn't work; every View returned a 404.
We had just installed MVC 3 and ASP.NET 4, so we went though all the usual suspects:
- Integrated pipeline
- Permissions
- runAllManagedModulesForAllRequests in system.webserver
Eventually a random comment on some forum led us to the KB980368 Hotfix. That solved the problem!
VAG-COM / VCDS in Yorkshire for Audi A4 B7 Cabriolet
I wanted to change some settings on my A4 Cabriolet. The settings were:
So after an hour of waiting at Harrogate Audi in their swanky coffee area, they finally told me theirmonkeys mechanics had been unable to make the changes, and couldn't spare any more time looking at it. They also said not to believe everything you read on the Internet.
BAD HARROGATE AUDI, NO BISCUIT.
Well, just wanted to say that a good chap called Chris Dodgson from Auto Diagnostic Services drove his van round to my office today, plugged in his VCDS laptop and sorted all the changes in about 10 minutes.
It goes to show that Harrogate Audi is just a showroom, and the so-called Audi specialists who work in their service department don't know how to do simple electronic changes on Audis. What a ridiculous situation.
- Turn off selective locking
- Turn off speed locking
- Turn off seatbelt warning (module 17 penultimate code flip digit to 0)
- Enable telephone voice control (module 77 penultimate code flip digit from 0->2)
So after an hour of waiting at Harrogate Audi in their swanky coffee area, they finally told me their
BAD HARROGATE AUDI, NO BISCUIT.
Well, just wanted to say that a good chap called Chris Dodgson from Auto Diagnostic Services drove his van round to my office today, plugged in his VCDS laptop and sorted all the changes in about 10 minutes.
It goes to show that Harrogate Audi is just a showroom, and the so-called Audi specialists who work in their service department don't know how to do simple electronic changes on Audis. What a ridiculous situation.
18 Jan 2012
ValueInjecter - matching nullable to non-nullable values
Here's a little ValueInjecter plugin I wrote to handle ValueInjecter matching for properties where the name matches but one is nullable and the other is not.
So I just use it like so:
public class NullableInjection : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == c.TargetProp.Name &&
(c.SourceProp.Type == c.TargetProp.Type
|| c.SourceProp.Type == Nullable.GetUnderlyingType(c.TargetProp.Type)
|| (Nullable.GetUnderlyingType(c.SourceProp.Type) == c.TargetProp.Type
&& c.SourceProp.Value != null )
);
}
protected override object SetValue(ConventionInfo c)
{
return c.SourceProp.Value;
}
}
So I just use it like so:
existingCompetitor.InjectFrom<nullableinjection>(importedCompetitor);
14 Dec 2011
Visual Studio Javascript Function Folding - Code Region Outlining
I thought to myself, as I swam through an ASP.NET Razor View crammed with Javascript functions, 'SURELY someone has made a VS plugin that does function code folding?'.
Yup, Microsoft has. http://visualstudiogallery.msdn.microsoft.com/872d27ee-38c7-4a97-98dc-0d8a431cc2ed
Yup, Microsoft has. http://visualstudiogallery.msdn.microsoft.com/872d27ee-38c7-4a97-98dc-0d8a431cc2ed
9 Dec 2011
Visual Studio RegEx Regular Expression Cheat Sheet
Why MS decided to use a non-standard RegEx parser for Visual Studio's Find/Replace controls, I don't know. All I do know is that I forget the syntax EVERY FRIGGING TIME in the heat of battle. So, here's the skinny:
Match group definition using CURLY BRACE.
Match group replacement using BACKSLASH.
So to turn <bob> to *bob* use:
\<{.@}\>
and replace with
*{\1}*
NOTE the .@ is the VS 'non-greedy' match pattern
Match group definition using CURLY BRACE.
Match group replacement using BACKSLASH.
So to turn <bob> to *bob* use:
\<{.@}\>
and replace with
*{\1}*
NOTE the .@ is the VS 'non-greedy' match pattern
15 Nov 2011
IE8 window.location HTTP_REFERER
Stupid HTTP variable, HTTP_REFERER. It's spelt wrong for a start.
Anyway, to cut a long, stupid story short, an HTTP call invoked from a javascript window.location change will usually set the HTTP_REFERER variable on IIS because the browser sends a Referer header.
I say 'usually' because it DOESN'T FRIGGING WORK in IE8. IE9, FF, Chrome, fine. IE8, nope.
Sigh.
Anyway, to cut a long, stupid story short, an HTTP call invoked from a javascript window.location change will usually set the HTTP_REFERER variable on IIS because the browser sends a Referer header.
I say 'usually' because it DOESN'T FRIGGING WORK in IE8. IE9, FF, Chrome, fine. IE8, nope.
Sigh.
9 Nov 2011
Rockalldll.dll missing / not found error - Rockall Heap Manager on Windows 7
This week's mystery was a recurring error message in Windows 7 about a missing DLL - RockallDll.dll.
It sounded dodgy, and a quick google returned about a million nasty DLL sites purporting to offer a nice shiny replacement DLL, no doubt festooned with viruses and trojans.
So I didn't do that. I had to do some pretty deep searching to find out that this DLL was originally installed as part of Hermann Schinagl's Link Shell Extension that I installed ages ago. For some reason I had installed it on my E drive. Well, I don't know why it had stopped working, but I uninstalled it, downloaded the latest version and installed it on C drive instead.
Problem solved, woohoo. And I do recommend LSE as a quick way of creating symbolic links in Windows.
It sounded dodgy, and a quick google returned about a million nasty DLL sites purporting to offer a nice shiny replacement DLL, no doubt festooned with viruses and trojans.
So I didn't do that. I had to do some pretty deep searching to find out that this DLL was originally installed as part of Hermann Schinagl's Link Shell Extension that I installed ages ago. For some reason I had installed it on my E drive. Well, I don't know why it had stopped working, but I uninstalled it, downloaded the latest version and installed it on C drive instead.
Problem solved, woohoo. And I do recommend LSE as a quick way of creating symbolic links in Windows.
3 Nov 2011
IntelliType Pro 8, Media Keys and iTunes 10.5
I just got me a new MS Sidewinder X4 keyboard, and very swanky it is too. However, it requires Microsoft IntelliType Pro to work, and after installing that, I was annoyed to find that the Media Keys (Play, Pause, Prev Track, Next Track) didn't work with iTunes.
Here's what I found out.
Here's what I found out.
- iTunes control worked fine if I uninstalled IntelliType Pro, but this was no good because then I lost the use of the keyboard's Macro keys.
- By doing the trick of launching iTunes as administrator (right click on the iTunes icon - choose "Run as administrator") , iTunes can be controlled from the Media keys - But ONLY when iTunes is in Window Focus.
- Launching the IntelliType process (itype.exe) as administrator makes no difference.
However I have found a Heath Robinson / Rube Goldberg stylee solution!
HKTunes Portable is free software that launches iTunes for you and attaches custom key handlers to the Play/Pause, Next/Previous controls. So I configured that to use some wild, unlikely key combos, and then I set up those same key combos as Macros for the Media Keys in IntelliType Pro (Start > Microsoft Keyboard > Key Settings).
The only drawback is that you have to remember to launch HKTunes rather than iTunes, but I just replaced the pinned launcher in my task bar so it's not a problem. Wish Microsoft would sort out the underlying prob though!
UPDATE
I found a better solution! Check this guy's EXCELLENT MediaKeys app!
UPDATE
I found a better solution! Check this guy's EXCELLENT MediaKeys app!
Subscribe to:
Posts (Atom)