Cool Tools and Tips

Every time I rebuild my computer I can never remember the tools that I installed and the custom changes i make to the environemnt.  Also I added a few links to others who have similar lists.
 
 
 
 
David Yardy.

ASP.NET Debugging Stop Unexpectedly

My environment: Visual Studio 2008 (VS.NET), Vista, IIS7, ASP.NET. 
I have a situation when I’m debugging ASP.NET and after about 90 seconds of stepping through code the debugger suddenly stops and detaches.
 
Based on info from others, it seems that the underling problem is Vista and Internet Information Server 7 (IIS7).  There are two setting in IIS7 for Application Pools that can create this problem: Ping Enabled and Ping Maximum Response Time.  The default for Ping Maximum Response Time is 90 sec.  By changing these values should resolve the problem.  The references below probably provides a better overview of the problem.
 
Reference:
 
My web application times out when debugging in IIS7

Page.ResolveUrl(“~/ClientScripts/Validation.js”) – Web Application Root Operator

I have the situation where I need to load a JavaScript dynamically.  All the JS files are in the ClientScript directory (http://www.somewebsite.com/ClientScripts/Validation.js).  The ASPX page that references the client scripts could be anywhere, multiple folders (directories) down in the site or in the application root.  I would like a simple way of referencing the the application root.  The magic to this is the ~ (Tidle). The ~ resolves to the root of the current application. 
 
The following line allows me to reference the application root
this.ClientScript.RegisterClientScriptInclude("Validation.js", Page.ResolveUrl("~/ClientScripts/Validation.js"));
 
And here is the results in the HTML file.
<script src="/ClientScripts/Validation.js" type="text/javascript"></script>

ASP.NET AJAX WebServices and ScriptServices

I’ve been working with ASP.NET AJAX for the past week and the following resources allowed me to get up to speed pretty fast.  The following two books that I list are pretty good.   Both of the them cover some different information.  I believe the Professional ASP.NET 2.0 AJAX is a little easier to read and the examples are much better. 
 
Video – How Do I: Use an ASP.NET AJAX ScriptManagerProxy?
 
Video – How Do I: ASP.NET AJAX Enable an Existing Web Service?
 
Video – How Do I: Make Client-Side Network Callbacks with ASP.NET AJAX?
 
Video – How Do I: Choose Between Methods of AJAX Page Updates?
 
MSDN Article – Using ASP.NET Session State in a Web Service
 
Book – Introducing Microsoft ASP.NET AJAX (Dino Esposito)
 
Book – Professional ASP.NET 2.0 AJAX 
 
 

AJAX and JavaScript in ASP.NET & Visual Studio 2008

I have once again started working with AJAX.  About 8 months ago I was pretty deep into ASP.NET and AJAX, but since then I’ve done little with AJAX.  I’ve been finding many good reference for AJAX and do not have a common place to keep all these reference.  So for convince I’m going to keep the references here. 
 
 
Create Control Extenders
Using Visual Studio 2008, ASP.NET 3.5, and AJAX
(I went through the example in this article and everything seemed to work fine)
 
CodeProject – ASP.NET AJAX Controls and Extenders
4.36 out of 5. 
(This article looks very promissing)
 
Building ASP.NET 3.5 AJAX Extender Controls
 
MSDN – JScript IntelliSense Overview
 
WebCast – ASP.NET AJAX Support in Visual Studio 2008
(Good video)
 
WebCast – IntelliSense for Jscript and ASP.NET AJAX (in Visual Studio 2008)
 
JScript IntelliSense: A Reference for the “Reference” Tag 
/// <reference name="MicrosoftAjax.js" />
 
 
 

When Leaving Page, Prompt The User That Changes Have Not Been Saved

I’m in the process of create a few input forms in ASP.NET (Name, address, and etc).  If the user makes a change to the page and tries to navigate away from the current page without previously clicking save, the current page should prompt the user that there are changes to the page and does the user want to continue to navigate to a different page.  The prompt should be triggered by any of the following:  selecting a link, closing the window, entering a new URL in the address bar, selecting back, hitting refresh or other means. 
 
To solve this problem I first tried using onunload, but could not find any way of stopping the user from going to another page.

[sourcecode language=”xml”]
body onunload=”ConfirmExit”
[/sourcecode]

After a little research I found a method window.onbeforeunload.  window.onbeforeunload provides the developer a way to notify the user that there has been changes that have not been save before navigating to a different page.  In the following example I use ASP.NET instead of strait HTML.  The code behind loops through every webcontrol that’s in the form and wires up the control’s onchange JavaScript event to call InputChange function. If the user changes any of the controls the isDirty variable is set to true. 
 
As you can see in the following code the window.onbeforunload is set to ConfirmExit function. This is the part that is used to notifies the user. Any time the user tries to exit the page the ConfirmExit function will be called.  In the ConfirmExit function, if the isDirty variable is true, the user should be prompted with a message to confirm if they want to leave the page.  If isDirty is false the the user is redirect without a prompt.  
 
If the user clicks save button, I do no not want him to be prompted that the data has not been save. So I set the following for the button control.

[sourcecode language=”javascript”]
OnClientClick=”isDirty=false”
[/sourcecode]

 
I tested the following code and confirm it works in IE7 and FireFox 2.0

[sourcecode language=”csharp”]
public partial class TestPromptSave : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false) {
SetupJSdataChange(form1);
}
}

