Categories
Article ASP.NET Bookmarked C#.NET PHP Programming VB.NET/VB

WKHTMLTOPDF: A Free PDF solution

As any other developer we often find it difficult to get a Website to print something accurately, specially when printing labels for mailing as they are outside the default print area for most browser and no client want to adjust or memorize the print setting to adjust them manually. Also it doesn’t make sense to relay on client to do adjustments.

So the only universal solution I found is.. of course PDF files. But how do I create PDF. Well easy enough use some library. I did that I use iTextPDF very good Library that works for my C# project well for couple of years. But then what is the problem now? Well iTextPDF is now have commercial license at ridiculous pricing in fact it is now most expensive to any other paid software. Yet another Open Source project going paid on success. 

Also, there is a problem with library like iTextPDF that they need you to draw each and every element programmatically more often or not. And you find yourself caught in a web of long written code. I remember one of my report’s PDF generation code goes 1000 lines. I hate it. So, the best solution is to Generate PDF from a HTML source. We can handle HTML much better way and rather easily with our skills as web programmer. And even a designer in team can change PDF design giving time for programmer for other work. 

In this regard I look for free alternative of iTEXTPDF, which does have HTML parser to generate PDF from HTML file, and my search for now ends on http://code.google.com/p/wkhtmltopdf/ (wkhtmltopdf ) project. A Simple Executable that I can call from a my code to execute and it does the parsing. 

It is not really a good idea to call it through web page, but we should create a Windows service and use it there, but I have successfully test it to work on web server with quite a load and at least with my limitation to access Windows Service I can live with it running through HTML page, after all my PDF is just 1-2 page and it works fine. Will post code for it some other day. But do try it if you want a good and quick solution to your problem.

Categories
.NET Automation C#.NET

Regasm 64bit or 32bit through Windows Installer in Visual Studio

Well, nothing much to say, this article is taken from http://leon.mvps.org/DotNet/RegasmInstaller.html. Thanks Leon, just for my own bookmarking here are step. [Article is taken as it is from Leon’s article.]

Realising that I couldn’t be the only person who’s having this problem, I’ve decided to share the code to assist others. All you need to do is follow the below steps:

  • In your main project (the one containing the class you want to register), right click the project file and select Add / New Item and select Installer Class. Call it something like clsRegisterDll.cs
  • In the designer that appears, click ‘Click here to switch to code view’ or right click the clsRegisterDll.cs file in solution explorer and select View Code
  • Replace the code in the window with the code listed below, and make sure you change ‘YourNamespace’. This has to match with the namespace in the clsRegisterDll.Designer.cs file.
  • Compile your project
  • In your installer, make sure you have added your dll to the Application Folder, and then right-click the installer project and select View / Custom Actions
  • Right-click Install, and then click Add Custom Action
  • Double click on Application Folder, and then on your dll
  • Do the same for the Commit action
  • Build and test your installer
  • You should now have an installer that registers your dll using regasm /codebase.

clsRegisterDll.cs:

using System.ComponentModel;
using System.Configuration.Install;

namespace YourNamespace
{
[RunInstaller(true)]
public partial class RegisterDll : Installer
{
public RegisterDll()
{
InitializeComponent();
}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);

// Get the location of regasm
string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";
// Get the location of our DLL
string componentPath = typeof(RegisterDll).Assembly.Location;
// Execute regasm
System.Diagnostics.Process.Start(regasmPath, "/codebase \"" + componentPath + "\"");
}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
}
}
}

 

Categories
.NET Automation C#.NET VB.NET/VB

Excel Automation Add-In for Office 64bit Check List

After days of research, hit and trail; today I finally got a working 64bit Excel Automation Add-In. I am using Visual Studio 2010 [.NET 4.0 client profile framework]. 

1. Create a Project using Visual Studio Office 2010 template “Excel 2010 Add-In”. [I opt for this as I need Ribbon UI too, if you don’t need that you can use Class library project as well]

2. Create a New class and follow this article from Eric ( Excel Automation AddIn in Visual studio ). This article covers 95% of stuff you need it to work. Thanks Eric your article is still valid after 3 yrs and new version than those mention there.

3. Make sure you, have a Signed Assembly. I am not sure if it is really true, but when I was manually registering my assembly in failed cases, it warn me to sign them, though it says “registration succeed”.

4. Good thing is I am creating Add-IN using 32bit machine, and even though my addin works for 64bit. I do not define any specific CPU, just use ANYCPU flag where applicable.

5. Make sure you do not check “Make COM Visible” under Advance setting of your Assembly properties. Again I am not sure, if it is true, but it works when I have it OFF.

6. Do not check “Register for COM” under the BUILD Tab of Project property in Visual Studio. We need to manually register using 64bit regasm through our Setup or command line as you like.

7. [Guess not important or relevant], I have by default Sign for ClickOnce mainfest enable. Actually it is enable for Template project by Visual studio. I keep it that way, doesn’t harm to have it.

