Wednesday, September 23, 2015

Content Negotiation : CONTENT-TYPE & ACCEPT HEADERS

When one client machine talks to another machine (lets say one computer talks to another) to get some information, it sends a HTTP request (There are many ways to talk, but we will limit our conversation to HTTP only), The HTTP request contains a lot of interesting data which can be manipulated by the server to build the response for them. HTTP request contains Headers & content. Headers are the meta data about the request. They can reveal every information, like what type of information is being sent, i.e. image/ text/ json/ xml etc. They can also mention what format of response they are expecting. Server process these headers to understand request & generate response type. This is called Content Negotiation. 

There are two very important headers which can be sent along the request to define these properties.

- CONTENT-TYPE (Define the data type of request)
- ACCEPT (Define the expected response format)


To explain this, Let's send some request to a local test server. First sending the request without any content-type set.

Request (No meta data)
POST http://localhost:3050/api/v2/tasks HTTP/1.1
User-Agent: Fiddler
Host: localhost:3050
Content-Length: 43
{"Subject":"This is the most interesting "}
Response
HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVEZTXExlYXJuaW5nXFdlYkFQSVxzcmNcV2ViQXBpMkJvb2suV2ViLkFwaVxhcGlcdjJcdGFza3M=?=
X-Powered-By: ASP.NET
Date: Wed, 23 Sep 2015 09:38:09 GMT
Content-Length: 794
 
You see that server returned 415 error, means it could not understand the request data type so as a return could not choose any media for-matter to serialize the data, even if we are passing the JSON. Now, let's exclusively define that we are sending JSON.

Request (JSON)

We are defining exclusively that we are sending JSON this time by defining the content-type header. There is a space between the code to define content of the request.

POST /api/v2/tasks HTTP/1.1
User-Agent: Fiddler
Host: localhost:3050
Content-Length: 43
Content-Type: application/json
{"Subject":"This is the most interesting "}


Response (JSON)

And yes, Server very easily understood the input type, choose a media formatter to process the request and send the response in default return type. The default return type in my application is JSON. The way request use to define its data type, response does the same, You can see a Content-Type in response as well, it means the response contains the JSON format. This will help client to understand the data type.

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVEZTXExlYXJuaW5nXFdlYkFQSVxzcmNcV2ViQXBpMkJvb2suV2ViLkFwaVxhcGlcdjJcdGFza3M=?=
X-Powered-By: ASP.NET
Date: Wed, 23 Sep 2015 09:45:31 GMT
Content-Length: 174
{"TaskId":0,"Subject":"In V2, Task Subject ","StartDate":null,"DueDate":null,"CreateDate":null,"CreatedBy":null,"CompleteDate":null,"Status":null,"Assignees":null,"Links":[]}


Request (JSON) expecting XML

Now Say, i want to send the JSON, but I want to see my results in XML, So now while I am sending the request to the server, I will also send the instructions to build the response according to my choice. I will define ACCEPT headers, means I will only accept predefined data type as return. It's server's job to build the result in this format. Client does not need to do anything here.

POST /api/v2/tasks HTTP/1.1
User-Agent: Fiddler
Host: localhost:3050
Content-Length: 43
Content-Type: application/json
Accept:application/xml

{"Subject":"This is the most interesting "}


Response (XML)

Now server honors the request and build the response in desired format & set its meta data. See below the content-type is XML means the data returned in XML. You can find the XML text in the content of the response.

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVEZTXExlYXJuaW5nXFdlYkFQSVxzcmNcV2ViQXBpMkJvb2suV2ViLkFwaVxhcGlcdjJcdGFza3M=?=
X-Powered-By: ASP.NET
Date: Wed, 23 Sep 2015 09:47:28 GMT
Content-Length: 387
In V2, Task Subject 0



Monday, May 4, 2015

Recover from DNN - SQL Injection attack

Many attackers attack DNN website and able to get into module definitions to update the values so when ever somebody opens the website, you start getting so many annoying popups. In worst cases website could go offline also.

During the investigation you will see errors like

 DotNetNuke.Services.Exceptions.ModuleLoadException: String was not recognized as a valid Boolean. ---> System.FormatException: String was not recognized as a valid Boolean. at System.Boolean.Parse(String value) at DotNetNuke.Modules.Admin.Modules.ModuleSettingsPage.BindData() in 

