Monday, May 30, 2011

Configuring the Transact-SQL Debugger

source : http://msdn.microsoft.com/en-us/library/cc646024.aspx

You must configure Windows Firewall exceptions to enable Transact-SQL debugging when connected to an instance of the Database Engine that is running on a different computer than the Database Engine Query Editor.
The Transact-SQL debugger includes both server-side and client-side components. The server-side debugger components are installed with each instance of the SQL Server 2008 Database Engine. The client-side components are installed when you install the SQL Server 2008 client-side tools.
There are no configuration requirements to run the Transact-SQL debugger when SQL Server Management Studio is running on the same computer as the instance of the SQL Server Database Engine. However, to run the Transact-SQL debugger when SQL Server Management Studio is running on a different computer from the instance of the Database Engine, you must enable program and port exceptions on both computers.
Caution note Caution
Enabling exceptions in the Windows Firewall may expose your computer to security threats that the firewall is designed to block. Enabling exceptions for remote debugging unblocks DCOM (TCP port 135) and IPSEC (UDP port 4500 and UDP port 500). It also allows the debugger to open additional ports.
On the computer that is running the instance of the Database Engine, use the Windows Firewall Control Panel application to specify the following information:
  • Add TCP port 135 to the exceptions list.
  • Add the program sqlservr.exe to the exceptions list. By default, sqlservr.exe is installed in C:\Program Files\Microsoft SQL Server\MSSQL10.InstanceName\MSSQL\Binn, where InstanceName is MSSQLSERVER for the default instance, and the instance name for any named instance.
  • If the domain policy requires network communications to be done through IPsec, you must also add UDP port 4500 and UDP port 500 to the exception list.
On the computer that is running SQL Server Management Studio, the first time you open a debugging session, SQL Server Management Studio opens a dialog box with three options for configuring the remote debugging access.
Cancel remote debugging
Cancels the attempt to start debugging. The security settings of your computer remain unchanged.
Unblock remote debugging from computers on the local network (subnet)
Enables remote debugging of computers on your local subnet. This setting may open vulnerabilities to computers on your local subnet, but the firewall continues to block information coming from outside the subnet.
Unblock remote debugging from any computer
Enables remote debugging of computers anywhere on the network. This setting is the least secure.
Optionally, you can manually configure the program and port exceptions by using Windows Firewall to specify the following information:
  • Add TCP port 135 to the exceptions list.
  • Add program ssms.exe (SQL Server Management Studio) to the exceptions list. By default, ssms.exe is installed in C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE.
The requirements to start the Transact-SQL debugger are as follows:
  • SQL Server Management Studio must be running under a Windows account that is a member of the sysadmin fixed server roll.
  • The Database Engine Query Editor window must be connected by using either a Windows Authentication or SQL Server Authentication login that is a member of the sysadmin fixed server role.
  • The Database Engine Query Editor window must be connected to an instance of the SQL Server 2008 Database Engine or later. You cannot run the debugger when the Query Editor window is connected to an instance that is in single-user mode.

Tuesday, May 17, 2011

[ASP.NET] RequiredFieldValidator + OnClientClick issue

source : http://alvinzc.blogspot.com/2006/10/aspnet-requiredfieldvalidator.html



OnClientClick property was introduced in ASP.NET 2.0 to eliminate the need of developers to add the client-side "onclick" to a control programmatically. For instance,

JS script

function ConfirmMe()
{
   return confirm("Do you want to proceed?");
}


ASPX





Well, that is pretty straightforward. BUT, it goes weird when you have a validator control (eg. RequiredFieldValidator) that is used to validate the "txtName" textbox server control. For instance,









Whenever you press the button with no textbox value, the client-side confirmation dialog will be invoked first before the validator message is able to show up. This isn't what we expected it to behave. I tried several ways to overcome this problem, including using CLIENT CALLBACK, disabling the CauseValidation, but it failed. Finally, I was able to find a solution by adding JUST ONE line in the JS script.

function ConfirmMe()
{
   if(Page_ClientValidate())
      return confirm('Do you want to proceed?');

   return false;
}

Monday, May 16, 2011

Resolve Url combined with Server.UrlEncode


1) Resolve Url

Control.ResolveUrl Method

.NET Framework 4

Converts a URL into one that is usable on the requesting client.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)


