Wednesday 12 April 2017

AngularJS Routing - Removing Hash Tag

While implementing routing in AngularJS, I observed that URLs formed while routing contained “#!” - http://localhost:53432/#!/quote/indexTo clean the URLs html5Mode attribute of $locationService should be set in the module’s config function as follows:

var app = angular.module('app', ['ui.router']);

app.config(function ($locationProvider, $stateProvider, $urlRouterProvider) {
   $locationProvider.hashPrefix('');
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
   $stateProvider
        .state('group', {
            url: '/',
            templateUrl: '/quote/GroupInfo',
            controller: 'quoteController'
        })
        // nested states
        // each of these sections will have their own view
        .state('census', {
            url: '/',
            templateUrl: '/quote/CensusDetail',
            controller: 'censusController' })       
     }


In the above code snippet ui-router is implemented for routing. The highlighted code is all required to get rid of hash tag from the URLs.

Friday 9 March 2012

Table Valued Parameter

There was  a situation where we wanted to use table variable in dynamic sql join statements. However attempting to write below code deesnt works:

declare  @sql as nvarchar(1000)

declare  @tablevar as table(name nvarchar(10),age int)

insert into @tablevar values ('johny',66)

exec sp_executesql N'select * from @tablevar', N'@tablevar table', @tablevar = @tablevar

Above statement will throw following Error Message:
--Msg 156, Level 15, State 1, Line 1
--Incorrect syntax near the keyword 'table'.
--Msg 1087, Level 15, State 2, Line 1
--Must declare the table variable "@tablevar"

In SQL Server 2008 there is a new feature of user defined table types. We can create a table type of the the desired schema and then declare a variable of that type. Variable created of type user defined table types are called table valued parameters (TVP). TVP parameters could be passed to stored procedures and functions. These variable can also be used in dynamic sql statements.

create type tabletype as table(name nvarchar(10),age int)

declare @tablevariable as tabletype

insert into @tablevariable values ('poonam',66)

exec sp_executesql N'select * from @tablevariable',N'@tablevariable tabletype
READONLY',@tablevariable = @tablevariable

Please note the usage of "READONLY" option which is compulosry while passing a table valued parameter as a parameter.

Reference URL: http://msdn.microsoft.com/en-us/library/bb510489%28v=sql.100%29.aspx

Length for data type nvarchar(max) in ADO.Net parameter collection

I had a SQL Server based stored procedure with an output parameter of type nvarchar(max). Issue we faced was what length of this variable length parameter should be set in the ADO.NET parmater collection.
Answer is length for such data types could be set to -1 in the parameters collection as follows:

oDatabase.AddOutParameter(oDbCommand, "@RegionNotDeleted", DbType.String,-1);

Protocol Violation Exception raised while using PHP webserivce in .net project

In one of our asp.net 3.5 project we were using PHP services. Whenever a request to PHP service was made we were alternatively getting following exception:
"System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine"

After googling and refering to related sites we tried setting useUnsafeHeaderParsing in web.config to true. But this didnt solve this issue.

<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true" />
</settings>
</system.net>


Reason for the above exception was that the request headers were  changing in the consequtive requests and this was not validated by HTTP 1.1 protocol. To resolve this we created a custom class that inherits the proxy class (auto-generated by Visula Studio). In this class WebRequest method was overridden and the HTTP protocol version was changed to 1.0 and the property KeepAlive was set to false. In HTTP/1.1 a keep-alive-mechanism was introduced, where a connection could be reused for more than one request. By setting KeepAlive to false a new connection was created with every request.

public class checkSiteUserEmailBindingCustom: wsPHPWebsite.checkSiteUserEmailBinding
        {
            private static PropertyInfo requestPropertyInfo = null;

            public checkSiteUserEmailBindingCustom() { }

            protected override System.Net.WebRequest GetWebRequest(Uri uri)
            {
                // Retrieve underlying web request
                System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = System.Net.HttpVersion.Version10;

                return webRequest;
               
            }
        }



If we drill down the web services classes all the classes directly/indirectly belongs to System.Web.Services.Protocol namespace. Base class for all XML web service client proxies is WebClientProtocal class. This is an abstract class and contains a virtual method GetWebRequest. 
WebClientProtocal class is inherited by HTTPWebClientProtocol. This is an abtract base class for all XML Web service client proxies that use the HTTP transport protocol. This class is further inherited by  System.Web.Services.Protocols.SoapHttpClientProtocol. This is the  class which proxies derive while using SOAP protocol. This class Overrides HttpWebClientProtocol.GetWebRequest(Uri).


Reference URLs:
http://forums.asp.net/t/1223296.aspx/1
http://weblogs.asp.net/jan/archive/2004/01/28/63771.aspx
http://msdn.microsoft.com/en-us/library/system.web.services.protocols.webclientprotocol.aspx
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

Thursday 2 February 2012

How to rename a SQL Server Database

To rename a database we need need to rename both the logical and physical database files. The logical file name is the name used to refer to the physical file in all Transact-SQL statements. Following steps could be followed to rename a SQL Server setps:

I. Below query will retrieve currently set Logical and Physical File name:   

 SELECT * FROM sys.master_files AS mf

 SELECT DB_NAME(database_id) AS DatabaseName, name AS LogicalFileName, physical_name AS PhysicalFileName FROM sys.master_files AS mf

In the above query sys.master_files is a view available in master database.

II. Rename the logical file name. Database must be online while changing logical file name. Query syntax is as follows:
ALTER DATABASE <DBName> MODIFY FILE (NAME = '<logicalFile>', NEWNAME = '<newlogicalfilename>') 

 --For Example

ALTER DATABASE Test MODIFY FILE (NAME='Test', NEWNAME='NewTest')

 ALTER DATABASE Test MODIFY FILE (NAME='Test_log', NEWNAME='NewTest_Log')

III. Rename the physical file name. Database must be online while changing physical file name. Query syntax is as follows:
--ALTER DATABASE <DBName> MODIFY FILE (NAME = '<logicalFile>', FILENAME = '<physicalname>')

 --For Example
ALTER DATABASE Test MODIFY  FILE (NAME='NewTest', FILENAME='C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\NewTest.mdf')

 ALTER DATABASE Test MODIFY  FILE (NAME='NewTest_Log', FILENAME='D:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\NewTest_1.ldf')

IV. Take the database offline

V. Rename the physical files manually

VI. Reattach the database.

Monday 8 August 2011

How to add a custom icon in the Ribbon for outlook custom form region

Icon for custom form region in the Outlook ribbon can be set programmatically as follows:
  1. Firstly in Solution Explorer, double click on the .resx file of the form region.
  2. Click on Add Resource->Add Existing File option and select the desired icon file through the Browse window. This will add the image file in a separate Resources folder.
  1. Navigate to the InitializeManifest() function in the code window of form region file and add the following line:

In the above code, “PageIcon” is the name given to the image while adding it to the .resx file.


Friday 5 August 2011

Deployment of Outlook AddIn containing Custom Form Region

Steps to create Installer for Outlook Add In:
Setup Project Settings
1)      Add a Setup Project in the solution.
2)      Right click on setup project. Select View->File System. In File System tab right click on “Application Folder” and select Add-> Project Output. In “Add Project Output Group” select Primary Output.  Selecting primary output will add the required dlls in “Detected Dependencies” folder.
3)      Right-click the Application folder and select Add-> File. Browse to your application folder, and then bin\Release. Select the .dll.manifest, and .vsto files that reside there.
4)      Right click the Application Folder and select Properties Window option. In the Properties window, under DefaultLocation, change ProgramFilesFolder][Manufacturer]\[ProductName] to [AppDataFolder][Manufacturer]\[ProductName].

