May 18, 2018

Error in Crystal Report: could not load file or assembly

I moved crystal report files from ASP.Net website to Windows Forms Application, and run the application, then I got this error message on loading reports.

Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.

By adding following settings in app.config file solved this issue for me.

 <startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
 </startup>

April 16, 2018

Read web.config file outside the application folder in C#

I have a web application running on my server. And I needed to develop a small utility application which has to do some work in database, and I don't want to write same database credentials or connection strings(and some other additional app-settings) in this new console application, I want it to read settings from same web.config file. But the problem I was facing when I tried this code:

 string filePath = @"D:\MyProject\Web.config";
 Configuration c1 = ConfigurationManager.OpenExeConfiguration(filePath);
 var value1 = c1.AppSettings.Settings["Key1"].Value;

It was not giving any error if it was failed to load the given file path, but also was not wokring as per my expectations. The Configuration object c1 was loaded fine but when I tried to read ApSettings, it was empty. There was no key/value pairs loaded in this collection object. So trying to read value for Key1 was giving me error as:

 Object reference not set to an instance of an object. 

After wasting much time thinking and searcing on this issue, I found this solution. It worked fine and allowed me read config file from another application.

 string filePath = @"D:\MyProject\Web.config";
 var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
 var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
 var value = configuration.AppSettings.Settings["Key1"].Value;

Here I get proper value of Key1, in variable value.

March 31, 2018

Running PHP script from the command line

I was need to call my php script from command prompt in Windows. This is what I found:

Let's say, we have PHP file myscript.php in Test folder inside htdocs folder of XAMPP installation (this is not mandatory, you can place your php file anywhere on your local hard drive), and I want to call it from command prompt.

We need to write it this way:

 php "D:\xampp\htdocs\Test\myscript.php"

It is assumed that your php.exe file path is set in environment PATH variable. If it is not included in environment PATH variable then you may need to write full path for php.exe. In my case, it is like this:

 "D:\xampp\php\php.exe" "D:\xampp\htdocs\Test\myscript.php"

March 17, 2018

XAMPP - PHP - Connect with MS SQL Server 2008 R2 in Laravel

Few days back I got stuck in this situation, I was developing PHP web application with Laravel 5.6 framework, the problems comes when I tried to connect with MS SQL Server rather than MySQL or MariaDB (that comes as default in XAMPP package), It started giving me error like driver not found. Here is how I solved this problem.

First you need to download Microsoft Drivers 4.3 for PHP for SQL Server, then extract the files to some folder. Copy the file php_pdo_sqlsrv_71_ts_x86.dll to php/ext folder (from XAMPP installation's root folder). Now we have to instruct PHP to use this extension.

Open php/php.ini file and move the cursor to extensions section. Add following new line of extension which directs PHP to use SQL Server driver's dll.

extension=php_pdo_sqlsrv_71_ts_x86.dll

We have successfully set-up our PHP environment with MS SQL Server Driver.

Don't forget to restart your server afterwards.

Now you should be able to successfully connect with SQL Server from PHP.

Here comes the Laravel part. You have to specify connection string parameters in .env file of Laravel root folder.

DB_HOST=MY-PC\MYSQL2008R2
DB_PORT=1433
DB_DATABASE=ITEMMASTER
DB_USERNAME=myuser
DB_PASSWORD=mypassword

Or alternative way is to define connection parameters in config/database.php file.

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => 'MY-PC\MYSQL2008R2',
            'port' => '1433',
            'database' => 'ITEMMASTER',
            'username' => 'myuser',
            'password' => 'mypassword',
            'charset' => 'utf8',
            'prefix' => '',
        ],

Now Laravel website should be able to connect with SQL Server database.

I hope you found this post helpful. Do you agree? Share your thoughts in the comments below!

February 24, 2018

C# - Set DateTime required format for each request in ASP.NET MVC

I was being asked this question by one of my colleague, if we can set a DateTime format at some global level so that if we need to display/process date at multiple controllers or views, we should not be worried about a specific format. I came up with this solution, I am sharing here, and welcome your suggestion or comments for any improvements or alternative solutions.

Lets say we need to display and save date in yyyy/MM/dd format. Since our server default format is set to dd/MM/yyyy, and we could not change it because some other applications might get affected, so we decided to set the required format at request level in our ASP.Net MVC application. For this we have to set the date format for current culture for each request, i.e. inside Global.asax.cs, in Application_BeginRequest() event.

Here is the real code which will set the format for current culture with the required one.

protected void Application_BeginRequest(Object sender, EventArgs e) 
{
 CultureInfo newCultureInfo = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        newCultureInfo.DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
        //newCultureInfo.DateTimeFormat.LongDatePattern = "yyyy/MM/dd";
        //newCultureInfo.DateTimeFormat.FullDateTimePattern = "yyyy/MM/dd";
        newCultureInfo.DateTimeFormat.DateSeparator = "/";
        Thread.CurrentThread.CurrentCulture = newCultureInfo;
}

Here I am setting the DateTimeFormatInfo object's ShortDatePattern property to "yyyy/MM/dd", in CurrentCulture. Similarly you can set other required properties like LongDatePattern, FullDateTimePattern, ShortTimePattern or LongTimePattern etc. In this way you don't have to set the required date pattern in multiple places.