Tuesday 22 September 2020

How to find first weekday in the month in MSSQL


declare @n datetime = dateadd(mm,0,getDate())

   declare @FirstWeekDayOfMonth int

   SELECT @FirstWeekDayOfMonth= CASE

         WHEN Datename(dw, Dateadd(dd, -Datepart(dd, @n) + 1, @n)) = 'Saturday' THEN Day(Dateadd(dd, -Datepart(dd, @n) + 3, @n))

         WHEN Datename(dw, Dateadd(dd, -Datepart(dd, @n) + 1, @n)) = 'Sunday' THEN Day(Dateadd(dd, -Datepart(dd, @n) + 2, @n))

         ELSE Day(Dateadd(dd, -Datepart(dd, @n) + 1, @n))

       END 

   select @FirstWeekDayOfMonth



Note: to find out first day of next month , change  first line to dateadd(mm,1,getDate()) 


1st day of next N month , change the first line to dateadd(mm,n,getDate())

Monday 7 September 2020

Unit test c#: Create Fake Httpcontext to bypass Null exception for User object

 Microsoft Visual studio Test Framework


Requirement: Nuget package Moq 4.13.0.0 (or later)

Settings in visual studio: Menu->Tools->Options ->Debugging->General
Tick "Use Managed Compatibility Mode"

Code: (copied from stackoverflow)
Step 1: 
Create a new class FakeHttpContext and paste the below code. Change the Returns value to whatever user name you need.

public static class FakeHttpContext
    {
        public static void SetFakeContext(this Controller controller)
        {

            var httpContext = MakeFakeContext();
            ControllerContext context =
            new ControllerContext(
            new RequestContext(httpContext,
            new RouteData()), controller);
            controller.ControllerContext = context;
        }


        private static HttpContextBase MakeFakeContext()
        {
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            var user = new Mock<IPrincipal>();
            var identity = new Mock<IIdentity>();

            context.Setup(c => c.Request).Returns(request.Object);
            context.Setup(c => c.Response).Returns(response.Object);
            context.Setup(c => c.Session).Returns(session.Object);
            context.Setup(c => c.Server).Returns(server.Object);
            context.Setup(c => c.User).Returns(user.Object);
            user.Setup(c => c.Identity).Returns(identity.Object);
            identity.Setup(i => i.IsAuthenticated).Returns(true);
            identity.Setup(i => i.Name).Returns("sanjeeb");

            return context.Object;
        }
    }


Step 2: 
Initialize the Fakehttpcontext in test initialize function. 

FakeHttpContext.SetFakeContext(usersController);



Accessing SMB share from Raspberry pi (or linux)

 Install smbclient :

sudo apt update 
 
sudo apt install smbclient
 
 
List the share items in the server: 
smbclient -L host
for example: smbclient -L 192.168.0.30
 
you need to enter password.then you'll see the result like this:
 
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.9.5-Debian]

 Sharename       Type      Comment
 ---------       ----      -------
 print$          Disk      Printer Drivers
 RaspberrypiShare Disk      
 IPC$            IPC       IPC Service (Samba 4.9.5-Debian)
 pi              Disk      Home Directories
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.9.5-Debian]

 Server               Comment
 ---------            -------
 RASPBERRYPI          Samba 4.9.5-Debian

 Workgroup            Master
 ---------            ------- 
 
 
To browse Shared folder :
smbclient \\\\192.168.0.30\\RaspberrypiShare pa$$w0rd
 
 

Temperature/CPU/Memory info of Raspberry Pi in console

 Open the console/terminal in the desktop or

Connect to the raspberry pi via ssh and type in the below command


For temperature:
/opt/vc/bin/vcgencmd measure_temp

For CPU info:
cat /proc/cpuinfo

For Memory info:
cat /proc/meminfo

Add Python(or Java or anything) to Environment variables

After installation of python in windows 10, python is not added to environment variable by default. If you want to use cmd for python, then python installed path must be configured in the environment variable. 

When you type python in cmd, you'll see the below message:

Follow the below steps to add python to environment:
1) Open System in Control panel
2) Click Advanced system settings
3)Click Environment Variables
4) Select Path in User Variables and click edit
5) Either create new and type in the path of Python installed location or Browse the folder using browse button in the above screenshot.

6) Click OK and close the Environment variables settings window and all other opened windows. 
7) Start a new command prompt and type python to test. 









Done!



Generate SQL script from entity framework using powershell/visual studio

to run the below command , start PowerShell in visual studio. Select the database project. In PowerShell:   Script-Migration this command wi...