We’ve now effectively told the setup project to install all our application goodness to the user’s AppData folder (Normally C:\Users\[username]\AppData in Windows 7, for example). We are doing this as this is a safe, sandboxed location that requires a minimum of privileges to install to.
5)      Optionally in the Properties window for the Setup project change values for Manufacturer, Product Name and Version.  Values specified here will be replaced by the corresponding keys in step 4.

Registry Key Settings
To get the add-in working, we will need to add registry keys. When you are developing the solution in Visual Studio, this happens automatically in the background, so it can be a surprise that you will need to do it explicitly! The keys we are creating will be saved to: HKCU\Software\Microsoft\Office\Outlook\Addins.
Open the Registry Panel in the Setup Project and create keys as follows:
1)      Expand the HKEY_CURRENT_USER key, then Software, and rename [Manufacturer] to Microsoft (because we are adding this key to the Microsoft Office section of the Registry).
2)      Create a tree of key values underneath Microsoft: Office\Outlook\Addins
3)      Finally, create a key for your add-in. Give it a unique name, such that it is different from the add-in project name (so you can identify keys created by the installer, and keys created by Visual Studio).
4)      In that key, set the following values:
§  String – “Description”
§  String – “FriendlyName”
§  String – “Manifest” – set this to “[TARGETDIR]MyOutlookAddin.vsto|vstolocal”
[TARGETDIR] is a macro  and specifies the root destination directory for the installation.
§  DWORD – “LoadBehavior” – set this to “3”
(LoadBehavior and not LoadBehaviour!)
      
  1. Select the new key (the one you just set values for) and in the Properties window, set DeleteAtUninstall to be true (important: do not set it for any keys higher up in the tree!)  
  2. If there is a custom form region in the add in we need to specify separate registry keys as follows:
 
Under the IPM.Appointment key, string value of format ProgID.RegionName should be added. Value for this string should match with the new key added in the “AddIns” folder in 4th step.





Reference URL: