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.


Friday, November 30, 2012

Hyperlink clicks doing page refresh in JQuery

Hyperlink clicks doing page refresh in JQuery

Normally if you have multiple Ajax calls and Hyperlink action on the same page then you will face multiple call issues, when you clicks on hyperlink same time another Ajax call action trigger and create the issues, you have to prevent those call using ‘stopPropogation’ and ‘preventDefault’, see below example

Following code will refresh page when you click on “Click Here”

$("CallFunctionToOpenWindow(event)’>" Click Here "");

Try following way to write same code, this will prevent server call which will not refresh the page


anchor.click(function (event) {
    CallFunctionToOpenWindow (event);
    event.stopPropagation();
    event.preventDefault();
});       

CallFunctionToOpenWindow(): you can write simple custom function to do the action on hyperlink click.

Image Background Position not working in Firefox, CSS Style Sheet

Image Background Position not working in Firefox, CSS Style Sheet

If you are facing issues to set Image background position with following style sheet class
display: inline-block;
border: none;
background-image: url(/images/MockIcon.png);
background-repeat: no-repeat;
background-position-x: 50px;
background-position-y: 5px;
vertical-align: middle;

 Try following style sheet class which will work in all the Browsers (Chrome, IE and Firefox)
display: inline-block;
border: none;
background-image: url(/images/MockIcon.png);
background-repeat: no-repeat;
background-position: left 50px top 5px;
vertical-align: middle;
 

Sunday, September 09, 2012

Do your unit testing with Rhino Mocks

Mock object can be used with .NET framework and utilize in all kinds of .net unit testing, Normally real object call is slow because of lots other dependency like libraries, db etc.
also if real object is not 100% ready. In all that scenario Rhino Mocks framework can be used.

For functional testing you have to have real object ready and for unit testing Rhino Mock is the best and quick  solution.

Using rhino mock you can simply create mock object and from the caller method you can call mock object instead of calling real object.

to know more about Rhino Mock you can refer following links

Do your unit testing with Rhino Mocks

Mock object can be used with .NET framework and utilize in all kinds of .net unit testing, Normally real object call is slow because of lots other dependency like libraries, db etc.
also if real object is not 100% ready. In all that scenario Rhino Mocks framework can be used.

For functional testing you have to have real object ready and for unit testing Rhino Mock is the best and quick  solution.

Using rhino mock you can simply create mock object and from the caller method you can call mock object instead of calling real object.

to know more about Rhino Mock you can refer following links



Do your unit testing with Rhino Mocks

Mock object can be used with .NET framework and utilize in all kinds of .net unit testing, Normally real object call is slow because of lots other dependency like libraries, db etc.
also if real object is not 100% ready. In all that scenario Rhino Mocks framework can be used.

For functional testing you have to have real object ready and for unit testing Rhino Mock is the best and quick  solution.

Using rhino mock you can simply create mock object and from the caller method you can call mock object instead of calling real object.

to know more about Rhino Mock you can refer following links



Saturday, September 01, 2012

How to write data into the Excel 2010 using C#.Net


How to write data into the Excel 2010 using C#.Net
You can use .Net library to export or write data into the excel 2010 and above, below the code to do the same using C#.NET
Add Reference
Goto .NET tab and addMicrosoft.Office.Interop.Excel” reference in your project
NameSpace
using Excel = Microsoft.Office.Interop.Excel;
C#.Net Source Code
var exApp = new Excel.Application();

// Make the Excel file visible.
exApp.Visible = true; 
//Add one sheet into
exApp.Workbooks.Add(); 
// use only one workSheet.
Excel._Worksheet workSheet = exApp.ActiveSheet;
// column headings in cells A1 and B1.
workSheet.Cells[1, "A"] = "ID";
workSheet.Cells[1, "B"] = "Description";
var row = 1;
for (int i = 0; i <= 10; i++)
{
   row++;
   workSheet.Cells[row, "A"] = "Id_" + i;
   workSheet.Cells[row, "B"] = "Description " + i;
} 
workSheet.Columns[1].AutoFit();
workSheet.Columns[2].AutoFit();
// Call to AutoFormat
workSheet.Range["A1", "B3"].AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
//Copy into the range
workSheet.Range["A1:B3"].Copy();