You need to get into your database to clean up these values. There are 2 tables where you need to take a look, See the table names below
  • dbo.ModuleSettings 
  • dbo.TabModuleSettings 
Inside these tables, Check the SettingValue which might be affected. Take a backup of these tables, and use the following script to clean up these tables 

See the entries
SELECT [TabModuleID]
      ,[SettingName]
      ,[SettingValue] as SettingValue_Old,
       left(SettingValue, charindex('

Update [dbo].[TabModuleSettings]
set SettingValue = left(SettingValue, charindex('

Friday, March 27, 2015

Update your window 7 Product key (Change product key link not available in window 7)

Normally, to change the product key in Windows 8, you open Control Panel, select System, and then click on the Change product key link.

If when you try to change the Windows product license key, you find that the Change product key link is not available or visible, you may want to check if your Windows is activated first, because this usually happens if Windows has not been activated yet.

You can activate Windows using the SLUI.EXE 3 command. You can get more information here on how to activate any version of Windows.

Or you can run the following command at an elevated command prompt says KB2750773:

slmgr.vbs /ipk  

You need to do it with Admin rights

Wednesday, March 18, 2015

Capturing bulk screenshots of entire website for review etc

There are many cases we need to review the entire website with business. It's always easy to take print out of the desired pages and pass it to business. Although there are many paid tools for SEO which helps getting the detail of specified page, I found a very good Mozilla Plugin to capture the screenshots. You just need to pass the list of all pages in a text file and you get screenshot of every page. You can login to target website before running the tool to capture the If some pages are under authenticated area

Extension Name : Grab Them All (Mozilla)

http://rafal.zelazko.info/2008/06/23/easy-screenshot-of-many-sites/



Tuesday, July 10, 2012

SQL Script to automatically generate CRUD Stored Procedures

Speed up development by automatically generating commmon stored procedure definitions

There are numerous times when developing a new web or windows application when you will need to write some stored procedures for the basic CRUD (Create, Read, Update, Delete) operations for each table in your database.
For instance a table will need a stored procedure defined for Inserting a record into it, deleting a specified record from it, selecting a specified record from it and selecting all records from it. Writing these Stored Procedures can be very repetitive and time consuming. Once you are familiar with how to write these procedures it can become a chore having to mindlessly type them all out.
The SQL script provided in this article generates 4 Stored Procedure definitions for each specified table(s) in your database. The auto generated Procedure definitions achieve the following:
  1. 1) List all records in the table (proc name suffix of _lst)
  2. 2) Get a specific record from the table (proc name suffix of _sel)
  3. 3) UPDATE or INSERT (UPSERT) a row- (proc name suffix of _ups)
  4. 4) DELETE a specified row - (proc name suffix of _del)
For a table called 'Location' the script will create procedure definitions for the following procedures:
dbo.udp_Location_lst
dbo.udp_Location_sel
dbo.udp_Location_ups
dbo.udp_Location_del
The stored procedure definitions can either be printed to the screen for you to copy and paste OR executed to actually create the Stored Procs. The stored proc names are prefixed with udp_ (user defined procedure) to avoid conflicts with any existing system stored procs.
The script uses the INFORMATION_SCHEMA tables to obtain information about the tables and columns in the specified database. The INFORMATION_SCHEMA is a SQL-92 standard that can be used in most major database systems including ORACLE and SQL SERVER.

Assumptions made by the script

This script assumes that the primary key will be used for selecting, updating and deleting an individual record and that the primary key is the first column in the table. The script also assumes if the primary key is an integer then it is an IDENTITY (autonumber) field.
This script is not suitable for the link tables in the middle of a many to many relationship.
After the script has run you will need to add an ORDER BY clause into the '_lst' procedures according to your needs / required sort order.
The script assumes you have set valid values for the config variables config section at the top of the script
You can set the script to run on all user defined tables in your database OR on a single specified table.
The script can be downloaded here

Example output

The following is the printed output from the script when it was set to run on the 'Customers' table of the 'Northwind' sample database that is provided with SQL Server 2000.
CREATE PROC [dbo].[udp_Orders_lst]
AS
SET NOCOUNT ON