public string ResolveUrl(
 string relativeUrl
)

Parameters

relativeUrl
Type: System.String The URL associated with the TemplateSourceDirectory property. 

Return Value

Type: System.String
The converted URL.

ExceptionCondition
ArgumentNullExceptionOccurs if the relativeUrl parameter contains null.

If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL.
For example, consider the following scenario:
  • A client has requested an ASP.NET page that contains a user control that has an image associated with it.
  • The ASP.NET page is located at /Store/page1.aspx.
  • The user control is located at /Store/UserControls/UC1.ascx.
  • The image file is located at /UserControls/Images/Image1.jpg.
If the user control passes the relative path to the image (that is, /Store/UserControls/Images/Image1.jpg) to the ResolveUrl method, the method will return the value /Images/Image1.jpg.
This method uses the TemplateSourceDirectory property to resolve to the absolute URL. The returned URL is for client use.
For more information on resource paths in a Web site, see ASP.NET Web Project Paths.
Note Note
For mobile Web pages only, if your application relies on cookieless sessions or might receive requests from mobile browsers that require cookieless sessions, using a tilde ("~") in a path can result in inadvertently creating a new session and potentially losing session data. To set a property with a path such as "~/path", resolve the path by calling the ResolveUrlwith an argument such as "~/path" before assigning it to the property.

The following example creates an Image Web server control object and uses the ResolveUrl method to set the path to the image, which is stored by the ImageUrl property.

public class MyResolveUrl:Control
{
   private string _ImageUrl;     
   public string ImageUrl
   {
      get
      {
         return _ImageUrl;
      }
      set
      {
         _ImageUrl = value;
      }
   }
   protected override void Render(HtmlTextWriter output)
   {           
      Image myImage = new Image();
      // Resolve Url.
      myImage.ImageUrl = ResolveUrl(this.ImageUrl);
      myImage.RenderControl(output);         
   }     
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Community Content Add






2) Assuming next situation
web site : "my web site"
The resolved url for "my web site\directory\file.css" will be

ResolveUrl("~/my web site/directory/file.css") will be 
"my web site/directory/file.css"

But this will not be found because it contains spaces(" ")
The solution will be

Server.UrlEncode(ResolveUrl("~/my web site/directory/file.css"))


Annotations FAQ

Thursday, May 5, 2011

String Format for DateTime [C#]

This example shows how to format DateTime using String.Format method. All formatting can be done also using DateTime.ToString method.

Custom DateTime Formatting

There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed),t (P.M or A.M) and z (time zone).
Following examples demonstrate how are the format specifiers rewritten to the output.
[C#]
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

You can use also date separator / (slash) and time sepatator : (colon). These characters will be rewritten to characters defined in the current DateTimeForma­tInfo.DateSepa­rator andDateTimeForma­tInfo.TimeSepa­rator.
[C#]
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)

Here are some examples of custom date and time formatting:
[C#]
// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

Standard DateTime Formatting

In DateTimeForma­tInfo there are defined standard patterns for the current culture. For example property ShortTimePattern is string that contains value h:mm tt for en-US culture and value HH:mm for de-DE culture.
Following table shows patterns defined in DateTimeForma­tInfo and their values for en-US culture. First column contains format specifiers for the String.Format method.
SpecifierDateTimeFormatInfo propertyPattern value (for en-US culture)
tShortTimePatternh:mm tt
dShortDatePatternM/d/yyyy
TLongTimePatternh:mm:ss tt
DLongDatePatterndddd, MMMM dd, yyyy
f(combination of D and t)dddd, MMMM dd, yyyy h:mm tt
FFullDateTimePatterndddd, MMMM dd, yyyy h:mm:ss tt
g(combination of d and t)M/d/yyyy h:mm tt
G(combination of d and T)M/d/yyyy h:mm:ss tt
mMMonthDayPatternMMMM dd
yYYearMonthPatternMMMM, yyyy
rRRFC1123Patternddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
sSortableDateTi­mePatternyyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
uUniversalSorta­bleDateTimePat­ternyyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)
  (*) = culture independent
Following examples show usage of standard format specifiers in String.Format method and the resulting output.
[C#]
String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                      LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"          LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"  LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"                ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"             ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                        MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                     YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"             SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"            UniversalSortableDateTime