May 25, 2010

Few ADO.NET points for reference

FieldCount : is used to get number of colums in a row by using sqldatareader

IsClosed is used to find whether the sqldatareader is closed or not.

Good Example of stored procedure

//Create the Connection Object
SqlConnection ConnectionObject = new SqlConnection(ConnectionString);
//Create the Command Object
SqlCommand CommandObject = new SqlCommand("StoredProcedureName", ConnectionObject);
//Specify to CommandObject that you intend to execute a Stored Procedure
CommandObject.CommandType = CommandType.StoredProcedure;
//Create an SQL Parameter object
SqlParameter ParameterObject = new SqlParameter();
//Specify the name of the SQL Parameter
ParameterObject.ParameterName = "Parameter1";
//Assign the Parameter value
ParameterObject.Value = "Some Value";
//Specify the Database DataType of the Parameter
ParameterObject.DbType = DbType.String;
//Specify the type of parameter - input-only(default), output-only, bidirectional
ParameterObject.Direction = ParameterDirection.Input;
//Associate the Parameter to the Command Object
CommandObject.Parameters.Add(ParameterObject);
//Open the connection
ConnectionObject.Open();
//Execute the command
int Records_Affected = CommandObject.ExecuteNonQuery();
//Close the Connection
ConnectionObject.Close();


Executing and getting data from two queries using datareader

- By using NextResult() method
- Example

//Create the SQL Query with 2 Select statements
string SQLQuery = "Select * from Customers;Select * from Employees;";
//Create the Connection Object
SqlConnection ConnectionObject = new SqlConnection(ConnectionString);
//Create the Command Object
SqlCommand CommandObject = new SqlCommand(SQLQuery, ConnectionObject);
//Open the connection
ConnectionObject.Open();
//Execute the command. Now reader object will have 2 tables of data.
SqlDataReader ReaderObject = CommandObject.ExecuteReader();
//Loop thru the tables in the DataReader object
while (ReaderObject.NextResult())
{
while (ReaderObject.Read())
{
//Do Something
}
}
//Close the Reader
ReaderObject.Close();
//Close the Connection
ConnectionObject.Close();


Uses of using Stored procedures

1) Better Performance - as they are precompiled one's
2) Security - we can specify who has the rights to execute
3) Reduce Network traffic - Just we need to specify the name of SP instead of sending a big sql query

Differences between Datareader and Dataset

DataReader
1. DatReader works on a Connection oriented architecture.
2. DataReader is read only, forward only. It reads one record at atime. After DataReader finishes reading the current record, it moves to the next record. There is no way you can go back to the previous record. So using a DataReader you read in forward direction only.
3. Updations are not possible with DataReader.
4. As DataReader is read only, forward only it is much faster than a DataSet.
DataSet
1. DataSet works on disconnected architecture.
2. Using a DataSet you can move in both directions. DataSet is bi directional.
3. Database can be updated from a DataSet.
4. DataSet is slower than DataReader.

May 24, 2010

Access modifiers in C#

Access Modifiers
=================

Public
can be accessed by any other code in the same assembly or another assembly that references it.

Private
can only be accessed by code in the same class or struct.

Protected
can only be accessed by code in the same class or struct, or in a derived class.

Internal
can be accessed by any code in the same assembly, but not from another assembly.

Protected Internal
can be accessed by any code in the same assembly, or by any derived class in another assembly.

Some points

1) structs members cannot be declared as protected because they doesn't support inheritance.
2) destructors cannot have access modifiers.
3) internal is the default access modifier for class, struct, interface.
4) Interface members are always public.
5) Enumeration members are always public.

Indexers in C#

- used to treat an object as an array.
- syntax

this [argument list]
{

get
{

// your get block code

}

set
{

// your set block code

}

}

- Example

using System;



namespace Indexer_example1

{

class Program

{

class IndexerClass

{

private string[] names = new string[10];



public string this[int i]

{

get

{

return names[i];

}

set

{

names[i] = value;

}

}

}



static void Main(string[] args)

{

IndexerClass Team = new IndexerClass();

Team[0] = "Rocky";

Team[1] = "Teena";

Team[2] = "Ana";

Team[3] = "Victoria";

Team[4] = "Yani";

Team[5] = "Mary";

Team[6] = "Gomes";

Team[7] = "Arnold";

Team[8] = "Mike";

Team[9] = "Peter";



for (int i = 0; i < 10; i++)

{

Console.WriteLine(Team[i]);

}

Console.ReadKey();

}

}

}

FAQ's on Constructors

1. Is the Constructor mandatory for the class ?

Yes, It is mandatory to have the constructor in the class and that too should be accessible for the object i.e., it should have a proper access modifier. Say for example we have the private constructor in the class then it is of no use as it cannot be accessed by the object, so practically it is no available for the object. In such conditions it will raise an error.

2. What if I do not write the constructor ?

In such case the compiler will try to supply the no parameter constructor for your class behind the scene. Compiler will attempt this only if you do not write the constructor for the class. If you provide any constructor ( with or without parameters), then compiler will not make any such attempt.

3. What if I have the constructor public myDerivedClass() but not the public myBaseClass() ?

It will raise an error. If either the no parameter constructor is absent or it is in-accessible ( say it is private ), it will raise an error. You will have to take the precaution here.

4. Can we access static members from the non-static ( normal ) constructors ?

Yes, We can. There is no such restriction on non-static constructors. But there is one on static constructors that it can access only static members.

May 12, 2010

Windows Workflow Foundation(WWF) basics

Windows Workflow Foundation(WWF) basics

 

I found good article on WWF here. Very useful….Happy Learning…

 

http://www.dotnetfunda.com/articles/article155.aspx

May 7, 2010

Dotnet Framework Basics

 

Microsoft .NET Framework

========================

 

.NET Framework = Base class Library(BCL) + CLR

 

CLR - provides Memory management, Thread management, Exception handling, Garbage collection, Security.

BCL - user interface, data access, database connectivity, cryptography, web application development, numeric algorithms, and       network communications.

 

Source Code(C#) -------------> MSIL (or) CIL ----------------------------> Native Code

                C# compiler                       CLR using JIT compiler

 

Types of JIT compiler

---------------------

Pre-JIT

Econo-JIT

Normal-JIT

 

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(default) :- 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.

 

CTS & CLS

----------

- CLS is a subset of CTS

- Common Type System (CTS) describes how types are declared, used and managed in the runtime and facilitates cross-language integration, type safety, and high performance code execution.

- Common Language Specification (CLS) is nothing but guidelines that a language to follow so that it can communicate with other .NET languages in a seamless manner

 

Assemblies

----------

- an assembly is a partially compiled code library for use in deployment, versioning and security

- an assembly is an EXE or DLL

- Private and Shared assemblies

- Private assemblies are stored along with the applicaton folder

- Share assemblies are stored in GAC.

- ILDASM is used to view assembly. It shows manifest and metadata.

- Manifest contains version, scope, security, references to resources and classes.

- Metadata is data about data and it contains information about classes, structs, interfaces, namespaces, etc.

- To create strong name sn -k "c:\temp.snk" to uniquely identify the assembly in GAC.

- Delay Signing : To avoid showing the strong names to developer during development

                [assembly:AssemblyKeyFileAttribute("myKey.snk")]

                [assembly:AssemblyDelaySignAttribute(true)]

- Ex:

                using System;

                namespace SURComponents

                {

                                public class Sample

                                {

                                                public string GetData()

                                                {

                                return "hello world";

                                                }

                                }

                }

In the above example SURComponents assembly is created. We can import this assembly and create an object for the class Sample to use the method GetData().

 

Garbage Collector

-----------------

- Three generations Generation0, Generation1, Generation2.

- Gen0 contains all the newly created objects and it is in the top of heap. Gen1 contains all the living objects from Gen0   and in the middle of heap. Gen2 contains all the living objects from Gen1 is in the bottom of heap.

- GC is able to collect the garbage in two ways: full collections (searching the entire managed heap for dead objects) and   partial collections (searching only a single generation zone).

 

 

Access to COM components is provided in the System.Runtime.InteropServices and System.EnterpriseServices namespaces of the framework