This is a dumping ground of software development topics that I've run across and found interesting (mainly .NET development). Many topics are covered more thoroughly in other places, and much of the information is gleaned from other places, and I'll try to credit those sources when possible.

Thursday, October 22, 2009

Disabling ASP.NET Session

Interesting tidbit from this MSDN page.

"You should be aware that even when not in use, sessions carry some overhead for an application. You can squeeze a little bit more performance out of your pages if you turn off sessions on pages that do not use it. Also, setting session state to read-only can also optimize pages that read but do not write data to sessions. Configure sessions in this fashion by adding an attribute to the @Page directive in one of these two ways:"

<%@ Page EnableSessionState="false" %>
<%@ Page EnableSessionState="readonly" %>

Wednesday, August 05, 2009

SQL Server - Scripting table data

Scripting data is handy for lookup tables and I found just what I needed in this stored procedure and discussion. I copied the enhanced procedure below, just in case the link ever dies.


CREATE PROCEDURE dbo.spScriptInsertStatements (
@TableName varchar(100),
@WhereClause varchar(1000) = NULL,
@OrderBy varchar(1000) = NULL
)
AS

DECLARE @sql nvarchar(MAX)
DECLARE @sqlColumns varchar(MAX)
DECLARE @sqlColumnValues varchar(MAX)
DECLARE @hasIdentity bit
DECLARE @tbl TABLE (
SortOrder int IDENTITY(1, 1) NOT NULL,
SQLText varchar(MAX) NULL
)

SET @hasIdentity = 0

SELECT @sqlColumns = COALESCE(@SqlColumns + ', ','') + COLUMN_NAME,
@hasIdentity = CASE @hasIdentity
WHEN 0 THEN COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity')
ELSE 1 END
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND (@OrderBy IS NULL OR COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 0)

SELECT @SqlColumnValues = COALESCE(@SqlColumnValues + '+ '', '' + ','') + CASE
WHEN DATA_TYPE IN ('varchar','nvarchar', 'char', 'nchar', 'datetime' )
THEN 'COALESCE(QuoteName(' + Column_Name + ', ''''''''), ''NULL'')'
ELSE 'COALESCE(CAST(' + Column_Name + ' AS varchar(MAX)), ''NULL'')' END
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND (@OrderBy IS NULL OR COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 0)

SELECT @sql = 'SELECT ''INSERT INTO ' + @TableName + ' (' + @SqlColumns + ') ' +
'VALUES ('' + ' + @SqlColumnValues +' + '')'' AS SQLText FROM ' + @TableName +
COALESCE(' WHERE ' + @WhereClause, '') + COALESCE(' ORDER BY ' + @OrderBy, '')

-- If table contains an Identity column and sorting was not specified
IF @hasIdentity = 1 AND @OrderBy IS NULL
BEGIN
INSERT INTO @tbl (SQLText) VALUES ('SET IDENTITY_INSERT ' + @TableName + ' ON ')
INSERT INTO @tbl (SQLText) VALUES ('GO ')
END

INSERT INTO @tbl (SQLText)
EXEC sp_executesql N'EXEC sp_executesql @statement = @sql', N'@sql nvarchar(MAX)', @sql

-- If table contains an Identity column and sorting was not specified
IF @hasIdentity = 1 AND @OrderBy IS NULL
BEGIN
INSERT INTO @tbl (SQLText) VALUES ('GO ')
INSERT INTO @tbl (SQLText) VALUES ('SET IDENTITY_INSERT ' + @TableName + ' OFF ')
INSERT INTO @tbl (SQLText) VALUES ('GO ')
END

SELECT SQLText FROM @tbl ORDER BY SortOrder

-- Unit Test
--
-- EXEC spScriptInsertStatements @TableName = 'glsBatchType'
-- EXEC spScriptInsertStatements @TableName = 'glsBatchType', @WhereClause = 'glsTypeSeq < 3'
-- EXEC spScriptInsertStatements @TableName = 'glsBatchType', @OrderBy = 'glsTypeCode DESC'
--
GO

ASP.NET Panel with empty GroupingText

Using the GroupingText property of an asp:Panel will cause a pretty box to surround a group of controls, but sometimes I want to have that box without the text. I set GroupingText=" " hoping it would be interpreted as no text, but instead you can see the space in the box, which is annoying. Checking out the client side HTML (and MSDN documentation), I found the panel is represented as FIELDSET and LEGEND elements. By replacing the asp:Panel elements with fieldset, and not specifying a legend, I get the caption-free box I want. Note that you may have to put your fieldset in a div to get the style you want, like width. Simple example below:

<asp:Panel runat="server" GroupingText="Group of controls" Width="300px">
A panel using GroupingText="Group of controls"
</asp:Panel>
<br />

<asp:Panel runat="server" GroupingText=" " Width="300px">
A panel using GroupingText=" " (see gap in box above?)
</asp:Panel>
<br />

<div style="width: 300px;">
<fieldset runat="server">
A fieldset to get a solid box with no text.
</fieldset>
</div>

Wednesday, February 13, 2008

C# .NET: Ignoring Alt-F4 key combination

Alt-F4 is a shortcut that closes a window. For some reason you may not be fond of this behavior. After doing some digging around, I finally found a good method of ignoring this request - overriding the ProcessDialogKey method. You can go do more research if you'd like, but the basic code is as follows:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == (Keys.Alt | Keys.F4))
return true;
else
return base.ProcessDialogKey(keyData);
}

Friday, February 08, 2008

Getting a Batch File's Command Line

I can't remember where I got this batch script, but it's handy for getting the complete command used to call the batch file. For instance, the text written to the file may be something like this: "C:\cmdlinetest.bat" -install -param2 some other stuff...
-------------------------------------------------------------------------

REM Set variable 'line' to first token in the command line.
set line=%0

REM Loop through the next token, concatenating value, then shifting the
REM command line to process the next token. Stop when token is empty.
:loop
set line=%line% %1
if "%1" == "" goto :done
shift
goto loop

REM Done finding command line arguments, echo to file.
:done
echo %line% > C:\cmdline.txt

Thursday, May 03, 2007

OuterXml for Java

Java can be extremely frustrating, particularily because there always seems to be many many different ways to do a simple task. For example, I needed to get a fragment from an XML document which seems easy enough. .NET has the XmlNode class which has a property OuterXml. Done. However, no such simple equivalent exists for Java, at least in my limited experience. I don't claim to be an XML expert, so I'm not about to write a diatribe. After a couple of hours, I found this guy's blog, which I used as a basis for the method below. You can figure out how to get a org.w3c.dom.Node object, that's another painful tale...


public static String getOuterXml(Node node)
throws TransformerConfigurationException, TransformerException
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");

StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
}

Thursday, April 05, 2007

Windows Firewall for FTP

This is silly, but it hung me up for more than a few minutes. I was trying to FTP from a laptop to my desktop PC (running Microsoft's IIS FTP server). The client would claim it was connected, then after a few seconds, report "Connection closed by remote host." So I'm guessing it's a firewall problem, and I'm right (for a change), but it was still not easy to find. So below are the screen shots of the magical checkbox I needed to check.

Followers