Monday, December 7, 2009

WCF basicHttpBinding with Windows Authentication

Wrote a Report scheduling program and was having a little problem with Windows Authentication and didn't want to turn Anonymous access on so I stumbled upon the following article which was a great help:

Recipe: WCF basicHttpBinding with Windows Authentication

Wednesday, October 7, 2009

Saving Binary in SQL Server to PDF file

Well, I'm working on a project where I enable a user to upload a file and store it in SQL Server 2005, and then need to pull it out of the database and save the PDF file to disk using a WinClient app.

System.IO.FileStream fs;                // Writes the binary to a file (*.pdf).
System.IO.BinaryWriter bw; // Streams the binary to the FileStream object.
int bufferSize = 100; // Initial size of the binary buffer.
byte[] outbyte = new byte[bufferSize]; // The binary byte[] buffer to be filled by GetBytes.
long retval; // The bytes returned from GetBytes.
long startIndex = 0; // The starting position in the binary output.
string rec_id = ""; // The id to use in the file name.

while (myReader.Read())
// Get the id.
rec_id = myReader["DocumentationUploadID"].ToString();

// Create a file to hold the output.
fs = new System.IO.FileStream("DocumentID-" + rec_id + ".pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
bw = new System.IO.BinaryWriter(fs);

// Reset the starting byte for the new Binary.
startIndex = 0;

// Read the bytes into outbyte[] and retain the number of bytes returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

// Continue reading and writing while there are bytes beyond the size of the buffer.
while (retval == bufferSize)

// Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

// Write the remaining buffer.
bw.Write(outbyte, 0, (int)retval);

// Close the output file.

Monday, September 28, 2009

Windows 7 Launch Party

Attended the Windows 7 and Server 2008 R2 launch party in Atlanta today.

It was interesting to see the sensor demos so I thought I'd post a quick note about it if anyone is interested.

Having Fun link

Monday, September 21, 2009

Pad Integer with Zeros using C#

A quick example for padding an Integer with zeros in C#:

System.Int32 intMyInteger = 87366;
System.String strMyInteger = intMyInteger.ToString().PadLeft(20, '0');

Thursday, September 10, 2009

SQL Change Table Owner

Need a quick way to change the table owner for multiple tables then try this:

DECLARE @old sysname, @new sysname, @sql varchar(1000)

@old = 'CHANGE_name_here_ONLY'
, @new = 'dbo'
, @sql = '
AND TABLE_SCHEMA = ''' + @old + '''
EXECUTE sp_changeobjectowner ''?'', ''' + @new + ''''

EXECUTE sp_MSforeachtable @sql

Wednesday, August 5, 2009

Little fun...

Well, it's getting close to football season so I thought I'd post a little fun.

Who do you think would win? :)

Friday, June 19, 2009

Formatting Dates with T-SQL

I've seen a lot of weird and extravagant ways to format dates, and wanted to share a simple method for displaying dates with as little code as possible.

CONVERT (VARCHAR, column_name, StyleID)

Example StyleIDs are listed below:

0 or 100
9 or 109
13 or 113
20 or 120
21 or 121

mon dd yyyy hh:miAM (or PM)
dd mon yy
Mon dd, yy
mon dd yyyy hh:mi:ss:mmmAM (or PM)
dd mon yyyy hh:mm:ss:mmm(24h)
yyyy-mm-dd hh:mi:ss(24h)
yyyy-mm-dd hh:mi:ss.mmm(24h)
yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
dd mon yyyy hh:mi:ss:mmmAM
dd/mm/yy hh:mi:ss:mmmAM

Monday, May 4, 2009

ASP.NET menu problem in IE8

If you use the ASP.NET menu on any of your web sites and just upgraded to IE8 then you probably just noticed that the drop down menu may have a white background.

What IE8 is doing is correct (by design), in the sense that in Standards mode IE8 is following the standards. By default the (element).currentStyle.zIndex returns "auto" when the zindex has not been set. The ASP.NET menu controls just assumes a different value. :)

Override the zindex property. Like such:

<style type="text/css">
z-index: 100;

<DynamicMenuStyle CssClass="IE8MenuFix" />

Friday, May 1, 2009

Modal Progress Display for ASP.NET

If you use the AJAX Extensions in your ASP.NET web site then you've probably used the UpdateProgess control to display that some action is processing in the background.

Here is a code snippet of a simple Modal type processing box I use for some static notifications of some action.


<asp:UpdateProgress ID="UpdateProgress2" runat="server" DynamicLayout="False" DisplayAfter="1">
<div class="PageWorkingBackground">
<div class="UpdateProgress">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/SEIB-Validating.gif" AlternateText="[image]" />
<asp:Image ID="ajaxLoadNotificationImage" runat="server" ImageUrl="~/images/bar_loader.gif"
AlternateText="[image]" />

