Posts

Showing posts from 2015

Print a receipt using a Thermal Printer with C#.NET

Image
I’ve been asked to integrate receipt printing for a software I’ve been developing and the thermal printer my client uses is a very cheap one ordered from eBay for $36.31. There are more developer friendly feature rich printers available but they are very expensive. Who wants to use a expensive printer when the customers will throw away the receipt anyway?

UPDATE: After seeing the feature set of the more expensive Epson printer, I do think that anyone who wishes to have receipts printed, should use a feature rich printer. Some even have color printing capabilities. 



As usual, a quick google search landed me on some Delphi code and most of the code were for those expensive printers. Mostly, Epson. Then I found a link to this page sending-a-bit-image-to-an-epson-tm-t88iii-receipt-printer-using-c-and-escpos which is a dead end now. Luckily, I searched using a piece of code from SFO and it landed me here. printing-utf-8-encoded-text-in-epson-tm-t88iv-thermal-printer

But still, I didn’t quite …

Adding Syntax Highlighting To Blogger

Image
When I was searching for syntax highlighters I found this really cool library highlightjs.org and decided to use that instead of piggybacking on SO.



The steps
Don't download hightlight.jsYou need to make changes to your blogger template. Go to your blogger dashboard. ( https://www.blogger.com/ )Go to Template section on the left side bar.Backup your templateThere is a "Live On Blog" showing a screen of your current blog. under that, there is a "Edit HTML". click that button.hit CTRL+F and type "</b:skin>" and hit enter. Add these lines below "</b:skin>"

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/sunburst.min.css"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
hit CTRL+F and type "</body>" and hit enter. Add this script above "</body>"
<script&…

Audit SQL Tables with Trigger

Image
Say , you want to audit every record changes in your database tables and also, you need an easier way to query these data whenever needed, I present you, a sample SQL trigger for inserting a new audit record to audit table when an insert, update or delete operation is done on a table row.



First, the audit table: 
CREATE TABLE [dbo].[AuditRecord]( [ID] [int] IDENTITY(1,1) NOT NULL, [AuditType] [char](1) NOT NULL, [TableName] [nvarchar](50) NOT NULL, [OperationTimeStamp] [datetime] NOT NULL CONSTRAINT [DF_AuditRecord_OperationTimeStamp] DEFAULT (getdate()), [UserName] [nvarchar](50) NOT NULL, [Data] [xml] NOT NULL, CONSTRAINT [PK_AuditRecord] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

[AuditType] is either I (Insert), U (Update), or D (Delete)
[Data] is an XML field, which will contain all the IUD operations n…

ASP.NET MVC Fine Grained Identity & Access Control - Part 1

Image
In this series I will explain how I designed a generic claims based access control system ASP.NET MVC 5 with Identity 2.0. Part 2: ASP.NET MVC Fine Grained Identity & Access Control - Part 2

The method I explain here will be useful for applications with moderate complexity, which would require an end user (an admin) to define which resources a particular user has access to. Since I could not find any existing projects with what I want, I bootstrapped my own. I realized that a lot of people want to build something like this, but there are no available resources. Also, please, not that, there is a lot of room for improvement. I will be updating the github repo as I develop the blog post series.




This post targets moderate .NET skill sets. If you have no idea what a claim is, you should first go through that. And see how role based access works. And then come back to claims. Understand the difference and then come back to this post. Also, its nice if you do know what logic gates are. Mo…

Log4Net Logging Output with ReSharper Test Runner - Config

This post assumes you know what Log4Net is and how to configure it and you are using ReSharper and you are unit testing your code. If you don't its time to learn something new..

If you want your log lines to show up in resharper test output window, you need to add  TraceAppender to the appenders list.

<appender name="NetTraceAppender" type="log4net.Appender.TraceAppender, log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" /> </layout> </appender> and the use it under root tag.

<root> <level value="Debug"/> <appender-ref ref="NetTraceAppender" /> <appender-ref ref="RollingFileAppender" /> </root> If using iBatis, and you only want to see the processed SQL query, include following also.

<!-- Print only messages of level DE…

Copy SQL Table With Child Table Relaionships

dbo.RoomOccupancyRate has many dbo.OccupancyChildRate records.

How to copy them?

Following stored procedure takes care of it. It even has the option to copy only the active items.

ALTER PROCEDURE [dbo].[sp_CopyHotelContractRoomOccupancyRate]@NewHotelContractIDint,@SelectedHotelContractIDint,@UserName nvarchar(20),@ActiveItemsOnly bit,@OldDateBandIdint,@NewDateBandIdint,@OldRoomOccupancyIdint,@NewRoomOccupancyIdint AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements. SET NOCOUNT ON; IF (EXISTS(SELECT TOP 1 cr.[RoomOccupancyRateID] FROM [dbo].[RoomOccupancyRate] cr INNER JOIN [dbo].[HotelRoomOccupancy] rc ON cr.[RoomOccupancyID]= rc.[RoomOccupancyID] WHERE rc.[RoomOccupancyID]=@OldRoomOccupancyId)) BEGIN PRINT '----[dbo].[RoomOccupancyRate] table should be copied'

Conditional Filtering in SQL

Assume I need to select a set of data based on the following criteria.


If the @Active-Items-Only flag is set, then select only the active itemsotherwise select all active and non-active items.DECLARE@ActiveOnly bit =1;-- select only activeSELECT[HotelContractID],[RoomTypeID],[TravelStart],[TravelEnd],[ReleaseTypeID],[ReleaseValue],[Active]FROM[dbo].[RoomAllocation]WHERE[HotelContractID]=124AND(CASE-- if active only selected, -- check only active = 1 rows in the table and return 1WHEN@ActiveOnly =1AND(Active =1)THEN1-- if active and inactive both selected, -- check for both active = 1 and active = 0 rows in the table and return 1WHEN<

Random Background & Text Color Generation - Javascript

The Code in JSFiddle:



References/Credits:
http://stackoverflow.com/a/9601429/959245
http://stackoverflow.com/a/12043228/959245
http://stackoverflow.com/a/13542669/959245

Extract all MVC / WebApi area/controller/actions

I did some search and created my own based on the answers found in SO.

Extract all MVC / WebApi area/controller/actions - Gist

// http://stackoverflow.com/questions/5508050/how-to-get-a-property-value-based-on-the-name // http://forums.asp.net/t/1600843.aspx?How+list+all+of+the+Actions+in+an+MVC+application+for+security+audit+ // http://stackoverflow.com/questions/5801630/mvc-get-all-action-methods // http://stackoverflow.com/questions/21583278/getting-all-controllers-and-actions-names-in-c-sharp // http://stackoverflow.com/questions/15690834/asp-net-mvc4-list-of-all-areas // http://stackoverflow.com/questions/1091853/error-message-unable-to-load-one-or-more-of-the-requested-types-retrieve-the-l /// <summary> /// Extract all MVC/WebApi area/controller/actions /// </summary> publicclassResourceHelper { privateconststring NamespacePrefix = "github"; publicstatic List<ApplicationResource> GetResources() { var resources = new List<ApplicationResource>()…