private void SetupJSdataChange(Control parentControl) {
foreach (Control control in parentControl.Controls) {
//Response.Write(control.ID + “<br>”);
if (control is WebControl) {
WebControl webControl = control as WebControl;
webControl.Attributes.Add(“onchange”, “InputChanged(this)”);
}
}
}
protected void Page_Save(object sender, EventArgs e) {
}
}
[/sourcecode]

HEre some more

[sourcecode language=”xml”]
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”TestPromptSave.aspx.cs” Inherits=”TestPromptSave” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
</head>
<body >
<script type=”text/javascript” language=”javascript”>
var isDirty = false;
window.onbeforeunload = ConfirmExit;

function InputChanged(control)
{
isDirty = true;
}
function ConfirmExit()
{
if(isDirty == true){
return “You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?”;
}
}

</script>

<form id=”form1? runat=”server” >
<a href=”RedirectPage.aspx”>RedirectPage.aspx</a>
<br /><br />
<table>
<tr>
<td>
First Name
</td>
<td>
<asp:TextBox ID=”txtFirstname” runat=”server” />
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
<asp:TextBox ID=”txtLastName” runat=”server” />
</td>
</tr>
<tr>
<td>
State
</td>
<td>
<asp:DropDownList ID=”ddlState” runat=”server” >
<asp:ListItem Text=”" Value=”" />
<asp:ListItem Text=”CO” Value=”CO” />
<asp:ListItem Text=”TX” Value=”TX” />
<asp:ListItem Text=”FL” Value=”FL” />
<asp:ListItem Text=”OK” Value=”OK” />
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:Button ID=”butSave” runat=”server” Text=”Save” OnClientClick=”isDirty=false” onclick=”Page_Save” /> &nbsp; | &nbsp;
<asp:Button ID=”butCancel” runat=”server” Text=”Cancel” />
</form>
</body>
</html>
[/sourcecode]

Resource:
MSDN – onbeforeunload Event
 
4 Guys From Rolla.Com – Prompting a User to Save When Leaving a Page
http://www.4guysfromrolla.com/webtech/100604-1.shtml
 
The IE OnBeforeUnload Event Handler
 
Reminding Users to Submit Forms
 
IE runs onbeforeunload twice for no reason?
 
Warn users about unsaved changes in a web form

SQL Server Searching Object Text

I had a situation where soft deletes were going to be implemented in the database, but for the past year hard deleted were being implemented.   On every table the IsDeleted column was created.  The newer Stored Procedures, Views, and Functions included IsDeleted in there WHERE statement, but all the db object that were created prior to implementing the IsDeleted needed to be changed to include the IsDeleted. 
 
The following was my first attempt to identify DB objects that did not include IsDeleted.  This seemed to return the correct data. But, there seem to be a problem.  There were situations where the results that were returned included duplicate names.  Come to find out the [Text] column that is returned from syscomments is NVARCHAR(4000).  If the [Text] of DB object exceeded 4000 characters an additional records was created every additional 4000 characters.   There were a few stored procedures that surpassed 4000 characters and in-turn had duplicate Object_names returned in the results .  This is not that big of a deal, but I wasn’t getting back the results I expected. 
 

SELECT OBJECT_NAME(id), *

FROM syscomments

WHERE

(

OBJECTPROPERTY(id, ‘IsScalarFunction’) = 1

OR OBJECTPROPERTY(id, ‘IsTableFunction’) = 1

OR OBJECTPROPERTY(id, ‘IsProcedure’) = 1

OR OBJECTPROPERTY(id, ‘IsView’) = 1

)

AND [Text] NOT LIKE ‘%IsDeleted%’

 
I found a solution to my problem by referencing sys.objects table and using the function OBJECT_DEFINITION, which returns the Transact-SQL source text of the definition of a specified object.  OBJECT_DEFINITION is not limited to NVARCHAR(4000), but instead is NVARCHAR(MAX).  I probably could have referenced INFORMATION_SCHEMA.Tables, but decided not to mess with it today and just wanted to get it to work.
 