Wednesday, June 20, 2012

How To Set automated Build using Custom “.proj” file and Upgrad Template in TFS 2010

How To Set automated Build using Custom “.proj” file and Upgrad Template in TFS 2010

Below I am trying to give simple and full steps to create automated build for any .NET application using TFS 2010. So here we go
Prerequisite Steps
  • To do automated build, I believe you should have one separate Build machine and this build machine should have all admin permission to connect to your VSTS server. 
  • Install all prerequisite required for your application.
  •  Make sue build machine has Microsoft Team Foundation Server 2010 Installed
Once you have all the setup or installation done then you can proceed to create automated build for your application.

Below I am setting up automated build for one of the Silverlight Project.

Step -1
To start build configuration, very first step to setup Controller and Agent for the build
Open Team Foundation Administrator Console

Steps-2
Go to the “Build Configuration”

Steps-3
Click on the Build controller properties and Create the Build controller by giving Display name (ex: Project name - Controller) , click Test Connection and click OK
Steps-4
Create the Build Agent by giving Display name and select previous created controller (Steps-3), define Working Directory path, you can add new tag if any and say OK
Same way you can create multiple agent for single controller now you can close Build Configuration.
Steps-5
After you done with Controller and Agent setting, you can go head and create build definition for your project. To do that Open visual Studio and Team Explorer window to create builds definition
Right click and select “New Build Definition” on the Team Explorer window

Steps-6
Type the Build Definition name and description as per your project

Steps-7
Go to the new tab Trigger on the same window and select the build option as you want
Here you can set your daily build setup like every day midnight your application get build, you can specify day and time too

Steps-8
Go to the new tab Workspace on the same window and define the “Source Control Folder” (ex: $/VSSRootFolder) and “Build Agent Folder” (by default:  $(SourceDir))

Source Control Folder: select the folder where all your project or solutions files exists
Steps-9
Go to the new tab Build Defaults on the same window and select the controller as you’re created on Steps-3 and define the folder path where you want the output file from this build

Steps-10
Go to the new tab Process on the same window


Go to the Build Process template and select “Upgrade Template”
This option we have to select if we want to build all the projects in different folder structure, The Default template will create all projects output into one single folder

Now on the Build Process parameters
Give the TFSBuild.proj file path into “Required -> Configuration Folder Path
On the “Advancedà MSBuildPlatform” select X86 


Save your build definition
Steps-11
“.proj” file should be on the “TeamBuildTypes” folder as same shown on the above diagram.

Now Open TFSBuild.proj files into visual studio and mention the project to build in proper order
Set the build configuration

 <ItemGroup>
    <ConfigurationToBuild Include="Release|AnyCPU">
      <FlavorToBuild>ReleaseFlavorToBuild>
      <PlatformToBuild>AnyCPUPlatformToBuild>
    ConfigurationToBuild>
ItemGroup>

Mention the project name you want to build and mention the folder name where you want build output files.

<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/../../ProjectRootFolder/Dev/First.csproj">
       <Targets>Targets>
  <Properties>OutDir=$(BinariesRoot)\$(Configuration)\OutFolderName\;OutputPath=$(BinariesRoot)\$(Configuration)\OutFolderName\;Properties>
       SolutionToBuild>
ItemGroup>

$(BuildProjectFolderPath): This is the path you mentioned on the steps-8 “Source Control Folder”
$(BinariesRoot)\$(Configuration)\: This is the path you mentioned on the steps-9 (\\ServerName\Ritesh\)
OutFolderName: This is the folder where your First.csproj file output stored after build.
//ServerName : Build machine name
So finally “First.csproj” output will be stored into “//ServerName/Ritesh/OutFolderName/”, all Dll’s, bin folder and config files will be stored into this folder after build.
Call another "Proj" from first "Proj" file
Little advanced , You can call another msbuild file from proj file. Your msbuild file should be on the same folder where your proj file exists
So your “.msbuild” file should be as below folder structure
Project RootFolder à TeamBuildTypes à ProjectBuild à Build_Database.msbuild
Once you have “.msbuild” file ready you can specify new ItemGroup into “.proj” file like below.
 
