Posts

Code Design - Choosing Abstract or Interface - Level 100

More often we wont get a chance to work on these aspects in early stage of your career. Even at the time of mid career getting a work related to API designing/Development is bit difficult. Unless you have a very good mentor in your initial career you will never get to know the real time. And you will end up in theoretical knowledge if you are computers academics. Others need to witness nightmares :-) Always try to implement good design in your day to day work. Never try to concentrate on these concepts for the matter of career growth. Only the passion to write a good code lets you to remember things. Design patterns are not meant for only senior programmers. You need to have a little bit knowledge on nitty-gritty of OOP/OOD concepts. And I need to say that continuous practice only lets you to excel further in OOP/OOD concepts irrespective of your experience levels. Here are some basic recommendations to help you to decide whether to use an interface or an abstract class to provid...

C# - Finding connection string - Easy way

Most of the times we used to face difficulty while creating connection strings in our C# applications and especially when you are working in data intensive apps. I used to do lots of permutations and combinations (due to drivers installed) to find out the proper connection string in my initial career. Here I would like to explain easiest way and proper manner to test the connection ahead to actual usage. For database connections in C# apps you often need to supply an ADO Connection String. Here is a tip how to generate such string very easily. (Windows only) Go to your desktop  Right click and choose NEW/Microsoft Data Link - if you don't have this option on your menu, just choose NEW/Text Document  If you have make a new text file - change the file extension from .txt to .udl  Double click on the new file - you will see now the Microsoft Connection wizard (Data Link Properties)  Choose your database driver and connection properties  Test the connec...

Generics in C# - Straight

In programming, generics are also known as parametrized types or parametric polymorphism . Generics have been added to the .Net framework since version 2.0. They introduce the type parameter T to methods, classes (or types) and interfaces. There is a documented conversation about Generics in C#, Java and C++ with Anders Hejlsberg (The original author of Turbo Pascal , the chief architect of Delphi and the current lead architect of C#). With using generics, your code will become reusable, type safe (and strongly-typed) and will have a better performance at run-time since, when used properly, there will be zero cost for type casting or boxing/unboxing which in result will not introduce type conversion errors at runtime. The compiler of C# will be able to detect any problems of invalid casting at compile time. Reusability with generics means that you will be able to create a method or a class that can be reused with different types in several places. Now with generics you will be writin...

A quick Utilities, Tips & Tricks for web dev only

​Hi,​ While doing some validatioons you want to validate character count. Then here is the link http://www.javascriptkit.com/script/script2/charcount.shtml Do you want to validate the CSS you wrote? Then here is the link http://jigsaw.w3.org/css-validator/#validate_by_input You want to decode or encode a character. Then here is the link http://www.base64decode.org/ Do you want to add days, months or years to a DATE? Then here is the link http://www.timeanddate.com/date/dateadd.html ​Tired of writing SQl and Do you love to work on LINQ but don't have a proper IDE? Then here is the link http://www.linqpad.net/ Do you want to format the CSS you wrote in your own standards? Then here is the llink http://www.lonniebest.com/FormatCSS/ ​Do you want to format XML, JSON, HTML, HTML5, RegEx or SQl or else any? Here is the link http://www.freeformatter.com/html-formatter.html​ Do you want to quickly write html, javascript and css and see how it behaves in browser? Then ...

How to avoid a CORS problem?

This summary is not available. Please click here to view the post.

My Learnings with current Mobility/Web market trend

Hi, First of all a big happy new year to all my visitors. Today I would like to share my learning about the current market trend as the winter is going to complete and all the market will start to bounce back in next 30-45 days. At the starting of my career I used to be in a big misconception like any fresher. I always think ASPX, PHP, JSP, CFM etc are far better than the plain HTML pages. I really felt painful to write plain prototype based html with javascrript. But now after seeing the market trends and mobility boom I realized the true strength & can confidently say it like HTML is far far away from any other above mentioned server based web technologies. In the coming future the only UI we will see with is developed only with HTML5. Question : So WHY previously we didn't concentrated on these things, Because HTML already there and which works only via http protocol means we have web services etc to work on. Answer : Previously the handshakes will be based on SOA...

Implementing impersonation in c#

