Thursday, December 20, 2012

How to solve "CustomError for 403 Forbidden not working"?


In IIS 7 add following settings into web.config
  
    
      
      
      
      
    
   
  

How to use QR code in ASP.NET MVC?

The lib is: QRCODENET
http://qrcodenet.codeplex.com/


public ActionResult QRImage(int id)
        {
            string url = string.Format("http://myevents.apphb.com/event/details/{0}", id);
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = qrEncoder.Encode(url);

            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            MemoryStream ms = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
            byte[] image = ms.ToArray(); ;
            return File(image, "image/png");
        }

Wednesday, December 19, 2012

Reverse polish notation in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Threading;
 
namespace RPNEvaluator
{
    class RPNEvaluator
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
 
            string rpn = "3 4 2 * 1 5 - 2 3 ^ ^ / +";
            Console.WriteLine("{0}\n", rpn);
 
            decimal result = CalculateRPN(rpn);
            Console.WriteLine("\nResult is {0}", result);
        }
 
        static decimal CalculateRPN(string rpn)
        {
            string[] rpnTokens = rpn.Split(' ');
            Stack stack = new Stack();
            decimal number = decimal.Zero;
 
            foreach (string token in rpnTokens)
            {
                if (decimal.TryParse(token, out number))
                {
                    stack.Push(number);
                }
                else
                {
                    switch (token)
                    {
                        case "^":
                        case "pow":
                            {
                                number = stack.Pop();
                                stack.Push((decimal)Math.Pow((double)stack.Pop(), (double)number));
                                break;
                            }
                        case "ln":
                            {
                                stack.Push((decimal)Math.Log((double)stack.Pop(), Math.E));
                                break;
                            }
                        case "sqrt":
                            {
                                stack.Push((decimal)Math.Sqrt((double)stack.Pop()));
                                break;
                            }
                        case "*":
                            {
                                stack.Push(stack.Pop() * stack.Pop());
                                break;
                            }
                        case "/":
                            {
                                number = stack.Pop();
                                stack.Push(stack.Pop() / number);
                                break;
                            }
                        case "+":
                            {
                                stack.Push(stack.Pop() + stack.Pop());
                                break;
                            }
                        case "-":
                            {
                                number = stack.Pop();
                                stack.Push(stack.Pop() - number);
                                break;
                            }
                        default:
                            Console.WriteLine("Error in CalculateRPN(string) Method!");
                            break;
                    }
                }
                PrintState(stack);
            }
 
            return stack.Pop();
        }
 
        static void PrintState(Stack stack)
        {
            decimal[] arr = stack.ToArray();
 
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                Console.Write("{0,-8:F3}", arr[i]);
            }
 
            Console.WriteLine();
        }
    }
}

How to change checkbox size?

The behavior is different in browsers. And maximum size is limited.


How to solve "Service has zero application endpoints" in WCF host

Check service name in config file
Make sure it is a full name with all the namespace path