8. Make sure to create a good GUID for Class definition as define in article. There is another GUID used in Assembly information, I try to match them and it didn’t work. When it works they have different GUID. Though have it same or different should make difference according to me.

9. I define a ProgID on top of my class like

[ProgId("EVA.Excel")]
[Guid("A4C56D50-D25D-41BA-AE3E-9BE5A56736D3")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Sound

{

}

I guess that is all what I make changes and have it working. Best of luck.

 

 

Categories
.NET ASP.NET C#.NET Programming

Sandcastle: Code Documentation in C#

Over the years I use lot of documentation and those help me build my software with ease. However, never need to build one for my projects as they are not quite often big and what more never need to do so, as they are quite handful of function in complete project. But now in order to upgrade my coding and making it more friendly for my brain. I finally was looking for a decent method to start documenting my code in separate code files much like PHP or MSDN documentation. 

The best was to start with .NET documentation tools as they are more readily available. After searching and trying to make sense what all these tools are good for, I finally reach to Sandcastle. Install it, but found nothing in my start menu. Read about it, seems a command line tool. But I need GUI, been a lazy programmer. Well I got the answer in term of Sandcastle Help File Builder on Codeplex (http://shfb.codeplex.com/) . Looks promising, I install it. And within 3 minute of installation, my documentation is ready. How?

Simple. I open the software, Add Documentation Source as my project SLN file, click build and it create file for me. I need nothing else. There maybe lot of configuration to do, but for first time, it is more than enough. I have been commeting my code day I start programming. And from about a year or so, I start using those /// commenting as in XML generation tool within Visual Studio. But never compile them in help file. 

Now I know easiest method to do. i.e. open Builder GUI, click build [For second time of course]. I will keep generating lot of help file so I can check them back when needed in future. Changing my programming style when nobody is there to talk or see face to face is enjoyable. Me and my computer sitting alone and doing lot of new learning. Going back to days of programmer is really good to continue my learning process that I left 4 years ago.

I am still good and hope to improve further.

Categories
.NET ASP.NET C#.NET VB.NET/VB WPF

GDI+ Generic Error

I see lot of people asking about GDI+ Generic Error. Since this is “generic” error nobody can tell what is wrong with code. So did a work around and get it working. I had this error quite a time in early days of my programming and just found same error in one of junior’s code. Well from my experience I can say one thing. GDI+ Generic error 99.9% means “developer miss a detail or overlooked something”. Most common reason of error are negligence of

1. Path should be physical path

2. All folder should present before you create file.

3. Previous files should be in write mode if case of overriding files.

4. Make sure permission are for writing to User of process.

5. Do not include any special character which windows do not permit in folder name or file name.

6. FILE FORMAT has nothing to do with this error most often.

7. Recheck permission if you try all above.

8. Recheck Path again :).

Point #7 and #8 are most important solution and always works for me.

Categories
.NET ASP.NET C#.NET Concept/algorithm Database VB.NET/VB

Static Object in ASP.NET

Recently I found a problem in one of application we developed. Here my colleague use a Static SqlConnection object through a class in his code. He did that to save his effort of writing and Connection everytime. Everything works great during our development and online reviewing. However as soon as we lauch website in beta mode, it start creating problem which seems bit random in occurance and hence were hard to track. During this time I get involve in the project development and I was not very big fan on Static object and start to think against that code, which looks good to me at first. However for some reason I miss the basic of C# and ASP.NET and hence it took a while to recognize the problem was indeed the static code. So for those who did same mistake here is explanation of why Static object didn’t work in ASP.NET for connection.

When ASP.NET application is first called the complete code is loaded in Memory, specially the pages in App_Code folder and they act as part of web server and not as standard PHP site where they run and loaded on each request. Hence, once this happens all static object become static in memory for each and every request your server will get. This is indeed good way to share information between all User and all request, if that help your case. But in most general case you never want to share information between user in this dynamic method, which is not permanent. Anyways, for connection it really doesn’t make a difference if you share the object as you anyways want to open connection and close it as soon as you are done. In most case you have 1 simple insert or fetch query to run. And in a website with several users at a time it hardly make a difference as Sqlconnection are open and close in jiffy and took less than 1 second in most cases. For a shopping cart which has 100 -200 order per month this case will work just fine.

However, since connection is shared, there is every possible chance that if you fire a SQL query for say Reader, it get shared with some user on some other session and this is what you don’t want. It happens to our project when we have Bulk CSV upload which usually take 30-50 seconds, but if someone during this time fires and open other page of website they start to get random Database related errors. From SqlReader is close to field “Xyz” is not available, as they tend to share the connection with other requests.

Hope you avoid those problem. The colleague who did this in our project is not working with us anymore, but hey, where ever you are , please don’t punish us like this again !!!

Categories
.NET ASP.NET C#.NET

Session in ASP.NET handler Page

To use Session data in Handler Class you need to implement

IRequiresSessionState

which is part of Following namespace (using for helping copy/paste )

using System.Web.SessionState;

Self reminder 🙂