31 January 2013

String and String Builder in C#

  1. System.String is immutable.(Non updatable) 
  2. System.StringBuilder is mutable (Updatable) 
  3. Using StringBuilder, various string operations can be performed in most effective manner.
Example 
class Program 

static void Main(string[] args) 

//for example: 
string str = "hello"; //Creates a new object when we concatenate any other words along with str variable it does not actually modify the str variable, instead it creates a whole new string. 
str = str + " to all"; 
Console.WriteLine(str); 
StringBuilder s = new StringBuilder("Hi"); 
s.Append(" To All"); 
Console.WriteLine(s); 
Console.Read(); 

}

Difference between Primary Key & Foreign Key

Difference between Primary Key & Foreign Key
Primary Key
Foreign Key
Primary key uniquely identify a record in the table.
Foreign key is a field in the table that is primary key in another table.
Primary Key can't accept null values.
Foreign key can accept multiple null value.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
Foreign key do not automatically create an index, clustered on non-clustered. You can manually create an index on foreign key.
We can have only one Primary key in a table.
We can have more than one foreign key in a table.

Difference between Primary Key & Unique Key

                                                   Difference between Primary Key & Unique Key
Primary Key
Unique Key
Primary Key can't accept null values.
Unique key can accept only one null value.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
By default, Unique key is non-clustered index.
We can have only one Primary key in a table.
We can have more than one unique key in a table.
Primary key can be made foreign key into another table.
Unique key can't be made foreign key into another table.

Anonymous method in C#

The concept of anonymous method was introduced in C# 2.0. An anonymous method is inline unnamed method in the code. It is created using the delegate keyword and doesn’t required name and return type. Hence we can say, an anonymous method has only body without name, optional parameters and return type. An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.

Features of anonymous method

  1. A variable, declared outside the anonymous method can be accessed inside the anonymous method.
  2. A variable, declared inside the anonymous method can’t be accessed outside the anonymous method.
  3. We use anonymous method in event handling.
  4. An anonymous method, declared without parenthesis can be assigned to a delegate with any signature.
  5. Unsafe code can’t be accessed within an anonymous method.
  6. An anonymous method can’t access the ref or out parameters of an outer scope.

Difference between Stored Procedure and Function

Basic Difference

  1. Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).
  2. Functions can have only input parameters for it whereas Procedures can have input/output parameters .
  3. Function takes one input parameter it is mandatory but Stored Procedure may take o to n input parameters..
  4. Functions can be called from Procedure whereas Procedures cannot be called from Function.

Advance Difference

  1. Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.
  2. Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.
  3. Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.
  4. Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.
  5. Inline Function can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

Assembly in .Net

An assembly is the basic building block in .NET. It is the compiled format of a class, that contains Metadata, Manisfest & Intermediate Language code. 
An assembly may be either Public or Private. A public assembly means the same as Shared Assembly. 
Private Assembly :
This type of assembly is used by a single application. It is stored in the application's directory or the applications sub-directory. There is no version constraint in a private assembly. 
Shared Assembly or Public Assembly : 
A shared assembly has version constraint. It is stored in the Global Assembly Cache (GAC). GAC is a repository of shared assemblies maintained by the .NET runtime. It is located at C:\Windows\Assembly OR C:\Winnt\Assembly. The shared assemblies may be used by many applications. To make an assembly a shared assembly, it has to be strongly named. In order to share an assembly with many applications, it must have a strong name. 

A Strong Name assembly is an assembly that has its own identity, through its version and uniqueness. 

What is .Net Framework ?

The .NET Framework is an environment for building, deploying, and running Web Services and other applications. It consists of three main parts: the Common Language Runtime, the Framework classes, and ASP.NET.
.Net Frame work is platform where developer develops web application,windows service,web services ,WCF services,Remote servics and also deploy the applications.
It has two components
1.CLR :Common language Runtime
2. Class Liberary.
Common Language Runtime :
The Common Language Runtime (CLR) Environment provides a rich set of features for cross-language development and deployment. CLR supports both object-oriented languages and procedural languages. CLR manages the execution of code and provides various services such as security, garbage collection, cross-language exception handling, cross-language inheritance, support for the Base Class Library (BCL), and so on. These are the main constituents of the CLR:
  • The Common Type System (CTS) supports object-oriented programming languages as well as procedural languages. Basically CTS provides a rich type system that's intended to support a wide range of languages.
  • The Common Language Specification (CLS) is a subset of the Common Type System, to which all language compilers targeting CLR must adhere.
  • All compilers under .NET will generate a uniform, common language called Intermediate Language (IL), no matter what language is used to develop the application. In fact, CLR will not be aware of the language used to develop an application. For this reason, IL can be considered the language of CLR—a platform for cross-language development.
  • The Just in Time (JIT) compiler converts the Intermediate Language code back to a platform/device-specific code. In .NET you have three types of JIT compilers:
  • Pre-JIT (compiles entire code into native code at one stretch)
  • Ecno-JIT (compiles code part by part, freeing when required)
  • Normal-JIT (compiles only that part of the code when called, and places it in the cache)
  • Type safety is ensured in this phase. In all, the role of a JIT compiler is to bring higher performance by placing the once-compiled code in cache, so that when the next call is made to the same method/procedure, it's executed at a faster speed.
  • The Virtual Execution System (VES) implements the Common Type System. VES loads links and runs Portable Executable (PE) files. VES also ensures loading of the information contained in metadata.
  • Metadata describes and references the datatypes defined by the VOS type system, lays out instances of classes in memory, resolves method invocation, and solves versioning problems (DLL hell).
Figure 1 depicts the .NET architecture.
Figure 1 .NET architecture.
The Windows operating systems run on the Intel family of microprocessor chips. To generate platform-neutral code, two things should happen:
  • Elimination of hardware dependencies such as microprocessor instruction sets, etc.
  • Elimination of software dependencies such as operating-system-naïve API, etc.
Once the code is compiled using any of the .NET-compliant language compilers, it gets converted to IL (Intermediate Language), as shown in Figure 1. This code is not compiled to machine-native code, but to an intermediate form that doesn't contain any specific information about hardware or software dependencies. When you run this code, since it's not in machine-specific form, it fails to execute. A runtime environment is required that understands and converts the IL code to machine-specific code. This is the role played by CLR (along with the many other functions discussed earlier). I won't talk about these issues in detail here, but will instead concentrate on implementation of cross-language capabilities of the .NET Framework.

Common Language Features

  • All languages use the same library, the Base Class Library (BCL). However, the syntax used by these languages remains the same as that of the original language.
  • No language under .NET has its own library.
  • Garbage collection is the responsibility of the CLR environment and not a language.

C# - Exception Handling

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: trycatchfinally and throw.
Try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or  more catch blocks.
Catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
Finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.
Throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

Difference between Delete and Truncate

DELETE 
1. DELETE is a DML Command. 
2. DELETE statement is executed using a row lock, each row in the table is locked for deletion. 
3. We can specify filters in where clause 
4. It deletes specified data if where condition exists. 
5. Delete activates a trigger because the operation are logged individually. 
6. Slower than truncate because, it keeps logs. 
7. Rollback is possible. 

TRUNC ATE 

1. TRUNCATE is a DDL command. 
2. TRUNCATE TABLE always locks the table and page but not each row. 
3. Cannot use Where Condition. 
4. It Removes all the data. 
5. TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. 
6. Faster in performance wise, because it doesn't keep any logs. 
7. Rollback is not possible. 

29 January 2013

Global.asax in Asp.Net

It is used to handle the application level events.  It is an option file and if there are any events written then it will be fired. If you want to share any information across the users then application level logic has to be written in this file. Because session will be per use and the global data which has to be shared across the server needs to written in Application_Start event. It will be fired at the first request to the server.

If you want to write any logic while application shutdown then Application_End event should be fired. 

Some times we may need to write the logic when new session starts. When user logged in then new session will be started and it will fire the Session_start and when session ends Session_End event will be fired. This will be shared only for that session.

If there is any error occur in the application then Application_Error event helps you to capture the error.
The Global.asax file consists of following events that can be handled using it:

1. Application_Start

2. Session_Start

3. Application_BeginRequest

4. Application_AuthenticateRequest

5. Application_Error

6. Session_End

7. Application_End

2 January 2013

SQL Server 2008 R2 Local Database Instance


Setting up a SQL Server 2008 R2 Local Database Instance

Setting up and accessing a local database instance in SQL Server 2008 is a somewhat less then intuitive process.  This post will attempt to alleviate some of the wasted time and frustration of this process.  Please let me know if you experience any issues and also if you see anything that needs to be corrected.  Thanks and I hope this helps!

Start up the Local Server

  • Click Start -> Microsoft SQL Server 2008 R2 -> SQL Server Configuration Manager
  • Set the SQL Server(SQLEXPRESS) and SQL Server Browser to automatic start mode.
  • Right click -> Properties -> Service Tab
SQL Server 2008 Server Settings
SQL Server 2008 Server Settings

Login to Local Server

  • Now open up SQL Server Management Studio and Connect to Object Explorer and select Server Name: [Your PC name]\SQLEXPRESS
  • Example:  8540P-KL\SQLEXPRESS
  • To find your PC name:  Right click My Computer -> Properties -> Computer Name tab
  • Login using windows authentication:  Using the user name [Your Domain]/[Your User Name]
SQL Server 2008 Login Settings
SQL Server 2008 Login Settings



Setup User Account

  • In SQL Mgmt Studio -> Expand your local Server -> Security -> Right click on Logins -> New Login
  • Uncheck Enforce password policy, password expiration and user must change pw(Since this is local)
  • Default database -> Your Database
  • User Mapping Page -> Map to your db and grant db_owner role
  • Status Page -> Grant Permission to connect and Enable Login
SQL Server 2008 User Settings Local DB
SQL Server 2008 User Settings Local DB




Setup Access Permissions/Settings for User

  • Right click your Local Server -> Properties -> Security Tab -> Enable SQL Server and Windows Authentication Mode
  • Open SQL Server Configuration Manager -> SQL Server Network Configuration -> Protocols for SQLEXPRESS -> Enable TCP/IP
SQL Server 2008 Server Permissions