Monday, June 12, 2017

Open New Window (or Response.Redirect) from Server Side (Code Behind) in ASP.Net using C#

Here Is the question I received from one of my colleagues, how to open new page from server side in a new window. There could be two possible ways to get it work.

  • First option is to add javascript code to the button's OnClientClick handler. But this handler should be attached before the control is rendered, let's say in button's OnPreRender event.

    protected void MyButton_OnPreRender(object sender, EventArgs e)
        string newPageUrl = "NewPage.aspx";
        MyButton.OnClientClick = "'" + newPageUrl + "'); return false;";

    This technique would not let the request post back to the server, so it could save this round-trip.

  • Second option could be use of Response.Write() method on regular button click event.


    This would send a post back to the server, but the advantage to this method could be that it will allow you write code on button click you may want to execute before opening a new window.

Monday, June 5, 2017

C# - Delete files older than N number of Days/Months in a directory

In this post, I will share C# code to delete files from specific directory, with the condition that only files which are older than N number of Days or Month or any other time unit. Why I come to this code because recently we faced this problem because of not deleting the older files.

I was working on a small utility program to automate repeated tasks, one of which is to take daily backups for my project's database. This was done before, but after some time we faced this problem because we were running out of space, it happens for the reason that we have lot of backup files generated and no one is going to delete older files which are of no-use. The best solution come to my mind is to modify the program code to delete older files by it-self.

Here is the C# code for this task.

string[] files = Directory.GetFiles(directoryPath);

foreach (string file in files)
   FileInfo fi = new FileInfo(file);
   if (fi.LastAccessTime < DateTime.Now.AddMonths(-1))

Obviously you can change DateTime.Now.AddMonths method to AddDays, AddHours or other units according to your requirements.

Thursday, May 25, 2017

How to generate script for multiple objects using SQL Server Management Studio

In this post, I will explain how to script multiple objects using SQL Server Management Studio. You may be aware of generating script by right clicking on specific object (Table / Stored Procedure / Function / View etc), or also you may generate scripts by right clicking on the database node, select Tasks, and then click on Generate Scripts... option. In this second option you can generate script for all objects or for some selected objects with advanced scripting options.

But in this post I will explain a short trick to generate script for multiple objects without going through the Generate Scripts... wizard. Obviously it will not provide other advanced options that we can have while using the Generate Script wizard, but it will let you create script for Create / Drop scenarios.

Following are the steps required to generate script for multiple objects, here I am considering generate Create script for selected tables.

  • Within SQL Server Management Studio, select your database.
  • Since I am going to generate script for tables, so I selected the Tables node, you may want to select Stored Procedures, Functions or Views etc.
  • On the right hand side you may see Object Explorer Details view, if not, you can make it visible from menu View > Object Explorer Details, or simply you can hit F7 shortcut key.
  • In details view, select the required objects.
  • Right click on the selected objects > Script Table as > Create To > New Query Editor Window (or other option)

Thursday, April 13, 2017

IIS - How to add Module Mapping for PHP

In this post, I will explain how to enable IIS for hosting PHP websites by Add Module Mapping for PHP files.

Following are the steps required to add module mapping.

  • Open the IIS Manager, from the Connections panel on the left, click on the hostname of your server.
  • Double-click on the Handler Mappings option.

  • On right side of the Handler Mappings screen, from Actions panel, click on Add Module Mapping.

  • Type the following information into the appropriate text boxes, and then click OK.

    • Request path: *.php
    • Module: FastCGImodule
    • Executable: C:\php\php-cgi.exe
    • Name: PHP_via_FastCGI

  • On the confirmation dialog for creating FastCGI Application, Click Yes.

Now, your IIS should enable to host PHP files. IIS has to be restarted to make new changes in effect.

Sunday, March 26, 2017

How to Install Laravel framework on Windows with XAMPP (as PHP development environment)

In this post, I will explain how to install Laravel framework on Windows, with XAMPP as PHP development environment.

First you have to install XAMPP you can find here: XAMPP Installer

You can install Laravel framework by using Composer. You can download composer from Composer setup will ask for PHP installation path, in my XAMPP installation it is:


You can verify if composer is correctly installed, by executing the command composer in command prompt. If you get an error message, it means it is not added in the Environment Variables, so you have to add the composer.exe folder path in Environment Variables.

In-case if you have already have installed composer, its better to update it-self to the latest version before moving forward. Run the following command to update composer it-self.

composer self-update

Here we finished installing composer, and now we have to install Laravel. Move to a web-accessible folder, the folder where we usually place all web projects. In my XAMPP installation it is D:\xampp\htdocs

Now go to command prompt, move to the above mentioned web-accessible folder, and execute following command:

composer create-project --prefer-dist laravel/laravel myweb

It will install Laravel framework to a folder named myweb inside the htdocs folder, you can use your own project name in place of myweb (last part in the above command). Then composer will download and configure all the required dependencies, and finally your Laravel basic application is now ready.

If you have PHP installed locally with XAMPP and you may use PHP's built-in development server to serve your application. To make our development envrionment to server this website we have to use the artisan command. This command will start a development server.

You can try access it by localhost path:


You should see the following home page for Laravel in your browser.

And it's done. We have successfully installed Laravel framework on our machine.


Wednesday, February 22, 2017

Power BI Desktop - Accessing SQL Server Database - Error: The user was not authorized

In this post, I am sharing a solution for a problem I faced while I started using Power BI. When you try to connect with SQL Server as Data Source, you may encounter this error message:

The user was not authorized

And it will not let you proceed.

There could be two possibilities for this problem you can try and fix it. First of all, of-course check if the user credentials are correct for login purpose. In my case, I am using a user name which letting me login to my SQL Server successfully, but when I try to connect through Power BI by same user name, it starts giving me the above error message.

Here is how I fixed it.

Solution 1:

  • Go to SQLSERVERNAME > Security > Logins > select you desired user name

  • Right click on selected user, select Properties, and go to Server Roles page.

  • Assign sysadmin role by setting the checkbox as true.

  • Click OK.

Now if you try login from Power BI desktop client, it should login successfully.

Just in cause, if it still not logged-in, you may try this second option. I don't know the real reason for this behavior but I found on internet while searching for my problem and find this workaround, so I am sharing it here if it may help someone facing similar problem.

Solution 2:

Your log-in screen may be similar to this:

Note that here Windows tab is selected and providing two options as Use my current credentials and Use alternate credentials. You might have tried both and it did not worked.

The trick is that, just use the second tab below Windows, named Database. Similar to this:

Put your user name and password in this tab, and it should work now.

I hope you find this post helpful. I would be happy to receive your thoughts about this, if you have encountered similar problem and fixed it. In case if you have tried some other option, please just mention in the comments below and I will add in this post so others people could get benefit.

Thursday, February 9, 2017

Cannot open backup device. Operating System error 5

I created small utility program for different routine tasks to run on different schedules, one of which is to take database backup to a local folder, and it was working fine. To have access it across network for selected users, and making it available for copy/move to other backup devices, we shared this folder on network. This was the moment I start getting this error in utility error log. In this post I will show the trick I used to solve this problem in our case. First let me share the script I used to take backups, here it is:

TO DISK = 'D:\DBBackups\mydb-datetimestamp.Bak'  

After sharing the folder on network for specific users, it started generating the following error message:

    Msg 3201, Level 16, State 1, Line 1
    Cannot open backup device 'D:\DBBackups\mydb-datetimestamp.Bak'. Operating system error 5(Access is denied.).

    Msg 3013, Level 16, State 1, Line 1
    BACKUP DATABASE is terminating abnormally.


  • Open Windows Services Screen. (Start > Administration > Services)
  • Find the Service in the list with name SQL Server (MSSQLSERVER), check for the Log On As column (if it doesn't already exist in the list, you can add it from the View menu > Add / Remove Columns...).

    The name of the account you see in Log on As column, is the account you need to give permissions to the directory where you want to place backups.

  • Right click on the folder. Go to > Properties > Share and Security tabs. And add permissions for required account.
  • Click Apply / OK, and let the permissions take effect.

Now try taking new backup, and it should work successfully.

Sunday, January 15, 2017

Crystal report document load hangs

I faced this problem after I made some changes in Crystal Report design. Somehow in Page Setup dialog box, the printer is changed in drop-down list, pointing to my local PC printer. And it was working fine at local PC but getting stuck when crystal report document is calling its Load() method on server.

Here is how I fixed it in my case:

  • Open report in Crystal Report Designer and Right Click
  • Select Design > Page Setup..., Page Setup Dialog box appears.

  • From the printer drop-down list, select Microsoft XPS Document Writer.

  • Click OK button.

Then I deployed the new report file on server, it stars working.