Thursday, June 20, 2024

How to Rename a Solution and Project in Visual Studio

Renaming the Solution:

1. Close the Solution in Visual Studio

— Before making changes, ensure that the solution is closed in Visual Studio.

2. Rename in File Explorer

— Navigate to your solution directory.

— Locate the `.sln` file and rename it to your desired name.

— Tip: If there are solution user options (`.suo`) file, consider deleting it or renaming it to match the new solution name. Don’t worry; it will be regenerated.

3. Reopen in Visual Studio

— Launch Visual Studio and open the renamed solution file.

Renaming the Project:

1. Rename in Solution Explorer

— Right-click on the project you wish to rename.

— Select “Rename” and type in the new name.

2. Update the Default Namespace (Optional)

— With the project highlighted, open the Properties window (hit `F4`).

— Adjust the “Default Namespace” if you want your new items to adopt this namespace.

3. Rename Project File and Directory in File Explorer

— Go to the directory containing your project.

— Rename the project file (`.csproj` for C# projects).

— Consider renaming the directory of the project to keep things consistent.

4. Re-link the Renamed Project in Visual Studio

— Your project might now be “unavailable” since its path has changed.

— Remove the unavailable project by right-clicking and selecting “Remove”.

— Add it back by right-clicking on the solution (or relevant folder) > “Add” > “Existing Project”. Find your renamed project file and add it.

5. Update Namespaces in Code (Optional)

— To change the namespace in your code:

— Right-click on the project in Solution Explorer.

— Choose “Refactor” > “Rename” to rename the namespace across files.

— Note: Always review the changes and test your project after such a refactor.

6. Rebuild and Test

— After the renaming process, rebuild the solution to ensure everything compiles and runs as expected.

Final Thoughts:

Renaming might seem like a simple task, but it’s essential to follow the steps accurately, especially if you’re using source control. Always commit your changes before renaming to ensure a clean history and easy tracking. Happy coding!

Friday, January 19, 2024

Recursion using CTE in SQL Server

USE [tempdb]

GO

      -- Creating Employee details table

        IF EXISTS(SELECT * FROM SysObjects WHERE name = 'EmpDetails' AND xtype = 'U')

BEGIN

       DROP TABLE [EmpDetails]

END

GO

       CREATE TABLE [EmpDetails]

              (EmpID VARCHAR (10),

               EmpName VARCHAR (20),

               ManagerID VARCHAR(10),

               PRIMARY KEY (EmpID))

GO

       INSERT INTO [EmpDetails] VALUES ('100','ARKA GUPTTA',NULL)

       INSERT INTO [EmpDetails] VALUES ('101','SAIKAT','100')

       INSERT INTO [EmpDetails] VALUES ('102','DEBRAJ','100')

       INSERT INTO [EmpDetails] VALUES ('103','ANIRBAN','100')

       INSERT INTO [EmpDetails] VALUES ('104','SUMAN','101')

       INSERT INTO [EmpDetails] VALUES ('105','SUPRIYO','102')

       INSERT INTO [EmpDetails] VALUES ('106','AMLAN','102')

       INSERT INTO [EmpDetails] VALUES ('107','UTPAL','102')

       INSERT INTO [EmpDetails] VALUES ('108','PONCHU','105')

       INSERT INTO [EmpDetails] VALUES ('109','DEBABRATA','105')

       INSERT INTO [EmpDetails] VALUES ('110','BITTU','107')

GO

       SELECT * FROM [EmpDetails]

GO

       -- Excute the below query to find the Manager of each Employee using Recursive CTE.

WITH

       cteReports (EmpID, EmpName, ManagerID, OrganizationLevel)

AS

(     

       -- Anchor Query

       SELECT A.EmpID, A.EmpName, A.ManagerID, 1

       FROM   dbo.[EmpDetails] A

       WHERE  A.ManagerID IS NULL

        --     Seperator

       UNION ALL

        -- Recursive Query  

       SELECT A.EmpID, A.EmpName, A.ManagerID, R.OrganizationLevel + 1

       FROM   dbo.[EmpDetails] A

              INNER JOIN cteReports R

              ON A.ManagerID = R.EmpID

)

       SELECT EmpID, EmpName, ManagerID,

              (SELECT EmpName FROM [EmpDetails] WHERE EmpID = cteReports.ManagerID) 'ManagerName', OrganizationLevel

       FROM   cteReports

              ORDER BY OrganizationLevel, ManagerID