Subtracting Dates in C# and SQL

I know this is really basic stuff but I have seen too many examples of someone trying to implement their own date class to do something simple like subtract one day from today’s date.  I also remember the day (long ago) someone showed me using negative numbers worked in the dateadd function of sql server.  So in case you have not found these functions yet, here are some samples.

C#

This gives you a DateTime object with yesterday’s date.  (Note it will be yesterday at the current time)

SQL

Gives this result:

I think these are good examples of why you cannot forget the basics of math when you are working on a giant calculator. 

Hope this helps.

Programatically Retrieving IP Addresses on Windows 7

We ran into an interesting problem while testing our apps for windows 7.  Our WinForms apps kept throwing a “String or binary data would be truncated. The statement has been terminated.”  This is a pretty common error for SQL server to throw when you push a string that is longer than the column into the insert or update.  We were only seeing this on Windows 7 and it was coming from code we had been successfully running on XP for quite some time.  It turns out to be some code that we used to pull the IP Address of the machine running the application.  The old code is below.

Old Code

The problem with this is its pulling the first IP address found on the machine.  Windows 7 has IPv6 enabled by default.  This code started pulling the much longer IPv6 address which resulted in overflowing the column in SQL.  To correct this we updated the code to the following.

IP6 Aware Code

By comparing to the AddressFamily enum we are able to look at only version 4 addresses.  It’s interesting to note some of the other options in the enum options including some protocols I did not think I would see again.

Custom Attributes When Using Html.TextBoxFor

I finnally got a few minutes to work on the new asp.net mvc 2 this weekend.  I was working through the example on Model Validation Scott Guthrie did on Jan 15th and found my self wanting to resize the text boxes on my input form.  In his example he is using Html.TextBoxFor and calling into his model object for the attributes of the input tag.  This seemed really nice and I worked with it.  Since I wanted to resize the input boxes I started trying to put a class into the parameters of TextBoxFor since it has two overloads with htmlAttributes.  After some fiddling I decided to google, this turned up some questionable advice such as “Don’t be afraid of using raw HTML” which sounds a bit like hard coding it just because you don’t know how.  My search continued.  I decide I would take a look futher down the comments of ScottGu’s post and found the answer there (mostly).  He said about half way down you should use the following syntax:

This seemed to work but I was trying to add a class attribute so I could affect the text boxes.  I tried the following. 

This will not compile.  After a looking at this for a minute its clear that class is a keyword.  Simple work around below.

This got me right where I wanted to be.  I had a class attribute on my input boxes that I could reference in my css.  This would be the end of a happy story if after applying my css it had updated the look of my text boxes.  After a quick look in fire bug I found my page css was being overridden by a site level setting. 

Since this was a change I would not mind having site wide I thought about chaning it there or adding a input[type=”text”] to my pages css.  The only draw back to this is IE6 does not support this style of input selector.  I decided to go with the class element since it works in all the browsers I have to support (IE6 is still 20% of the traffic on our sites).