SELECT

Name, *

FROM

sys.objects

WHERE

(

  OBJECTPROPERTY(object_id, ‘IsScalarFunction’) = 1

  OR OBJECTPROPERTY(object_id, ‘IsTableFunction’) = 1

  OR OBJECTPROPERTY(object_id, ‘IsProcedure’) = 1

  OR OBJECTPROPERTY(object_id, ‘IsView’) = 1

)

AND OBJECT_DEFINITION(object_id) NOT LIKE ‘%IsDeleted%’

 
 
 
References:
SQL Server OBJECT_DEFINITION (Transact-SQL)
 

SQL Server sysobjects

 

Searching Syscomments Accurately (View the Comments section)

 

SQL Server Custom Shortcuts

Querying the INFORMATION_SCHEMA  (But has info about SQL Server Shortcuts)
This is where I originally found info about SQL Server shortcuts.
 
Assigning shortcuts to commonly run tasks in SQL Server 2005 Management Studio
 
SELECT * FROM, sp_helptext ShortCut for SQL Query Analyzer
 
 
SQL SERVER – Query Analyzer Shortcuts (SQL Server default shortcut keys)
This is probably my favorite SQL Server web site

SQL Server For Each Procedure

I created my sp_ForEachProcedure base on Microsoft undocumented sp_msForEachTable.  It doesn’t have all the features of sp_msForEachTable, but does include a few of them.  I even added a few custom parameters (@print, @execute).   I did dynamic SQL because I was having problems creating the where statement.  I believe I could use a case, but went ahead and used the dynamic sql.  I would like to join to syscomments and search the actual stored procedure, but ran out of time. 
 
In the future I plan on creating sp_ForEachView
 
 
 

–example of sp_msforeachtable

–exec sp_msforeachtable @command1=’PRINT ”?”’, @whereand=’ and o.name like ”%Human%”’

 
ALTER

PROCEDURE sp_ForEachProcedure

  @command1

VARCHAR(MAX),

  @whereand

VARCHAR(MAX) = NULL,

  @print

BIT = 1,

  @execute

BIT = 0

AS

–DECLARE @command1 VARCHAR(MAX)
–DECLARE @whereand VARCHAR(MAX)
–SET @command1 = ‘PRINT ”?”’
–SET @whereand = ‘name like ”%contact%”’
 

DECLARE

@executeCommand NVARCHAR(MAX)

SET

@executeCommand =

  ‘ DECLARE @statementTable TABLE (ID INT IDENTITY(1,1) ,statement NVARCHAR(MAX)) ‘

+

  ‘ DECLARE @userCommand NVARCHAR(MAX) ‘

+

  ‘ INSERT INTO @statementTable (statement) ‘

+

  ‘ SELECT ‘

+

  ‘ REPLACE(”’

+ REPLACE(@command1, ””, ”””) + ”’, ”?”, ”[” + p.name + ”]”)’ + ‘ AS Statement’ +

  ‘ FROM SYS.Procedures p ‘

IF

(@whereand IS NOT NULL)

BEGIN

  SET @executeCommand = @executeCommand +

  ‘ WHERE ‘

+ REPLACE(@whereand, , ””)

END

IF

(@print = 1)

BEGIN

  SET @executeCommand = @executeCommand + ‘ SELECT * FROM @statementTable’

END
IF

@execute = 1

BEGIN

  SET @executeCommand = @executeCommand +

  ‘ DECLARE @loopCount INT ‘ +

  ‘ DECLARE @statement NVARCHAR(MAX) ‘ +

  ‘ SELECT @loopCount = COUNT(*)FROM @statementTable ‘ +

  ‘ WHILE @loopCount <> 0 ‘ +

  ‘ BEGIN ‘ +

  ‘ SET @statement = (SELECT statement FROM @statementTable WHERE ID = @loopCount) ‘ +

  –‘ PRINT ”EXECUTING: ” + @Statement ‘ +

  ‘ EXEC sp_executesql @Statement ‘ +

  ‘ SET @loopCount = @loopCount – 1 ‘ +

  ‘ END ‘

END

–FOR DEBUGGING
–PRINT @executeCommand

EXEC

sp_executesql @executeCommand

GO
 

–sp_ForEachProcedure ‘PRINT ”?”’, ‘NAME LIKE ”%Human%”’, 1
–sp_ForEachProcedure ‘GRANT EXECUTE ON ? TO Scrub ‘, ‘NAME LIKE ”%Human%”’, 1, 1
 
References:
Is a Temporary Table Really Necessary (How not to use cursors):