SELECT [OrderID], 
 [CustomerID], 
 [EmployeeID], 
 [OrderDate], 
 [RequiredDate], 
 [ShippedDate], 
 [ShipVia], 
 [Freight], 
 [ShipName], 
 [ShipAddress], 
 [ShipCity], 
 [ShipRegion], 
 [ShipPostalCode], 
 [ShipCountry]
FROM Orders


SET NOCOUNT OFF
GO


CREATE PROC [dbo].[udp_Orders_sel]
 @OrderID int
AS
SET NOCOUNT ON

SELECT [OrderID], 
 [CustomerID], 
 [EmployeeID], 
 [OrderDate], 
 [RequiredDate], 
 [ShippedDate], 
 [ShipVia], 
 [Freight], 
 [ShipName], 
 [ShipAddress], 
 [ShipCity], 
 [ShipRegion], 
 [ShipPostalCode], 
 [ShipCountry]
FROM Orders
WHERE [OrderID] = @OrderID


SET NOCOUNT OFF
GO


CREATE PROC [dbo].[udp_Orders_ups]

 @OrderID int,
 @CustomerID nchar(5),
 @EmployeeID int,
 @OrderDate datetime,
 @RequiredDate datetime,
 @ShippedDate datetime,
 @ShipVia int,
 @Freight money,
 @ShipName nvarchar(40),
 @ShipAddress nvarchar(60),
 @ShipCity nvarchar(15),
 @ShipRegion nvarchar(15),
 @ShipPostalCode nvarchar(10),
 @ShipCountry nvarchar(15)
AS
SET NOCOUNT ON
IF @OrderID = 0 BEGIN
 INSERT INTO Orders (
  [CustomerID],
  [EmployeeID],
  [OrderDate],
  [RequiredDate],
  [ShippedDate],
  [ShipVia],
  [Freight],
  [ShipName],
  [ShipAddress],
  [ShipCity],
  [ShipRegion],
  [ShipPostalCode],
  [ShipCountry]
 )
 VALUES (
  @CustomerID,
  @EmployeeID,
  @OrderDate,
  @RequiredDate,
  @ShippedDate,
  @ShipVia,
  @Freight,
  @ShipName,
  @ShipAddress,
  @ShipCity,
  @ShipRegion,
  @ShipPostalCode,
  @ShipCountry
 )
 SELECT SCOPE_IDENTITY() As NewID
END
ELSE BEGIN
 UPDATE Orders SET 
  [CustomerID] = @CustomerID,
  [EmployeeID] = @EmployeeID,
  [OrderDate] = @OrderDate,
  [RequiredDate] = @RequiredDate,
  [ShippedDate] = @ShippedDate,
  [ShipVia] = @ShipVia,
  [Freight] = @Freight,
  [ShipName] = @ShipName,
  [ShipAddress] = @ShipAddress,
  [ShipCity] = @ShipCity,
  [ShipRegion] = @ShipRegion,
  [ShipPostalCode] = @ShipPostalCode,
  [ShipCountry] = @ShipCountry
 WHERE [OrderID] = @OrderID

END

SET NOCOUNT OFF

GO


CREATE PROC [dbo].[udp_Orders_del]
 @OrderID int
AS
SET NOCOUNT ON

DELETE FROM Orders
WHERE [OrderID] = @OrderID
SET NOCOUNT OFF
GO
You can see that the script formats the procedures nicely making it easy to follow the code in them and providing a consistent approach to naming conventions and formatting which helps speed up development time.

Conclusion

Whilst you can generate automatic 'SELECT / INSERT / UPDATE / DELETE statements using SQL Server Enterprise manager or SQL Server Management Studio these do not script the procedure definitions or parameter definitions / variable names used in the stored procs. The script provided can save you a great deal of time when you need to write repetitive CRUD stored procedures. Enjoy!.

With thanks from
http://www.sqlbook.com/SQL-Server/Auto-generate-CRUD-Stored-Procedures-40.aspx



