Thursday, December 23, 2010

Display Success and Error messages in SharePoint 2010

One of the common functions which is required from any developer; is to display a Success and Error messages to the user, and it’s always better to use the same style of the main application.
In SharePoint: SPUtility class contains two methods to achieve this.
Success Message:

SPUtility.TransferToSuccessPage: It is used to display a Success Message, and it has two overloads.
SPUtility.TransferToSuccessPage Method (String message)

Example:
SPUtility.TransferToSuccessPage("Welcome, You are in Success Page");

Figure (1) shows the output of this code. In this case the “Ok” button will redirect to the current SharePoint Site home page.


Figure(1) Success Page

SPUtility.TransferToSuccessPage Method (String message, String nextUrl, String linkText, String linkUrl)

Example:
SPUtility.TransferToSuccessPage("Welcome, You are in Success Page", "/SitePages/Continue.aspx", "", "");
In this case the “Ok” button will redirect to “"/SitePages/Continue.aspx” page.

Error Message:
The same is applied to SPUtility.TransferToErrorPage: it is used to display error message.

SPUtility.TransferToErrorPage(String message)
Example:
SPUtility.TransferToErrorPage("Sorry it seems that you did a mistake. This is the error page"); 


Figure (3) Error Page

Wednesday, December 15, 2010

SQL 2008 DateTime dataTypes

SQL 2008 introduced new types of DateTime data type (Time, Date, DateTime, DateTime2, SmallDateTime and DateTimeOffset).
The following query example explains the difference between each one of them.
SELECT 
CAST('2007-05-08 12:35:29. 1234567 +12:15' AS time(7)) AS 'Time' 
,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS date) AS 'Date' 
,CAST('2007-05-08 12:35:29.123' AS smalldatetime) AS 'Smalldatetime' 
,CAST('2007-05-08 12:35:29.123' AS datetime) AS 'Datetime' 
,CAST('2007-05-08 12:35:29.1234567+12:15' AS datetime2(7)) AS  'Datetime2'
,CAST('2007-05-08 12:35:29.1234567 +12:15' AS datetimeoffset(7)) AS   'Datetimeoffset'
,CAST('2007-05-08 12:35:29.1234567+12:15' AS datetimeoffset(7)) AS 'Datetimeoffset IS08601';

Here is the result set.


Data type

Output

Time

12:35:29. 1234567

Date

2007-05-08

Smalldatetime

2007-05-08 12:35:00

Datetime

2007-05-08 12:35:29.123

datetime2

2007-05-08 12:35:29. 1234567

Datetimeoffset

2007-05-08 12:35:29.1234567 +12:15



Reference:
MSDN

Sunday, December 12, 2010

Filter list of SQL objects in SQL Management Studio

As I believe that to increase your productivity; you have to master using the tools which you are using. SQL Management Studio is one of the tools which is used by any developer during the day.
If you are working in a large project and you have a large number of objects (Tables, Stored Procedures, Functions or Views) in the application database, and you want to edit a certain one. Instead of scrolling up and down and reading the names, you can easily filter the list using the Filter context menu.
The following screen-shots present how you can find a table by name and Creation date.






Friday, December 10, 2010

How to repair a SQL Server 2005/ 2008 Suspect database

Some times after Hardware or Power failure, when you open the SQL Management studio; you find that some of your databases are not accessible, and they are marked as “Suspect”
To fix this you can run the following query (replace “MyDataBaseName” with the database name):
ALTER DATABASE MyDataBaseName SET EMERGENCY
DBCC checkdb ('MyDataBaseName')
ALTER DATABASE MyDataBaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('MyDataBaseName', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE MyDataBaseName SET MULTI_USER

Source:
http://gargmanoj.wordpress.com/2008/07/17/how-to-repair-a-sql-server-2005-suspect-database/

Get list of Months Names in the current Thread language

One of the functions which are required in the localized applications is to get the list of months names in the current culture language.
To do this, some people do this using resources and add it manually to the control (DropDownList, ListBox, etc...), but you can accomplish this by the following single line of code which returns the months names translated to the current Thread culture.
Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames.Take(12);

Demo:
If the current thread culture is “fr-FR” it returns:

If the current thread culture is “de-DE” it returns:


If the current thread culture is “ar-EG” it returns:

I hope that this saves some of your valuable time.

Wednesday, December 8, 2010

Auto Sign out SharePoint

One of the customer requirements was "If the user is not the using the SharePoint portal in the browser for more than 10 minutes; The session should be ended for security reasons".
To accomplish this  I tried to enable the Session State for the SharePoint site "Which is disabled by default" and changed the Session time out, but it didn't work. So after a lot of research and googling, I decided to do it by JavaScript and with the help of the magic of JQuery:
I added the following java script block in the master page and it works fine , and the customer is satisfied.

<script src="http://code.jquery.com/jquery-1.4.4.js">
 
<script> $(document).ready(function(){  window.setTimeout(  
function(){   ie 
= (document.all) ? 
true : false;
if(ie){ document.execCommand('ClearAuthenticationCache',false);   }
var pathname = window.location.pathname;
window.location='/_layouts/AccessDenied.aspx?loginasanotheruser=true&amp;Source='  
pathname ;  }, 600000); }); 
</script>


I hope it helps you.

Sunday, December 5, 2010

Insert multiple rows values in one Insert statement (SQL 2008)

One of the nice tricks of insert statement; that it is possible to insert multiple rows values in one insert statement; you don’t need to insert each row in a separate statement.

CREATE TABLE DemoTable
    (
      ItemID INT IDENTITY
                 PRIMARY KEY,
      FirstName VARCHAR(50),
      LastName VARCHAR(50),
      CountryCode VARCHAR(4),
    )

INSERT  [DemoTable]
        (
          [FirstName],
          [LastName],
          [CountryCode]
        )
VALUES  ('Mahmoud','Hakeem','EG'),
        ('Michel','Durian','DE'),       
        ('Thomaz','Miracle','Fr')