Friday, 13 January 2012

Explanation about Execute Non Query, Execute Scaler, Execure Reader


Do not use: when database query is going to provide for sure exactly 1 record. It may be getting record by its id (which is PK in the database) - GetOrderById and such. In this case use ExecuteNonQuery with output parameters.
Use: when database query is going to provide a set of records. It may be search or report.


Use: when we are talking about a single database record - in Update, Insert, Delete and Get by Id. In all these cases we can use input/output/input-output parameters. Please note that from the application architecture point of view it is also good practices when your Insert and Update stored procedure returns changed record exactly like Get By Id method does.


Do not use: when database query returns a single value and this value can be defined as parameter in T-SQL. ExecuteNonQuery with output parameter(s) is always preferred in this case since it is more flexible, tomorrow there will be 2 values therefore having ExecuteNonQuery we do not need to change method signatures.
Use: when database query returns a single value and this value cannot be defined as output parameter, because of T-SQL type limitation for variables. For example type image cannot be output parameter in MSSQL.

Wednesday, 28 December 2011

Access Modifiers (Access Specifiers) in Dotnet

public :

public is the most common access specifier in C# . It can be access from anywhere, that means there is no restriction on accessibility. The scope of the accessibility is inside class as well as outside. The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private :
The scope of the accessibility is limited only inside the classes or struct in which they are declared. The private members cannot be accessed outside the class and it is the least permissive access level.

protected :
The scope of accessibility is limited within the class or struct and the class derived (Inherited )from this class.

internal :
The internal access modifiers can access within the program that contain its declarations and also access within the same assembly level but not from another assembly.

protected internal :
Protected internal is the same access levels of both protected and internal. It can access anywhere in the same assembly and in the same class also the classes inherited from the same class

What is Value Types and Reference types and difference between boxing and Unboxing

A Type is defined as a set of data and the operations performed on them. CSharp is a strongly typed language. The CSharp type system contains three Type categories. They are Value Types , Reference Types and Pointer Types . The Value Types store the data while the Reference Types store references to the actual data. Pointer Types variable use only in unsafe mode. The Value Types derived from System.ValueType and the Reference Types derived from System.Object .

The main difference between Value Types and Reference Types is that how these Types store the values in memory. Common Language Runtime (CLR) allocates memory in Stack and the Heap . A Value Type holds its actual value in memory allocated on the Stack and Reference Types referred to as objects, store references to the actual data. In C# it is possible to convert a value of one type into a value of another type . The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.

Wednesday, 21 December 2011

How to write basic stored procedures (update, save, delete)

Delete row from given id, here commentId is id which is provided by user and TUSER_COMMENT is table name , USER_COMMENT_ID is id column name and proc_DELETE_COMMENT is procedure name



delete from TUSER_COMMENT where USER_COMMENT_ID=@commentId

Insert Row
CREATE PROCEDURE proc_HitList_Registration(@Name varchar(25),@Email varchar(25),@Pwd varchar(15),@Gender varchar(6),@DOB datetime,@Role varchar(50))

Insert into TUSER_REGISTRATION values(@Name,@Email,@Pwd,@Gender,@DOB,@Role);

Update Row
CREATE PROCEDURE proc_HitList_UpdateProfile

(@Name varchar(25),@Email varchar(25),@Pwd varchar(15),@Gender varchar(6),@DOB datetime,@Role varchar(50))
Update TUSER_REGISTRATION Set User_Registration_Name=@Name,User_Registration_Pwd=@Pwd,User_Registration_Gender=@Gender,User_Registration_DOB=@DOB,User_Registration_Role=@role where User_Registration_Email=@Email

Select Row



Wednesday, 7 December 2011

Generate trace file to handle all types of exceptions

Step 1: catch your exceptions

catch (Exception ex)

   ErrorHandler e2 = new ErrorHandler();

where ErrorHandler is a class name and HandleException is method name where pe pass this exception.

Step 2: Create a seperate class file for handling all types of exception and also for generating trace or   error log file.

public void HandleException(Exception exceptionName)

 string ExpName = Convert.ToString(exceptionName.GetType().FullName.Remove(0,7));
 switch (ExpName)
      case "Data.SqlClient.SqlException":
      case "ArgumentException":
       case "FieldAccessException":

        // you can search aand so many exception here

where WriteError is method name where we pass the exception obj and retrive all information through this method

Step 3:
write the definition of WriteError method 

private void WriteError(Exception errorMessage)

     string path = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
     if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
     using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
         w.WriteLine("\r\nLog Entry : ");
         string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() + ". Error  Message:" + errorMessage.Message;
        string err1 = "Error Type:" + errorMessage.GetType();
        string err2 = "Error BaseException:" + errorMessage.GetBaseException();
        string err4 = "Error StackTrace:" + errorMessage.StackTrace;

and also add these namespace

using System.IO;

using System.Text;

Tuesday, 6 December 2011

Chicken Story (mind blowing climax)

A farmer rears twenty-five young hens and one old cock. As he feels that the old cock could no longer handle his job efficiently, the farmer bought one young cock from the market.

Old cock to Young cock : "Welcome to join me, we will work together towards productivity.
Young cock : What you mean? As far as I know, you are old and should be retired.
Old cock : Young boy, there are twenty-five hens here, can't I help you with some?
Young cock : No! Not even one, all of them will be mine.
Old cock : In this case, I shall challenge you to a competition and if I win you shall allow me to have one hen and if I lose you will have all.
Young cock : O.K. What kind of competition?
Old cock: 50 meter run. From here to that tree. But due to my age, I hope you allow me to start off the first 10 meters.
Young cock : No problem ! We will compete tomorrow morning.
Confidently, the following morning, the Young cock allows the Old cock to start off and when the Old cock crosses the 10 meters mark the Young cock chases him with all his might.
Soon enough, he was behind the Old cock back in a matter of seconds.

Suddenly, Bang! ...... Before he could overtake the old cock, he was shot dead by the farmer, who cursed, "Hell ! This is the fifth GAY chicken I've bought this week !"

Monday, 5 December 2011

Different types of JIT in DotNet

JIT compiler is a part of the runtime execution environment.

In Microsoft .NET there are three types of JIT compilers:

Pre-JIT :- Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application.
Econo-JIT :- Econo-JIT compiles only those methods that are called at runtime However, these compiled methods are removed when they are not required.
Normal-JIT :- Normal-JIT compiles only those methods that are called at runtime .

These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.