Pages - Menu

Create a Script for Daily SQL Backup Job with Unique Name

Scope

Create a SQL script or job that manage daily sql backup with various options.

Code

The below script is generated from Sql Server Management Studio with my own little tweak that append a timestamp to the file name. This will create backup with file name MyDatabase_yyyy-mm-ddThh.mi.ss.mmm.bak
Declare @SQLPath nvarchar(4000)
Set @SQLPath = N'D:\Backup\MyDatabase_' + Replace(CONVERT(nvarchar(30), GETDATE(), 126), ':', '.') + '.bak' 
BACKUP DATABASE [MyDatabase] TO DISK = @SQLPath WITH NOFORMAT, INIT, NAME = N'MyDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10

GO

PS. According to Microsoft. By default, SQL server will backup as NOINIT (append). For those who do not use the timestamp and always backup to the same filename, must make sure the right options are set for INIT or NOINIT depending on backup strategy of the business.

Example

A simple cursor example that does backup on all user databases and override the last backup.
DECLARE @name VARCHAR(50)
DECLARE @path VARCHAR(255)
DECLARE @fileDate VARCHAR(20)
DECLARE @fileName VARCHAR(255)
 
SET @path = 'D:\Backup\'  
SET @fileDate = Replace(CONVERT(nvarchar(30), GETDATE(), 126), ':', '.')

DECLARE db_cursor CURSOR FOR  
SELECT name 
FROM sys.databases
WHERE name NOT IN ('master','model','msdb','tempdb') -- system databases
and [state] = 0 -- online
 
OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0   
BEGIN   
       SET @fileName = @path + @name + '.bak'  
       BACKUP DATABASE @name TO DISK = @fileName
       WITH NOFORMAT, INIT, 
       SKIP, NOREWIND, NOUNLOAD, STATS = 10

       FETCH NEXT FROM db_cursor INTO @name
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

Xml Deserialization with Namespace Prefix in C#

Scope

  • To deserialize an xml from a 3rd party vendor into a strongly typed C# object.
  • The xml has a namespace attached.
  • No DTD or XSD were provided by vendor.

Steps

XML Schema Definition Tool (Xsd.exe)



Basically 2 command lines I executed:
  1. I first created the xsd file from a sample xml file that I have.
  2. Then I created the cs class file for the model by using the xsd scheme. 

Model

It is the best to generate model via Xsd tool because it contains a lot of System.Xml.Serialization Attribute that are needed for the serializer. Especially around the namespace area!! 

The class file will look something like this. A typical model class with lots of attribute hints.



Serializer

All the hard works were done for generating the model and xml attributes. That left this part a bit simple in comparison. It is a bit similar to Autofac, where the hard works are done at the entity level and the actual conversion is just 1 line of ToEntity().

The code looks like this. Some people managed to do this in 2 lines, but I found 4 lines look pretty neat for readability and my troubleshooting.



Troubleshoot


Unbounded Complex Type Bug

1 of the error that I ran into looks like this.

Unable to generate a temporary class (result=1). error CS0030: Cannot convert type

It is a confirmed Microsoft bug and won't be fixed.

The work around is to change the unbounded or add a dummy element for the complex type.


I added the element and regenerated the model class file, and the serializer is happy again.

Xsd generated incorrect schema

The xml schema reverse engineered by using xsd.exe from a sample xml file is slightly incorrect. It predicted I might have multiple child nodes, thus the class generated had all these unnecessary array declaration.

Looks like this.

   1: public MyComplexType Notes []