Here's the Script
-- #########################################################
-- Author: www.sqlbook.com
-- Copyright: (c) www.sqlbook.com. You are free to use and redistribute
--  this script as long as this comments section with the 
--  author and copyright details are not altered.
-- Purpose: For a specified user defined table (or all user defined
--  tables) in the database this script generates 4 Stored 
--  Procedure definitions with different Procedure name 
--  suffixes:
--  1) List all records in the table (suffix of  _lst)
--  2) Get a specific record from the table (suffix of _sel)
--  3) UPDATE or INSERT (UPSERT) - (suffix of _ups)
--  4) DELETE a specified row - (suffix of _del)
--  e.g. For a table called location the script will create
--  procedure definitions for the following procedures:
--  dbo.udp_Location_lst
--  dbo.udp_Location_sel
--  dbo.udp_Location_ups
--  dbo.udp_Location_del
-- Notes:  The stored procedure definitions can either be printed
--  to the screen or executed using EXEC sp_ExecuteSQL.
--  The stored proc names are prefixed with udp_ to avoid 
--  conflicts with system stored procs.
-- Assumptions: - This script assumes that the primary key is the first
--  column in the table and that if the primary key is
--  an integer then it is an IDENTITY (autonumber) field.
--  - This script is not suitable for the link tables
--  in the middle of a many to many relationship.
--  - After the script has run you will need to add
--  an ORDER BY clause into the '_lst' procedures
--  according to your needs / required sort order.
--  - Assumes you have set valid values for the 
--  config variables in the section immediately below
-- #########################################################

-- ##########################################################
/* SET CONFIG VARIABLES THAT ARE USED IN SCRIPT */
-- ##########################################################

-- Do we want to generate the SP definitions for every user defined
-- table in the database or just a single specified table?
-- Assign a blank string - '' for all tables or the table name for
-- a single table.
DECLARE @GenerateProcsFor varchar(100)
SET @GenerateProcsFor = 'Orders'
--SET @GenerateProcsFor = ''

-- which database do we want to create the procs for?
-- Change both the USE and SET lines below to set the datbase name
-- to the required database.
USE Northwind
DECLARE @DatabaseName varchar(100)
SET @DatabaseName = 'Northwind'

-- do we want the script to print out the CREATE PROC statements
-- or do we want to execute them to actually create the procs?
-- Assign a value of either 'Print' or 'Execute'
DECLARE @PrintOrExecute varchar(10)
SET @PrintOrExecute = 'Print'


-- Is there a table name prefix i.e. 'tbl_' which we don't want
-- to include in our stored proc names?
DECLARE @TablePrefix varchar(10)
SET @TablePrefix = 'tbl_'

-- For our '_lst' and '_sel' procedures do we want to 
-- do SELECT * or SELECT [ColumnName,]...
-- Assign a value of either 1 or 0
DECLARE @UseSelectWildCard bit
SET @UseSelectWildCard = 0

-- ##########################################################
/* END SETTING OF CONFIG VARIABLE 
-- do not edit below this line */
-- ##########################################################


-- DECLARE CURSOR containing all columns from user defined tables
-- in the database
DECLARE TableCol Cursor FOR 
SELECT c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, c.CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.Columns c INNER JOIN
 INFORMATION_SCHEMA.Tables t ON c.TABLE_NAME = t.TABLE_NAME
WHERE t.Table_Catalog = @DatabaseName
 AND t.TABLE_TYPE = 'BASE TABLE'
ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION

-- Declare variables which will hold values from cursor rows
DECLARE @TableSchema varchar(100), @TableName varchar(100)
DECLARE @ColumnName varchar(100), @DataType varchar(30)
DECLARE @CharLength int

DECLARE @ColumnNameCleaned varchar(100)

-- Declare variables which will track what table we are
-- creating Stored Procs for
DECLARE @CurrentTable varchar(100)
DECLARE @FirstTable bit
DECLARE @FirstColumnName varchar(100)
DECLARE @FirstColumnDataType varchar(30)
DECLARE @ObjectName varchar(100) -- this is the tablename with the 
    -- specified tableprefix lopped off.
DECLARE @TablePrefixLength int

-- init vars
SET @CurrentTable = ''
SET @FirstTable = 1
SET @TablePrefixLength = Len(@TablePrefix)

-- Declare variables which will hold the queries we are building use unicode
-- data types so that can execute using sp_ExecuteSQL
DECLARE @LIST nvarchar(4000), @UPSERT nvarchar(4000)
DECLARE @SELECT nvarchar(4000), @INSERT nvarchar(4000), @INSERTVALUES varchar(4000)
DECLARE @UPDATE nvarchar(4000), @DELETE nvarchar(4000)


-- open the cursor
OPEN TableCol

