Skip to main content


Showing posts from November, 2012

ASP.Net: Workaround with FormsAuthentication.RedirectToLoginPage Not Working

This method is used to Redirects the browser to the login URL. But sometime this function not works as expected and user is not sent to login page.

As defined in MSDN documentation “Unlike the HttpResponse.Redirect method, this method does not end the request by calling HttpResponse.End. This means that code that follows the RedirectToLoginPage method call will run.”
If FormsAuthentication.RedirectToLoginPage() method not working as expected that means code in the Page life cycle after your call to RedirectToLoginPage is running, and throwing a NullReferenceException.
Solution: Write Response.End() after  FormsAuthentication.RedirectToLoginPage(), and it will work fine then.
Note: If you want to know details about what the actual response contains when the method is called and why there is Null reference exception, you can use a trace tool such as Fiddler to see.

SQL Server 2012: New Features for Developers

Just few days back I have started to work with SQL Server 2012, here I am writing about some of the new features of SQL Server 2012.
SQL Server 2012 does not come with earth-shaking changes but comes with performance improvements, feature improvements and some new features. SQL Server 2012 also brings many new features to the T-SQL language, some of them are listed in this post.
SQL Server Management Studio 2012: SSMS 2012 packs a number of powerful productivity benefits that developers who use SSMS for development tasks will end up loving.  The most obvious change is to Management Studio is that it’s built on Visual Studio 2010(and it has same theme as in figure.) and has the same engine as Visual Studio which means you get a nice WPF experience, better font rendering and CTRL-scroll quick zoom-in/zoom-out.
Being built on Visual Studio 2010 also means that SSMS 2012 picks up Visual Studio 2010's vastly improved multi-monitor support. SSMS 2012 inherits Visual Studio's Clipboa…

SQL Server 2012: New feature Sequence Object

A sequence is a user-defined schema bound object that generates a sequence of numeric values according to the specification with which the sequence was created. The sequence of numeric values is generated in an ascending or descending order at a defined interval and can be configured to restart (cycle) when exhausted. Sequence supports TINYINT, INT, SMALLINT, BIGINT, DECIMAL and NUMERIC data types.
Sequences, unlike identity columns, are not associated with specific tables. The relationship between sequences and tables is controlled by the application. User applications can reference a sequence object and coordinate the values across multiple rows and tables.
Create Sequence in following steps:
1.Under Selected DB -> Go to Programmability -> then right click on Sequences and Select New
2.You can check supported Data types on New Sequence Screen.
3.Create New Sequence by providing your values.
4.And you are done, you can also create the same Sequence through Query as:

SQL Server 2012: New Logical Functions IIF and CHOOSE

There are the two new logical functions in SQL Server 2012: 1.IIF() Function 2.Choose() Function
IIF Logical Function: The IIF function, which is available in SQL Server 2012, returns one of the two values depending upon whether the Boolean expression evaluates to either True or False.
Example: DECLARE@Value1INT= 10 DECLARE@Value2INT= 20 SELECTIIF(@Value1>@Value2,'TRUE','FALSE')ASIIFResult GO
And result will be:
CHOOSE Logical Function: The CHOOSE function is used to return the value out of a list based on specified index number. You can think of it as an array kind of thing. The Index number here starts from 1.
Some points about Choose function: ·If an index value exceeds the bound of the array it returns NULL ·If the index value is negative then that exceeds the bounds of the array therefore it returns NULL ·If the provided index value has a float data type other than int, then the value is implicitly converted to an integer
Example: In the example I’ll show results with a…

SQL Server 2012: FORMAT Function

Using CLR under the covers, we will now finally have relative parity with the .format() function we are used to using in .NET languages like C#. This means we needn’t to memorize codes like 101 and 103 anymore (to convert datetime values to localized presentation formats), or we can easily format the currency values etc.
Examples: Format date with Format Function as:
DECLARE@dDATE='2012-11-19'; SELECTFORMAT(@d,N'yyyy-MM'), FORMAT(@d,N'yyyy-MM-dd'), FORMAT(@d,N'yyyy-MMM'), FORMAT(@d,N'dddd, MMM dd, yyyy'), FORMAT(@d,N'dddd, MMMM dd, yyyy')      
And the result is:
Format currency values as:
DECLARE@mDECIMAL(12,2)= 322311.43;
-- 'en-us' is culuture. SELECTDisplay=FORMAT(@m,'C','en-us')
And here is result:

SQL Server 2012: TRY_CONVERT Function

TRY_CONVERT(): TRY_CONVERT is somewhat same as we have TryParse in C#; it takes the value passed to it and tries to convert it to the specifieddata type. If the cast succeeds,TRY_CONVERTreturns the value as the specified data type; if an error occurs, null is returned.
For example: SETDATEFORMATdmy; SELECTTRY_CONVERT(datetime2,'11/19/2010')ASResult; GO
And result will be Null, as it considers 19 as month, now try as follows:
SETDATEFORMATmdy; SELECTTRY_CONVERT(datetime2,'11/19/2010')ASResult; GO
Result: 2010-11-19 00:00:00.0000000
However if you request a conversion that is explicitly not permitted, then TRY_CONVERT fails with an error. The following example demonstrates that TRY_CONVERT returns an error when the cast is explicitly not permitted. SELECTTRY_CONVERT(xml, 4)ASResult; GO
This conversion throw error as: Explicit conversion from data type int to xml is not allowed.