I would not blame the xsd tool but there are some manual works required to fix it. (Or C# code will end up with a lot of .FirstOrDefault()

XElement.Name.LocalName

As described before, there is a fallency of trying to access the element via XElement.Name. This method only works when there is no namespace. Out of curiosity and I read further that the correct way is to use XElement.Name.LocalName. I wouldn't recommend going down this path, but it is interesting to know that how we conventionally accessing an element name incorrectly (when you have namespace).

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.name%28v=vs.110%29.aspx

My first nopCommerce website gone Live!!

nopCommerce

After spending 6 months with this open source nopCommerce platform. My first nopCommerce website finally went live. http://www.thenorthface.com.au 

Scope

Our business requirement in a nut shell:-
  • Build a multi tenant e-commerce platform to cater all of our brands.
  • All our brand sites are independent to each others in terms of development requirements or target audience. Only the main workflow is the same.
  • We have multiple integration points that we need to interact with. (Not surprise!!)
  • The first brand is The North Face

Challenges

  • 6 Integration Points
    • CRM (Customer Information)
    • CMS
    • Payment Gateway (CyberSource)
    • Promotions / Product Price (1 way coming in from other integration point)
    • Inventory System
    • Product Reviews
  • 2 Console Apps
    • Bulk Image Upload
    • Inventory System / Price

Thoughts of nopCommerce


Likes

  • Open Source
  • MVC
  • Easy to learn
  • The source code is fairly solid and clean. There were likes and hates, but I am generally happy.
  • Plugins readily available to purchase or easy to build your own
  • Payment workflow is rock solid and easy to integrate with (I am using CyberSource)
  • Multi-tenancy semi-friendly
  • Multi-store ready
  • Admin interface out of the box is nice and professional enough
  • Good support from the official forum

Dislikes

  • There is no Controller Factory in their MVC architecture. I kind of expected that for a "platform"
  • Telerick MVC for admin (but they are now using Kendo UI in v3.3, which is good now)
  • Easy to customize? This is an arguable point. Surely it wasn't hard but could be easier. As a platform, I would like to see the core code set to protected/virtual (to allow custom code override) and more hooks (empty protected class) in the core controllers.

The North Face Australia

A quick comparison of old and new site. Which one do you like more?

Level 1 (Root) Category 

 


L: The level 1 category is a little messy. At the root level, it shows everything from all the child categories.
R: The level 1 category shows featured products of level 2 categories, this is now organized in a systematic way that allow administrator to decide what to show. 

Level 2 (Children) Sub-Category 

 


Color variants of the same product no longer clogging up space. 
We can now show more variety of products on category page.

Category Page - Filter

 


Color filters now more colorful than ever!!

Product Detail Page

 


Customer can choose different color on product detail page.

Shopping Cart

 


Demo

Not too bad? Why not try it out yourself? It may change your decision making should you need to choose an ecommerce platform next time. Definitely recommended.

A potentially dangerous Request.Form value was detected from the client

Troubleshoot

A potentially dangerous Request.Form value was detected from the client

One of the error I am getting when performing a form post with HTML tags in .Net.

Solutions

Web.config


One of the StackOverflow 150+ voted answer was this. 

<httpRuntime requestValidationMode="2.0" />

Put this in the Web.config and it will work. It is not my taste to change a site wide config for a form post though. I potentially want to block all other requests that post potentially dangerous HTML tags but except my Simple method.

ValidateInput = false


One of the simple solution available on the net is this. Set ValidateInput to false.



This works, but it potentially allowing the post not to check validation. I think I can still do better!!

AllowHtml


Potentially, I am setting AllowHtml attribute to a field that I would allow html. Everything else stay away.





MSBuild Error: Task could not find "sgen.exe" using the SdkToolsPath

Scope

I am getting an error when trying to build in release mode for one of my project. The error looks like this in Visual Studio 2012.

Task could not find "sgen.exe" using the SdkToolsPath "" or the registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0A\WinSDK-NetFx40Tools-x86". Make sure the SdkToolsPath is set and the tool exists in the correct processor specific location under the SdkToolsPath and that the Microsoft Windows SDK is installed




Step In

Search around on the internet, it appears that sgen.exe is an XML Serializer Generator Tool (MSDN). It appears to me that I have 2 solutions.

Solutions

Disable "Generate serialization assembly"

If we turn off "Generate serialization assembly", then the compiler will not be looking for sgen.exe to precompile the serialization assembly.

Locate sgen.exe

A better solution would be fix the sgen.exe so that VS can find it.

At an attempt to locate this sgen.exe for VS, I tried to download and install the following.
  • Microsoft Visual Studio 2012 SDK
  • Microsoft .NET Framework 4 (Web Installer)
  • Microsoft .NET Framework 4.5
Unfortunately, none of the above works except the below.
The v8.0A in the error message was a good hint of something about Windows 8

Conclusion

Finally, able to deploy from my machine in release mode for now. Until next time, I am sure I will run into similar issue again when building a continuous integration process and this article should come into handy.

nopCommerce - Instantiation of ICacheManager. PerRequestCacheManager not MemoryCacheManager?

Once you started looking into performance, and especially for category page, we would generally looking to cache the result set or page. By coincident, me and my colleague ran into the same problem, so I thought this could be quite common.

Scenario

While trying to implement caching strategy for category page, I found my results and in particular, my FeaturedProducts of the CategoryModel are not being cached while running in debug mode.

As usual, I created my custom controller as follow.


In my controller method, I would check and use the cache if it is available.

Steps In

In debug mode, immediately after the object is written to the cache, the object is in the cache. However, it will ALWAYS return false, and I if I step into the code, the object indeed not in the cache.
This can easily be over-generalised as one of the following without paying closer attention as:-

  • Cache not working
  • Nop is slow
  • Performance Issue

Solution

If we look at what nop provides for ICacheManager, there are 2 CacheManagers (We will ignore the NopNullCache for now) that implement ICacheManager, they are MemoryCacheManager (nop_cache_static) and PerRequestCacheManager (nop_cache_per_request).


As the names suggest, MemoryCacheManager writes objects to memory cache; PerRequestCacheManager writes objects to the session on per request bases.


In Nop.Web\Infrastructure\DependencyRegistrar.cs,  we found the following piece of code.





Without specifying what to register for ICacheManager in IoC, I found by default the PerRequestCacheManager (nop_cache_per_request) is instantiated.

This is an incorrect behavior for my problem as I want to put the content in the memory cache and made available to multi requests over a period of time (until times out).

We need to tell IoC which type of CacheManager we want to instantiate for the interface by using the following code. This will hint IoC to inject a concrete class of MemoryCacheManager to my ICacheManager!!