-- get the first row of cursor into variables
FETCH NEXT FROM TableCol INTO @TableSchema, @TableName, @ColumnName, @DataType, @CharLength

-- loop through the rows of the cursor
WHILE @@FETCH_STATUS = 0 BEGIN

 SET @ColumnNameCleaned = Replace(@ColumnName, ' ', '')

 -- is this a new table?
 IF @TableName <> @CurrentTable BEGIN
  
  -- if is the end of the last table
  IF @CurrentTable <> '' BEGIN
   IF @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable BEGIN

    -- first add any syntax to end the statement
    
    -- _lst
    SET @LIST = @List + Char(13) + 'FROM ' + @CurrentTable + Char(13)
    SET @LIST = @LIST + Char(13) + Char(13) + 'SET NOCOUNT OFF' + Char(13) + Char(13)
    SET @LIST = @LIST + Char(13)
    
    -- _sel
    SET @SELECT = @SELECT + Char(13) + 'FROM ' + @CurrentTable + Char(13)
    SET @SELECT = @SELECT + 'WHERE [' + @FirstColumnName + '] = @' + Replace(@FirstColumnName, ' ', '') + Char(13)
    SET @SELECT = @SELECT + Char(13) + Char(13) + 'SET NOCOUNT OFF' + Char(13) + Char(13)
    SET @SELECT = @SELECT + Char(13)
 
 
    -- UPDATE (remove trailing comma and append the WHERE clause)
    SET @UPDATE = SUBSTRING(@UPDATE, 0, LEN(@UPDATE)- 1) + Char(13) + Char(9) + 'WHERE [' + @FirstColumnName + '] = @' + Replace(@FirstColumnName, ' ', '') + Char(13)
    
    -- INSERT
    SET @INSERT = SUBSTRING(@INSERT, 0, LEN(@INSERT) - 1) + Char(13) + Char(9) + ')' + Char(13)
    SET @INSERTVALUES = SUBSTRING(@INSERTVALUES, 0, LEN(@INSERTVALUES) -1) + Char(13) + Char(9) + ')'
    SET @INSERT = @INSERT + @INSERTVALUES
    
    -- _ups
    SET @UPSERT = @UPSERT + Char(13) + 'AS' + Char(13)
    SET @UPSERT = @UPSERT + 'SET NOCOUNT ON' + Char(13)
    IF @FirstColumnDataType IN ('int', 'bigint', 'smallint', 'tinyint', 'float', 'decimal')
    BEGIN
     SET @UPSERT = @UPSERT + 'IF @' + Replace(@FirstColumnName, ' ', '') + ' = 0 BEGIN' + Char(13)
    END ELSE BEGIN
     SET @UPSERT = @UPSERT + 'IF @' + Replace(@FirstColumnName, ' ', '') + ' = '''' BEGIN' + Char(13) 
    END
    SET @UPSERT = @UPSERT + ISNULL(@INSERT, '') + Char(13)
    SET @UPSERT = @UPSERT + Char(9) + 'SELECT SCOPE_IDENTITY() As InsertedID' + Char(13)
    SET @UPSERT = @UPSERT + 'END' + Char(13)
    SET @UPSERT = @UPSERT + 'ELSE BEGIN' + Char(13)
    SET @UPSERT = @UPSERT + ISNULL(@UPDATE, '') + Char(13)
    SET @UPSERT = @UPSERT + 'END' + Char(13) + Char(13)
    SET @UPSERT = @UPSERT + 'SET NOCOUNT OFF' + Char(13) + Char(13)
    SET @UPSERT = @UPSERT + Char(13)
 
    -- _del
    -- delete proc completed already
 
    -- --------------------------------------------------
    -- now either print the SP definitions or 
    -- execute the statements to create the procs
    -- --------------------------------------------------
    IF @PrintOrExecute <> 'Execute' BEGIN
     PRINT @LIST
     PRINT @SELECT
     PRINT @UPSERT
     PRINT @DELETE
    END ELSE BEGIN
     EXEC sp_Executesql @LIST
     EXEC sp_Executesql @SELECT
     EXEC sp_Executesql @UPSERT
     EXEC sp_Executesql @DELETE
    END
   END -- end @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable
  END
  
  -- update the value held in @CurrentTable
  SET @CurrentTable = @TableName
  SET @FirstColumnName = @ColumnName
  SET @FirstColumnDataType = @DataType
  
  IF @TablePrefixLength > 0 BEGIN
   IF SUBSTRING(@CurrentTable, 1, @TablePrefixLength) = @TablePrefix BEGIN
    --PRINT Char(13) + 'DEBUG: OBJ NAME: ' + RIGHT(@CurrentTable, LEN(@CurrentTable) - @TablePrefixLength)
    SET @ObjectName = RIGHT(@CurrentTable, LEN(@CurrentTable) - @TablePrefixLength)
   END ELSE BEGIN
    SET @ObjectName = @CurrentTable
   END
  END ELSE BEGIN
   SET @ObjectName = @CurrentTable
  END
  
  IF @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable BEGIN
  
   -- ----------------------------------------------------
   -- now start building the procedures for the next table
   -- ----------------------------------------------------
   
   -- _lst
   SET @LIST = 'CREATE PROC [dbo].[udp_' + @ObjectName + '_lst]' + Char(13)
   SET @LIST = @LIST + 'AS' + Char(13)
   SET @LIST = @LIST + 'SET NOCOUNT ON' + Char(13)
   IF @UseSelectWildcard = 1 BEGIN
    SET @LIST = @LIST + Char(13) + 'SELECT * '
   END 
   ELSE BEGIN
    SET @LIST = @LIST + Char(13) + 'SELECT [' + @ColumnName + ']'
   END
 
   -- _sel
   SET @SELECT = 'CREATE PROC [dbo].[udp_' + @ObjectName + '_sel]' + Char(13)
   SET @SELECT = @SELECT + Char(9) + '@' + @ColumnNameCleaned + ' ' + @DataType
   IF @DataType IN ('varchar', 'nvarchar', 'char', 'nchar') BEGIN
    SET @SELECT = @SELECT + '(' + CAST(@CharLength As varchar(10)) + ')'
   END
   SET @SELECT = @SELECT + Char(13) + 'AS' + Char(13)
   SET @SELECT = @SELECT + 'SET NOCOUNT ON' + Char(13)
   IF @UseSelectWildcard = 1 BEGIN
    SET @SELECT = @SELECT + Char(13) + 'SELECT * '
   END 
   ELSE BEGIN
    SET @SELECT = @SELECT + Char(13) + 'SELECT [' + @ColumnName + ']'
   END
 
   -- _ups
   SET @UPSERT = 'CREATE PROC [dbo].[udp_' + @ObjectName + '_ups]' + Char(13)
     SET @UPSERT = @UPSERT + Char(13) + Char(9) + '@' + @ColumnNameCleaned + ' ' + @DataType
   IF @DataType IN ('varchar', 'nvarchar', 'char', 'nchar') BEGIN
    SET @UPSERT = @UPSERT + '(' + CAST(@CharLength As Varchar(10)) + ')'
   END
 
   -- UPDATE
   SET @UPDATE = Char(9) + 'UPDATE ' + @TableName + ' SET ' + Char(13)
   
   -- INSERT -- don't add first column to insert if it is an
   --      integer (assume autonumber)
   SET @INSERT = Char(9) + 'INSERT INTO ' + @TableName + ' (' + Char(13)
   SET @INSERTVALUES = Char(9) + 'VALUES (' + Char(13)
   
   IF @FirstColumnDataType NOT IN ('int', 'bigint', 'smallint', 'tinyint')
   BEGIN
    SET @INSERT = @INSERT + Char(9) + Char(9) + '[' + @ColumnName + '],' + Char(13)
    SET @INSERTVALUES = @INSERTVALUES + Char(9) + Char(9) + '@' + @ColumnNameCleaned + ',' + Char(13)
   END
 
   -- _del
   SET @DELETE = 'CREATE PROC [dbo].[udp_' + @ObjectName + '_del]' + Char(13)
   SET @DELETE = @DELETE + Char(9) + '@' + @ColumnNameCleaned + ' ' + @DataType
   IF @DataType IN ('varchar', 'nvarchar', 'char', 'nchar') BEGIN
    SET @DELETE = @DELETE + '(' + CAST(@CharLength As Varchar(10)) + ')'
   END
   SET @DELETE = @DELETE + Char(13) + 'AS' + Char(13)
   SET @DELETE = @DELETE + 'SET NOCOUNT ON' + Char(13) + Char(13)
   SET @DELETE = @DELETE + 'DELETE FROM ' + @TableName + Char(13)
   SET @DELETE = @DELETE + 'WHERE [' + @ColumnName + '] = @' + @ColumnNameCleaned + Char(13)
   SET @DELETE = @DELETE + Char(13) + 'SET NOCOUNT OFF' + Char(13)
   SET @DELETE = @DELETE + Char(13) 

  END -- end @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable
 END
 ELSE BEGIN
  IF @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable BEGIN
  
   -- is the same table as the last row of the cursor
   -- just append the column
   
   -- _lst
   IF @UseSelectWildCard = 0 BEGIN
    SET @LIST = @LIST + ', ' + Char(13) + Char(9) + '[' + @ColumnName + ']'
   END
 
   -- _sel
   IF @UseSelectWildCard = 0 BEGIN
    SET @SELECT = @SELECT + ', ' + Char(13) + Char(9) + '[' + @ColumnName + ']'
   END
 
   -- _ups
   SET @UPSERT = @UPSERT + ',' + Char(13) + Char(9) + '@' + @ColumnNameCleaned + ' ' + @DataType
   IF @DataType IN ('varchar', 'nvarchar', 'char', 'nchar') BEGIN
    SET @UPSERT = @UPSERT + '(' + CAST(@CharLength As varchar(10)) + ')'
   END
 
   -- UPDATE
   SET @UPDATE = @UPDATE + Char(9) + Char(9) + '[' + @ColumnName + '] = @' + @ColumnNameCleaned + ',' + Char(13)
 
   -- INSERT
   SET @INSERT = @INSERT + Char(9) + Char(9) + '[' + @ColumnName + '],' + Char(13)
   SET @INSERTVALUES = @INSERTVALUES + Char(9) + Char(9) + '@' + @ColumnNameCleaned + ',' + Char(13)
 
   -- _del
   -- delete proc completed already
  END -- end @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable'
 END

 -- fetch next row of cursor into variables
 FETCH NEXT FROM TableCol INTO @TableSchema, @TableName, @ColumnName, @DataType, @CharLength
END

-- ----------------
-- clean up cursor
-- ----------------
CLOSE TableCol
DEALLOCATE TableCol

-- ------------------------------------------------
-- repeat the block of code from within the cursor
-- So that the last table has its procs completed
-- and printed / executed
-- ------------------------------------------------

-- if is the end of the last table
IF @CurrentTable <> '' BEGIN
 IF @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable BEGIN

  -- first add any syntax to end the statement
  
  -- _lst
  SET @LIST = @List + Char(13) + 'FROM ' + @CurrentTable + Char(13)
  SET @LIST = @LIST + Char(13) + Char(13) + 'SET NOCOUNT OFF' + Char(13)
  SET @LIST = @LIST + Char(13)
  
  -- _sel
  SET @SELECT = @SELECT + Char(13) + 'FROM ' + @CurrentTable + Char(13)
  SET @SELECT = @SELECT + 'WHERE [' + @FirstColumnName + '] = @' + Replace(@FirstColumnName, ' ', '') + Char(13)
  SET @SELECT = @SELECT + Char(13) + Char(13) + 'SET NOCOUNT OFF' + Char(13)
  SET @SELECT = @SELECT + Char(13)


  -- UPDATE (remove trailing comma and append the WHERE clause)
  SET @UPDATE = SUBSTRING(@UPDATE, 0, LEN(@UPDATE)- 1) + Char(13) + Char(9) + 'WHERE [' + @FirstColumnName + '] = @' + Replace(@FirstColumnName, ' ', '') + Char(13)
  
  -- INSERT
  SET @INSERT = SUBSTRING(@INSERT, 0, LEN(@INSERT) - 1) + Char(13) + Char(9) + ')' + Char(13)
  SET @INSERTVALUES = SUBSTRING(@INSERTVALUES, 0, LEN(@INSERTVALUES) -1) + Char(13) + Char(9) + ')'
  SET @INSERT = @INSERT + @INSERTVALUES
  
  -- _ups
  SET @UPSERT = @UPSERT + Char(13) + 'AS' + Char(13)
  SET @UPSERT = @UPSERT + 'SET NOCOUNT ON' + Char(13)
  IF @FirstColumnDataType IN ('int', 'bigint', 'smallint', 'tinyint', 'float', 'decimal')
  BEGIN
   SET @UPSERT = @UPSERT + 'IF @' + Replace(@FirstColumnName, ' ', '') + ' = 0 BEGIN' + Char(13)
  END ELSE BEGIN
   SET @UPSERT = @UPSERT + 'IF @' + Replace(@FirstColumnName, ' ', '') + ' = '''' BEGIN' + Char(13) 
  END
  SET @UPSERT = @UPSERT + ISNULL(@INSERT, '') + Char(13)
  SET @UPSERT = @UPSERT + Char(9) + 'SELECT SCOPE_IDENTITY() As InsertedID' + Char(13)
  SET @UPSERT = @UPSERT + 'END' + Char(13)
  SET @UPSERT = @UPSERT + 'ELSE BEGIN' + Char(13)
  SET @UPSERT = @UPSERT + ISNULL(@UPDATE, '') + Char(13)
  SET @UPSERT = @UPSERT + 'END' + Char(13) + Char(13)
  SET @UPSERT = @UPSERT + 'SET NOCOUNT OFF' + Char(13)
  SET @UPSERT = @UPSERT + Char(13)

  -- _del
  -- delete proc completed already

  -- --------------------------------------------------
  -- now either print the SP definitions or 
  -- execute the statements to create the procs
  -- --------------------------------------------------
  IF @PrintOrExecute <> 'Execute' BEGIN
   PRINT @LIST
   PRINT @SELECT
   PRINT @UPSERT
   PRINT @DELETE
  END ELSE BEGIN
   EXEC sp_Executesql @LIST
   EXEC sp_Executesql @SELECT
   EXEC sp_Executesql @UPSERT
   EXEC sp_Executesql @DELETE
  END
 END -- end @GenerateProcsFor = '' OR @GenerateProcsFor = @CurrentTable
