Friday, September 20, 2013

How to force a page to use SSL (HTTPS) in ASP.NET (VB.NET/C#)

Force a page to use HTTPS instead of HTTP

The following sample code will force a page to use ssl woth asp.net . Please  add code in to Page_Load event

C#  Code

//this is the current url 
System.Uri currentUrl = System.Web.HttpContext.Current.Request.Url; 
//don't redirect if this is localhost 
 if (!currentUrl.IsLoopback)
 { 
  if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase))
  { 
    //build the secure uri 
    System.UriBuilder secureUrlBuilder = new UriBuilder(currentUrl); 
    secureUrlBuilder.Scheme = Uri.UriSchemeHttps; 
    //use the default port. 
    secureUrlBuilder.Port = -1; 
    //redirect and end the response. 
    System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString()); 
   } 
  } 

VB.NET Code

'this is the current url 
Dim currentUrl As System.Uri = System.Web.HttpContext.Current.Request.Url
'don't redirect if this is localhost
If Not currentUrl.IsLoopback Then
 If Not currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase) Then
  'build the secure uri
  Dim secureUrlBuilder As System.UriBuilder = New UriBuilder(currentUrl)
  secureUrlBuilder.Scheme = Uri.UriSchemeHttps
  'use the default port. 
  secureUrlBuilder.Port = -1
  'redirect and end the response.
  System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString())
 End If
End If

Tuesday, September 17, 2013

Forcefully Rename a SQL Server Database






When SQL Server management studio give below error we have forcefully rename  for our database.


Management Studio is telling you that there are connections that it doesn’t want to mess with.
Or when using sp_renamedb from a query window, you might see this similar error:
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
If you really really want to rename the database and don’t care at all about in-flight transactions, then use this script/template I recently came up with:
A Forceful rename  Script:

ALTER DATABASE [old_name]
 
 SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [old_name]
 
 MODIFY NAME = [new_name]
GO
ALTER DATABASE [new_name]
 SET MULTI_USER
GO