Thursday, December 27, 2012

Try Operating System other than Windows

Try Operating System other than Windows

My window 7 OS got crash and finding difficulity to boot my laptop, I did not find any window OS for free than I found there are so many Unix based OS available for free. I tried Fidora, it's really good It's solve all my purpose, I am able to boot my laptop and it is working. I can browser internet, search my files , backup etc. almost all the thing I can do that is require at home.  

There are so many free OS available, but I like Fidora, latest version is 18. Download ISO file and use USB creator softwware to craete bootable CD or USB and then start installation

Install Fidora 17:
http://fedoraproject.org/en/get-fedora

Download Lili USB Creator from ISO file
http://www.linuxliveusb.com/en/download

Fidora documentation available:
http://docs.fedoraproject.org/en-US/Fedora/18/html/Release_Notes/index.html

See the list of other OS available:
http://www.ixibo.com/list-of-free-operating-systems-download-now/

Wednesday, December 26, 2012

How to use MERGE, WHEN MATCH and WHEN NOT MATCH statement in SQL Server 2008


How to use MERGE, WHEN MATCH and WHEN NOT MATCH statement in SQL Server 2008
Normally we have common requirement for inserting data into the database table, we need to take care of duplicate data should not get inserted, for this we used to first check in the database table whether that data is present or not, If not then insert otherwise update.
Current Approcah

Declare
      @Id         Int = 10,
      @Name       Varchar(20) = 'Kesharwani',
      @Salary     Int = '30000'

-- Update the row if it exists.   
    UPDATE [Temp]
      SET Name = @Name, Salary = @Salary
      WHERE empId = @empId
-- Insert the row if the UPDATE statement failed.    
      IF (@@ROWCOUNT = 0 )
      BEGIN
          INSERT INTO [Temp] (Id, Name,Salary)
          VALUES (@empId, @Name,@Salary)
      END


New way to do the same operation
Let say you have one database table as below
Select * from Temp









Query -1
Declare
      @Id         Int = 10,
      @Name       Varchar(20) = 'Kesharwani',
      @Salary     Int = 30000
          
MERGE INTO [Temp] as TARGET
      USING (SELECT @Id AS empId,@Name AS Name,@Salary AS Salary) AS
SOURCE ON TARGET.empId = SOURCE.empId
      WHEN MATCHED THEN
            UPDATE SET Name=SOURCE.Name, Salary =SOURCE.Salary
      WHEN NOT MATCHED THEN
            INSERT (empId,Name,Salary)
VALUES (SOURCE.empId,SOURCE.Name,SOURCE.Salary);

Note: If you have multiple PK columns then in the USING statement you need to include all PK columns as same as below

USING (SELECT @Id,@Name,@Salary ) AS
SOURCE ON TARGET.empId = SOURCE.empId
      AND TARGET.empId1 = SOURCE.empId1
      AND TARGET.empId2 = SOURCE.empId2    

Result from Query-1
Employee Id = 10 data was present that’s why that row is UPDATED

Select * from Temp







Query -2
Declare
      @Id         Int = 30,
      @Name       Varchar(20) = 'Master',
      @Salary     Int = 40000              

MERGE INTO [Temp] as TARGET
      USING (SELECT @Id AS empId,@Name AS Name,@Salary AS Salary) AS
SOURCE ON TARGET.empId = SOURCE.empId
      WHEN MATCHED THEN
            UPDATE SET Name=SOURCE.Name, Salary =SOURCE.Salary
      WHEN NOT MATCHED THEN
            INSERT (empId,Name,Salary)
VALUES (SOURCE.empId,SOURCE.Name,SOURCE.Salary);


Result from Query-2
Employee Id= 30 was NOT present that’s why new data got INSERTED
Select * from Temp













Multiple values as an input
Let’s say you are passing table type as input parameter because you want to pass multiple data together, assume you are passing TempTest table (having same structure as Temp table) as an input .

Select * from TempTest






Query -3

MERGE INTO [Temp] as TARGET
      USING (SELECT empId,Name,Salary from TempTest) AS
SOURCE ON TARGET.empId = SOURCE.empId
      WHEN MATCHED THEN
            UPDATE SET Name=SOURCE.Name, Salary =SOURCE.Salary
      WHEN NOT MATCHED THEN
            INSERT (empId,Name,Salary)
VALUES (SOURCE.empId,SOURCE.Name,SOURCE.Salary);


Note: If you have multiple PK columns then in the USING statement you need to include all PK columns as same as below

USING (SELECT @Id,@Name,@Salary ) AS
SOURCE ON TARGET.empId = SOURCE.empId
      AND TARGET.empId1 = SOURCE.empId1
      AND TARGET.empId2 = SOURCE.empId2

Result from Query-3

From TempTest table employee Id =10 was present that’s why this data got UPDATED and employee Id =30 was NOT present then this data got INSERTED into the Temp table

Select * from Temp









You can perform all kinds of operation (INSERT, UPDATE, DELETE) using these statement , for more example see the link here http://technet.microsoft.com/en-us/library/bb510625.aspx

Sunday, December 16, 2012

Good reasons to work on Visual Studio 2012

Visual Studio 2012 is next generation platform (4.5 Framework) to develop .NET applications, this includes development of Window 8, Window phone, Gaming and 3D applications.
To know more about visual studio 2012, see below link

Features perspective
http://blogs.msdn.com/b/jasonz/archive/2012/08/15/visual-studio-2012-and-net-framework-4-5-released-to-the-web.aspx

Developer perspective
http://dotnet.dzone.com/articles/10-illustrated-examples-visual-0

Thursday, December 13, 2012

How to enable code analysis for Visual Studio 2010 projects (FxCop, StyleCop etc.)

Below I am giving steps to enable FxCop rule set. To enable FxCop Rule set into your projects, first you need the FxCop rule sets files (*.ruleset). By default you will get most of sets from Microsoft when you install Visual Studio 2010, you can customize those by selecting or unselecting rule sets. Or if you want to create your own custom rule set then below link will guide you for this 
I am assuming that rule set are available with you and you want to enable FxCop/StyleCop/etc. for your project.
  1. Right click on the project property and go to the “Code Analysis” tab
  2. Select check box “Enable Code Analysis on build”
  3. Select “ from “Run this rule set” dropdown and select your custom rule set you created or available with you.
  4. Click “Open” to customize
  5. Select/Unselect Rules using check box
  6. Set the Action likes if rules violate by code then it should be warning, error or none
  7. “Save” the rule set
  8. “Save” the Project
 


FxCop rule set is now enable for this project, when you build your project each time all rules run and give you errors/warning  if code violate the rules.

Sunday, December 09, 2012

How to use XQuery to retrieve data from XML


How to use XQuery to retrieve data from XML

XQuery is an Language that used to retrieve data from XML in required format, XQuery uses FLWOR expressions (For, Let, Where, Order By, Return), I am giving to some examples below to understand XQuery. By looking below example you should be able to retrieve data from XML using XQuery. 

Create One XML file and save as “Employee.xml”

xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee type=”Admin”>
    <Name>Ritesh</Name>
    <DOB>Jun-23-2000</DOB>
    <DeptId>10</DeptId>
    <Salary>2000</Salary>
  </Employee>
  <Employee type=”Supervisor”>
    <Name>Kumar</Name>
    <DOB>Jan-20-2000</DOB>
    <DeptId>20</DeptId>
    <Salary>4000</Salary>
  </Employee>
</Employees>

Example-1: To Find the Department Id of the employee name 'Ritesh'

for $emp in doc (Employee.xml)/Employees/Employee
where $emp/Name =”Ritesh”
return $emp/DeptId 

Return Answer
<DeptId>10</DeptId>

If you want to return only data then use below return statement
return data($emp/DeptId) 

Return Answer
10

If you want to return Data in different Structure then use below return statement
return
  <Id>{data($emp/DeptId)}</Id>

Return Answer
  <Id>10</Id>

Example-2: To Find the Employee whose salary is > 1000 and return name with type

for $emp in doc("Employee.xml")/Employees/Employee
where $emp/Salary > 1000
order by $emp/Name
return
<Department>
if ($emp/@type=”Admin”)
then
<Admin>data($emp/Name)</Admin>
else
<Supervisor>data($emp/Name)</Supervisor>
</Department>

Return Answer
  <Department>
  <Admin>Kumar</Admin>
  <Supervisor>Ritesh</Supervisor>
  </Department>

You can find more examples http://www.w3schools.com/xquery/default.asp

Saturday, December 01, 2012


Error: Resource cannot be found in MVC 3.0  application

I observed this error when I was working in one of MVC (Razor View) application, I got confused when all my page is available in correct View folder then why this error thrown. Finally I found there was no mistake from my implementation or page name etc.

Error:

Cause:
The problem was if you are running MVC application by Visual Studio and any of  view ( .cshtml or .aspx) file open (in Active tab) in Visual Studio and you are the running the application by clicking (F5 or Ctrl+F5) than you will get this error. Visual Studio not goes to start up page it is considering open page (in Active tab) as start up page.



Solution:
Close all view files (.cshtml or .aspx) and then run application by F5 or Ctrl+F5 or open .cs files in Active tab in Visual Studio. It ok to open .cs files.