END



 

Friday, June 29, 2012

Converting AD UTC time to DateTime

Convert local time to UTC

TimeZoneInfo.ConvertTimeToUtc(DateTime.Now.AddDays(15)).ToFileTimeUtc()
 
Result
129867569963464980

Convert UTC time to Local time

TimeZoneInfo.ConvertTimeToUtc(DateTime.Now)

Result:
{6/29/2012 4:29:09 PM}
    Date: {6/29/2012 12:00:00 AM}
    Day: 29
    DayOfWeek: Friday
    DayOfYear: 181
    Hour: 16
    Kind: Utc
    Millisecond: 954
    Minute: 29
    Month: 6
    Second: 9
    Ticks: 634765841499549823
    TimeOfDay: {16:29:09.9549823}
    Year: 2012

Wednesday, June 20, 2012

Deploy ASP.NET MVC App on Windows XP (IIS 5.1)

Thanks to
http://itscommonsensestupid.blogspot.com/2008/11/deploy-aspnet-mvc-app-on-windows-xp-iis.html

Deploying ASP.NET MVC application on Windows XP is definitely not easy; there are a lot of settings that need to be tuned. Not only that the application must be changed as well.

The first thing to do is to add the relevant mapping to the Application Configuration of the MVC application. To do this, one must go to Start-> Control Panel-> Administrative Tools-> Internet Information Services. Then, one should click on  the Internet Information Services icon, right-click on the MVC application, and select Properties. On the Virtual Directory Tab, choose Configuration, as shown below:



After clicking on the Configuration panel, choose the Executable to be the aspnet_isapi.ll, and the extension to be ".*", make sure that the "check that file exists" is unchecked. If the OK button is disabled, you must click on the textbox for the Executable, and only then the OK button is enabled.

Click OK all the way to close the Administrative tools.

One must also add a route to the RegisterRoutes static function. Here is the syntax that is compatible with ASP.NET MVC Beta
?
1
2
3
4
5
6
7
routes.Add(new Route
(
"{controller}.mvc/{action}/{id}",
new RouteValueDictionary(new { action = "Index", id = (string)null }),
new MvcRouteHandler()
));

Look through your MVC application, make sure that you don't have any hardcode reference to controller class or action method. If you are using

?
1

"/Home/uploadfiles"></form>


Make sure you change it to

?
1
"<%=Url.Action(new{controller=" home",="" })%>"=""></form>


If you have

?
1