Sunday, 6 October 2013

Collections in C#

Collections are group of records which can be treated as single logical unit. Collection classes are specialized classes for data storage and retrieval.Collection classes are derived from System.Collection namespace
Dot net  collections are divided into four categories:-
1. Indexed based
Indexed based collections helps to access the value of the row using internally generated index by the collections. They are two types:-
Array
List
2. Key value pair
Key value pair based collections helps to access the value of the row using user defined key. They are two types:-
Hashtable: - Uses a key to access elements in the collections
Sorted list: - Uses a key as well as an index to access elements in a list. It is a combination of an array and a hashtable.If we access items using an index, it is an arraylist and if we access the items using a key, it is a hashtable. The collection of items is always sorted by the key valu e.
3. Prioritized collections
Stacks
Queue
Queues and stacks helps to access data in a particular sequence.
Queues uses FIFO(First in First out) methodology.
Stacks uses LIFO(Last in First out) methodology.
4. Specialized collection
String collections

Hybrid dictionary

Thursday, 3 October 2013

Lamda Expression in C#

The concept of lamda expression was introduced in C# 3.0. It is just a new way to write anonymous methods. At compile time all the lamda expressions are converted into anonymous methods according to lamda expression conversion rules. The left side of the lamda operator "=>"(is called ‘goes to’ operator) represents the arguments to the method and the right side is the method body.

Lambda expression Syntax:
(parameters) => expression-or-statement-block

Types of Lamda expression

1. Statement Lamda
Statement lambda has a statement block on the right side of the lambda operator "=>".
Eg: a => {return a * a;};

       
2. Expression Lamda
Expression lambda has only an expression .Will not have a return statement or curly braces, on the right side of the lambda
operator "=>".
Eg: a => a * a; // here a*a is expression


Lets take an example :

Lets convert it to lambda expression

X=>x+2 is the lambda expression. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.

Hope you are enjoyed this.


Tuesday, 10 September 2013

Ranking Functions in SQL


Ranking functions are used  for statistical ordering. In this Article, you learn how to use the four ranking functions available in the SQL Server 2008.
The four ranking functions are:-

1.    ROW_NUMBER
2.    RANK
3.    DENSE_RANK
4.    NTILE


1.    ROW_NUMBER

ROW_NUMBER assigns a sequential number from 1 to n to the rows based on user specified sorting order.

Syntax:

ROW_NUMBER() OVER (ORDER BY <column Name>)

Example :
Consider a table tblStudent with columns ‘Name’ and ‘Marks’.



Using ROW_NUMBER() function

SELECT Name,
                Marks,
                ROW_NUMBER()OVER (ORDER BY Marks) AS RowNo
FROM tblStudent

RESULT:



2.    RANK
This function does much the same thing as ROW_NUMBER.But if there are same values in the record, it is said to be a tie and these records get the same rank.

Syntax:

RANK() OVER (ORDER BY <columnName>)


Example :
Consider the same table tblStudent.

Using Rank function
SELECT Name,
                Marks,    
                RANK() OVER (ORDER BY Marks) AS RankNo
FROM tblStudent
RESULT :


3.    DENSE_RANK

DENSE_RANK assigns same value to the each duplicate records, but does not produce gaps in the sequence.

Syntax:
         DENSE_RANK() OVER (ORDER BY <columnName>)

Example :
Consider the same table tblStudent.
Using Dense_Rank function
   SELECT Name,
                Marks,    
                DENSE_RANK() OVER (ORDER BY Marks) AS DenseRankNo
FROM tblStudent

RESULT:


4.    NTILE

NTILE is used to split the result into approximately equal groups.If you want a result to split into 4 equal groups,you can use NTILE(4).

Syntax :

NTILE(integerExpression) OVER (ORDER BY <columnName>)

Example :
Consider the same table tblStudent.
Using NTILE Function


Tuesday, 1 November 2011

LINQ


LINQ is about integrating query operations into the .NET platform in a comprehensive and open manner. It’s also about providing a unified way for you to query across any kind of data that you have in your program, whether it’s relational, objects or XML.
LINQ TO SQL
LINQ to SQL allows .NET developers to write “queries” in their .NET language of choice to retrieve and manipulate data from a SQL Server database.
Here I’m trying to explain LINQ TO SQL by a simple program.Create table as shown  in screenshot.



Add some data to the table as follows.

Create a new windows application and add DataGridView to the Form.
Then add LINQ TO SQL Classes to the project.For this click Project menu and click Add New Item.Then select Data in Categories and LINQ TO SQL Classes in Templates and click Add button.


Then drag the tables from the Server Explorer to the DataClasses1.dbml.



Now we are moving to the code part. Go to the Form Load event of the Form by double clicking the Form. Then add following code.


Run the code.


Monday, 10 October 2011

Crystal Report using C#.NET


This article is about creating Crystal Reports using C#.NET.

Crystal Report is a Reporting Application that can generate reports from various Data Sources like Databases , XML files etc.. The Visual Studio.NET Integrated Development Environment comes with Crystal Reports tools. The Crystal Reports makes it easy to create simple reports.
Here I’m trying to create reports from table Employee.
Create a windows application. Add a button control and crystalReportViewer control to the form. Then our Form will look like this.

 Then add crystal report to the project. For this click Project menu and click add New  Item. Then select Reporting in categories and select CrystalReport in Templates and click add button.



 Then do the following steps.




Add following code to the click event of button.

Run the code by press F5 and click Show button in the form.Our project will look like this.


Thursday, 29 September 2011

Picture Viewer using C#

Here I am trying to create a picture viewer application using C# in Visual studio 2008.Through this application we can view and  can convert images from one type to another.

Create a windows application and name it as PictureViewer.Then add openFileDialog, saveFileDialog,panel, pictureBox,menuStrip and three buttons to the form and name it as I did.Add three menu items and name as Open,Save As and Exit.Then our UI will look like this.


Open
When we click Open in File menu, an openFileDialog will come and we can select the image we want to  open.For this add following code to the click event of Open.


OpenImage() function is shown below.

















Save As
Add the following code to the click event of Save As

Function SaveImage() is shown below.


Function for SaveAsType() is shown below.

Rotate clockwise and anticlockwise
The following is the function for rotating an image.
Add the following code to click event of  Rotate clockwise button.



Add the following code to click event of  Rotate Anticlockwise button.

   
Next
To view the next image in the folder,add the following code to the click event of Next button.
Our project is finished and run the code by pressing F5 and open an image.then our application wil looks like this.




Thursday, 1 September 2011

Validating a textbox control


In this application I’m validating a textbox by allowing only number to enter.
Create a windows application with a textbox control as shown in fig below.







Here what we do is add some code in keyPress event of the textbox control, which  will show a message when we enter a wrong data.Add the following code in the keyPress event of the textbox control









ValidateNumber(e.KeyChar) function is shown below


















Run the project and enter an alphabet,it will show a message box as shown below