Thursday, 6 February 2014

Basic Crystal Report

I will explain how to create basic crystal reports using asp.net. Crystal Report is standard reporting tool for visual studio by using these we can display reports regarding employee details and display charts etc and crystal reports need minimal coding to display result. 

To implement crystal reports first design the table in database and give name UserInfomation

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
FirstName
Varchar(50)
LastName
varchar(50)
Location
varchar(50)
After completion of table creation enter some dummy data because we need to use that data to populate reports.

Now Open visual studio and create new website after that right click on your website and select Add new item in that select Crystal Report and click Add
After that add crystal report then it will prompt Crystal Report Gallery window in that select blank solution and click OK  
 
A blank report will create in our application now click on CrystalReports menu under that select Database under that select Database Expert
 
After click on Database Expert now Database Expert wizard will open in that select Create New Section >> select OLE DB (ADO) >> in that click on + sign of OLE DB (ADO) 
    
Now select Microsoft OLE DB Provider for SQL Server and click Next (Here we can select SQL Native client option also but sometimes during deployment if servers not contains this native client it will throw error).
 
 
Now enter SQL Server name, username, password and required database and click Next
After enter credentials for your required database click Next then click Finish (Here for my database I didn’t set any credentials for that reason I didn’t enter userid and password details don’t get confused).

After click Finish now our database loaded in OLEDB (ADO) section >> select your database >> select dbo >> select required tables
 Now open tables in that select required table and move to selected tables section and click OK
 


  


After that Database Fields in Field Explorer populated with our required data table now drag and drop the required fields from data table to reports Details section

 



Now open your Default.aspx page drag and drop CrystalReportViewer control from Reporting tab.


 
Whenever we click on New report source one window will open in that select crystal report for Report Source from the available reports in dropdownlist and click OK.

  

After assign available report to CrystalReportViewer control check your code that would be like this 

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now run your application your report will be like this 
  

Enumeration

Creating an Enumeration

To create an enumeration, you use the enum keyword, followed by the name of the enumeration, followed by a name for each item of the list. The name of the enumerator and the name of each item of the list follows the rules of names in the C# language. The formula of creating an enumeration is:
enum Enumeration_Name {Item1, Item2, Item_n};
To create an enumeration, you can manually type the code. To use a code snippet, right-click where you want to add the enumeration and click Insert Snippet... Double-click Visual C#. In the list, double-click enum:

 Enum

Here is an example of creating an enumeration:
 
using System;

public class Exercise
{
    enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        return 0;
    }
}

Declaring an Enumeration Variable

After creating an enumeration, each member of the enumeration holds a value of a natural number, such as 0, 4, 12, 25, etc. In C#, an enumeration cannot hold character values (of type char).
After creating an enumeration, you can declare a variable from it. Here is an example:

using System;

public class Exercise
{
    enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        HouseType propType;

        return 0;
    }
}
 
Just as done with the other types, you can use the var keyword to declare a variable of an enumeration type. 

Initializing an Enumeration Variable

After declaring a variable for an enumeration, to initialize it, specify which member of the enumeration would be assigned to the variable. You should only assign a known member of the enumeration. To do this, on the right side of the assignment operator, type the name of the enumeration, followed by the period operator, and followed by the member whose value you want to assign. Here is an example:

using System;

public class Exercise
{
    enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        var propType = HouseType.SingleFamily;

        return 0;
    }
}

You can also find out what value the declared variable is currently holding. For example, you can display it on the console using Write() or WriteLine(). Here is an example:
 
using System;

public class Exercise
{
    enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        var propType = HouseType.SingleFamily;

        Console.WriteLine("House Type:  {0}", propType);

        return 0;
    }
}
 
This would produce:
House Type:  SingleFamily
Press any key to continue . . .
An enumeration is in fact a list of numbers where each member of the list is identified with a name. By default, the first item of the list has a value of 0, the second has a value of 1, and so on. For example, on the HouseType enumeration, Unknown has a value of 0 while Townhouse has a value of 2. These are the default values. If you don't want these values, you can specify the value of one or each member of the list. Suppose you want the Unknown member in the above enumeration to have a value of 5. To do this, use the assignment operator "=" to give the desired value. The enumerator would be:
 
using System;

public class Exercise
{
    enum HouseType { Unknown = 5, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        return 0;
    }
}

In this case, Unknown now would have a value of 5, SingleFamily would have a value of 6 because it follows a member whose value is 1 (thus 5 + 1 = 6). Townhouse would have a value of 7, and Condominium would have a value of 8. You can also assign a value to more than one member of an enumeration. Here is an example:

using System;

public class Exercise
{
    enum HouseType { Unknown = 3, SingleFamily = 12, TownHouse, Condominium = 8 }
    
    static int Main()
    {
        return 0;
    }
}

In this case, Townhouse would have a value of 13 because it follows SingleFamily that has a value of 12.

Lambda Expression in c#

 

  1. What is a Lambda Expression?
  2. A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name.
  3. Why do we need lambda expressions? (Why would we need to write a method without a name?)
  4. Convenience. It's a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class.
    Benefits:
    1. Reduced typing. No need to specify the name of the function, its return type, and its access modifier. 
    2. When reading the code you don't need to look elsewhere for the method's definition. 
    Lambda expressions should be short. A complex definition makes the calling code difficult to read.
  5. How do we define a lambda expression? 
  6. Lambda basic definition: Parameters => Executed code.

Simple example

n => n % 2 == 1 

 

  • n is the input parameter
  • n % 2 == 1 is the expression
You can read n => n % 2 == 1 like: "input parameter named n goes to anonymous function which returns true if the input is odd".
Same example (now execute the lambda):

List<int> numbers = new List<int>{11,37,52};
List<int> oddNumbers = numbers.where(n => n % 2 == 1).ToList();
//Now oddNumbers is equal to 11 and 37
 
That's all, now you know the basics of Lambda Expressions.