Hi, Today am going to explain about the implementation of impersonation. This code we can use in many contexts like web, windows, services etc. The main things to be a pr requisite are user context should have a proper permissions and necessary references were made.( Please see the ‘usings’ here in the snippet) This impersonation is needed mainly in few cases like to do some server side functionality means printing, network printing accessing, checking available printers, IO operations, etc all comes under particular user context. This thing needs to be done in many cases as the IIS application runs under very less privileges. So the IIS_User need to be impersonated before doing few operations. Here am attaching the complete class for making it more simpler to benefit you people. You just need to copy this class in to your project and use a snippet as like this using (Impersonation impersonation = new Impersonation(impersonationUsername, impersonationDomainName, impersonationP...

Generate random string using c#

Hi, Today am blogging a simple concept related to generating a random sring. In realtime it will be used mainly for generating captcha, random password, Temporary objects etc. Here we can send the string length expected. Also You can modify he complexity. Its a simple code and easily understandble. Here is the code snippet. /// /// Creates the random string based on the length provided /// /// Length of the password. /// public static string generateRandomString(int PasswordLength) { string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"; Random randNum = new Random(); char[] chars = new char[PasswordLength]; int allowedCharCount = _allowedChars.Length; for (int i = 0; i < PasswordLength; i++) { chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())]; } return new string(chars); } hope it helps you. Regards, Pavan N

Finding file size in bytes on serverside using c#

Hi, This requirement will come mainly while doing I/O operations (in my opinion it is obviously one of tedious taks carried by server). Here am showing you the snippet of code to find out the number of bytes of a file. here am using this code to validate the size of the file on server side. Which is a common requirement while doing file uploads. Here is the snippet. private static long GetFileSizeOnDisk(string filelocation) { FileInfo info = new FileInfo(file); uint dummy, sectorsPerCluster, bytesPerSector; int result = GetDiskFreeSpaceW(info.Directory.Root.FullName, out sectorsPerCluster, out bytesPerSector, out dummy, out dummy); if (result == 0) throw new Win32Exception(); uint clusterSize = sectorsPerCluster * bytesPerSector; uint hosize; uint losize = GetCompressedFileSizeW(file, out hosize); long size; size = (long)hosize Thanks & Regards, Pavan N

Number to String conversion

Hi, Generally in finance or banking or this kind of requirement is needed. Its more or less a common thing to convert a number to a string. I used this to convert currency to words format. Here is the snippet. /// /// Numbers to text. /// /// The value. /// public string NumberToText(int value) { string[] num0to10 = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }; string[] num11to20 = new string[] { "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty" }; string[] num20to100 = new string[] { "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety...

Download file using WCF REST services

Hi, Generally we used to download a file using a handlers and sending parameters as a querystring (lets say download.ashx). Here few cases we might need to impersonate the user also. It purely depends on environment. Here is a sample code snippet just for reference. if (extension.Trim() == ".pdf") { context.Response.Clear(); context.Response.ContentType = "application/pdf"; context.Response.AddHeader("content-disposition", "attachment;filename=" + queryFile + ".pdf"); context.Response.BinaryWrite(System.IO.File.ReadAllBytes(file)); } But instead of that we can download a file as a stream using a WCF REST service too. Here is the code snippet for declaration in interface .. /// <summary> /// To fetch documents /// </summary> /// <param name="filePath">Represent text of filepath</param> /// <param name="fileName">Represents text of filename...

Validate domain user using c#

Hi, Here am sharing code related to validate a domain user againsta active directory. This is well used for implementing authentication and authorization in asp.net or windows application. the call should be sent a [domainname]\[username] and [password] public int VerifyUser(string userName, string password) { try { string domainName = string.Empty; string domainUserName = string.Empty; string[] tempString = null; string[] delimiter = new string[] { @"\" }; tempString = userName.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); if ((tempString.Length == 0) || (tempString.Length == 1)) { return 1; } domainName = tempString[0]; domainUserName = tempString[1]; LDAP.Domain_Authentication imperson = new LDAP.Domain_Authentication(domainUserName, password, domainName); if (imperson.IsValid()) { return 2; } return 3; } catch (Exception ex) { return -1; } } And the LDAP class i...

server side printing / PDF conversions using c# and asp.net/wcf service

Hi All, I know its a tough task to search this kind of  requirement . As i did same and endup with a solution like using Crystal reports for printing on network printer. And a WKHTML library to convert html pages to either images or PDF. There inturn i can convert TIFF files to PDF using Ghostscript(If you need this code please drop a comment). Now coming to this server side network printing. At first we need to have a following things Visual studio Crystal reports for Visual studio WKHTMLTOPDF/WKHTMLTOIMAGE A proper permissions on a folder where we are going to store our images or PDF files etc which need to be sent to a printer And the code flow is first i will scale the image to a standard size with the following code  prepare a HTML file having image tag with SRC as the image file which needs to be printed then i will call WKHTMLTOIMAGE library  to convert public static Image ScaleImage(Image image, int maxWidth, int maxHeight) { ...

Part– II : Coding standards

These are the main checklist I used to follow in day to day code development and maintenance. Hope this might help some of you new people. If there is any mistakes/typo please do not hesitate to comment. Coz these are my personal views and opinions 1. Need to avoid unnecessary comments especially while releasing a build 2. Reduce as much lines of code as possible 3. if a variable is used only once then declare it locally if possible 4. Follow the naming conventions especially in DB layer. This helps a lot I promise :) 5. Method name should tell what it does. Do not use miss-leading names. If the method name is obvious, there is no need of documentation explaining what the method does 6. Make a note or summary at top of the page if the code was developed my multiple persons. it helps to track when and which kind of development started 7. Make sure that the required input values gets logged while logging the error message in catch block 8. Avoid ...

Part– I : Coding standards

Coding practise defines the developer awareness of writing a good code. but not his technical expertise..... I mean a little :) As of my experience with different projects across the domains and technologies & tools I got  a chance to work on enhancements, bug fixings, code reviewing, code development from the scratch level, enhancing my own code which was written years back by myself  :) , reading and preparing documentation on reviewing the code base from the client..... and many more. I got realised few points which i would like to blog.