<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/../Build/Build_Databases.msbuild">
             <Targets>Targets>
<Properties>OutDir=$(BinariesRoot)\$(Configuration)\Release\Databases\; OutputPath=$(BinariesRoot)\$(Configuration)\Release\Databases\;Properties>
SolutionToBuild>
ItemGroup>

Let’s say you have some supporting files into the VSTS folder which should be a part of your project build and you want to copy those files when you run the build then you can separate that kinds of things into different “.msbuild” file and include into “.proj” file as per your need.
You can copy some of the folders or files from source save into your build folder like below

“Build_Databases.msbuild” 

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
<PropertyGroup>
<DirectoryDatabaseRoot>$(MSBuildProjectDirectory)\..\..\SDPvNext\Dev\DatabasesDirectoryDatabaseRoot>
<DirectoryDBOperations>$(DirectoryDatabaseRoot)\OperationsDirectoryDBOperations>
<DropFolder>$(DropLocation)\$(BuildNumber)Dropfolder>
<DBNameOperations>OperationsDBNameOperations>
  PropertyGroup>

With the following line I am copying $/VSTSRootFolder/Database/Operation folder into my build location (//ServerName/Ritesh/Release/Databases/Operation)
<Target Name="CopyFiles">
<Exec Command="xcopy /y /e "$(DirectoryDBOperations)" "
$(Dropfolder)\Release\Databases\$(DBNameOperations)\""/>
Target>

<Target Name="Run">
       <CallTarget Targets="CopyFiles"/>
Target> 

Steps-12
Once your build created, you can edit the build definition or Run build definition manually, to run build manually go to the build definition, right click and clicks on  “Queue New Build” check some property like
  • Build controller : the controller you created on steps-3
  • What do you want to build :  This will tell whether you want latest code to build or some old code from VSTS
  • Set the priority and: Normal/High etc.
  • Drop folder for this build : here your output files are going to save
Clicks on “Queue” button
Build process will get start and does the following
1)      Take latest code from VSTS
2)      Build the project as mentioned in the “.proj” file
3)      Copy all build output file into build machine “Target folder” as mention in the “.proj” file
4)      Copy all the files from VSTS to target folder in the build machine  as mentioned in the “.msbuild” file
5)      Display the status of the build, Succeed or Fail

References
Understanding a Team Foundation Build System
Video: Let Build Automation in Microsoft Visual Studio Team Foundation Server 2010 Work for You

Troubleshoot

While doing the build I got lots of build errors, wanted to show here with solution
MSBuild Fail Error: The path is already mapped in workspace WORKSPACE NAME
The complete error was “error: The path C:\Builds\RiteshTest\Sources is already mapped in workspace 2113_459_MachineName”
This error occurs when you already have one Workspace defined for one Build definition and again you are trying to create new build definition on the same Workspace.
To solve this error go to the build agent and change the working directory for your new Build Definition
MSBuild Fail Error: The Silverlight 4 SDK is not installed. Using TFS 2010
The complete error was: “C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.Common.targets(104,9): error : The Silverlight 4 SDK is not installed”
I resolved this issue by changing MSBuild Platform Setting from Auto to X86 on the Build Definition Window. You need to make sure to avoid this error
·         Make sure you have Installed Silverlight 4 SDK on build server.
·         On the Build Definition window, change the MSBuild Platform from Auto to x86, as described here
MSBuild Fail Error: The type 'Silverlight.Web.User' already contains a definition for 'DisplayName'
I got this error when I was trying to make automated build for Silverlight 4.0 application using TFS 2010.
The complete error was “Generated_Code\Models\Shared\User.shared.cs (13): The type 'MSIT.ES.SDPvNext.SilverlightUX.Web.User' already contains a definition for 'DisplayName'”
I resolved this issues by changing Platform and Configuration combination on the TfsBuild.proj file previously it was “Release|x86” and on the Build Definition window, change the MSBuild Platform from Auto to x86.
TfsBuild.proj 

<ItemGroup>
    <ConfigurationToBuild Include="Release|AnyCPU">
      <FlavorToBuild>ReleaseFlavorToBuild>
      <PlatformToBuild>AnyCPUPlatformToBuild>
    ConfigurationToBuild>
ItemGroup>