position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
min-height: 100%;
min-width: 100%


width: 150px;
text-align: center;
vertical-align: middle;
position: fixed;
bottom: 50%;
left: 45%;
border: solid 2px #453825;
margin: 10px;
padding: 10px;

Here is a picture of the modal box in action:

Friday, April 24, 2009

Complicate Brute Force Attacks in ASP.NET

Have you ever used a test program to simulate a Brute Force attack on your login screen? It's pretty vicious and cool at the same time.

On my login screens I've developed for public access I perform the following to help block any Brute Force attempt.

1. Implement Captcha technology after 3rd failed login attempt.
2. Lock account after fifth failed login attempt (optional).
3. Implement a random delay using System.Threading.Thread.Sleep on each failed login attempt.

Of couse, all of this may not be 100% attack proof, but is sure complicates things.

Below is some pseudocode I used to implement step 3 above:

//Sample function to validate user
if (fncValidateUser(ref mCN, txtUserID.Text.Trim(), txtPassword.Text.Trim()))
   Response.Redirect(strReturnURL, true);
   //Delay Request

//Randomly delay request, I use anywhere between 2 and 20 seconds.
private void subDelayRequest()
   System.Int32 minSeconds, maxSeconds;
   minSeconds = 2;
   maxSeconds = 20;
   Random rand = new Random();
   System.Threading.Thread.Sleep(rand.Next(minSeconds, maxSeconds) * 1000);

Thursday, April 16, 2009

A Little Fun with Generic Filters

Some have asked about my generic filters, and to answer some of the questions:

  • I have finally finished it (back in February).

  • It is database driven - heard of meta data driven ui? :)

  • It is used for both filtering data and filtering report criteria.

A little history on my project..
I've worked on 2 similar projects in the past. The first was a database driven filter screen written in classic ASP and simply provided a stacked layout of different controls and a simple listing of results with hyperlinks to edit details. It was precise and functional, but not expandable to include reports or reuse elsewhere. The second, my previous job, was class driven and cumbersome to develop against, to say the least. It generated only list boxes and a full page of results - thus it had no paging or reording abilities, but had a nice flow layout.

Since I started a new job I decided to start on my own generic filter that borrowed the idea of a database driven backend and various control types, and a flow layout (using DIV tags instead of tables) and threw in a slew of other enhancements.

Here is a quick screen shot of my generic filter (uses test data):

Here is the results screen, notice it uses paging and sorting (took a while to figure that out since my gridview isn't bound).

Here is a screen shot of the Reporting filter, it allows saving and scheduling of reports and more.

Granted there are a myriad of other options that a few simple screen shots can't show, but I'll try to post more info in the future. Best of all it's a simple component that will fit into any web page provided you have a database backend. :)

Sunday, March 22, 2009

Equal Column Heights using CSS and JQuery

Thought I would share a little JQuery code.

I finished my dynamically generated filter screen a few months ago and when I did the layout I decided to dynamically generate DIV tags instead of TABLES.

I basically had 3 types: small, medium, and large columns that floated left with a maximum of 3 small columns per row, 2 small and 1 medium, etc.

All worked fine except I had a nice light brown background for each and the heights were not equal. It kind of looked like a jumbled bar chart. :)

Since everything is dynamic, I turned to JavaScript. I first wrote a couple of JavaScript functions and all worked in IE but not FireFox (yep, it's usually the other way around, huh?) so I settled with JQuery afterall it's backed by Microsoft and they now offer Intellisense for it in VS 2008 SP1. It was sure easier, take a look at the code below:

//Function to loop through each item with passed in Class
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if (thisHeight > tallest) {
tallest = thisHeight + 5;

//Code to call equalHeight passing in Wildcard col because different width
//columns get generated (colSmall, colMedium, colLarge)
$(document).ready(function() {

Dynamically generated CSS Level 2.1 at it's finest. :)

Pretty simple and quick, huh? Here's quick photo of what it looks like:

Sunday, February 8, 2009

Windows 7 Install Experience

Well, one word.. "WOW"

Yes, I installed Windows 7 Beta 1 on my Dell Latitude 610, and it took roughly 20 minutes. Let me rephrase that, I was online with my wireless connection and no errors in 20 minutes. Pretty sweet compared to how long it took to reload in XP.

More updates as they come...

Installed the following successfully:
Adobe Flash Player 10
Adobe Reader 9
Microsoft Visual Studio 2008
SnagIt 8
Microsoft SQL Server 2008 Developer Edition

Thursday, January 8, 2009

Loop through DataSet

Just wanted to share a quick snippet of looping through a .NET DataSet

DataSet ds = new DataSet();
foreach(DataRow dr in ds.Tables[0].Rows)