Friday, December 22, 2006
Windows Developer Power Tools! Another Must Read

I'm seen parts of this book in pre-production and it's a must read!

Jim with the Book

Windows Developer Power Tools on Amazon

 

Want your copy signed??? Come to CodeMash! :)


CodeMash | Development | Utilities
Friday, December 22, 2006 6:45:00 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Wednesday, March 15, 2006
More adventures with SQL Server Express

This was fun. It’s a completely different database and set of circumstances than last night's fun and frivolity.

I finished an application, zipped up the folders and sent it off to a client. In this application there is a desktop app that talks to a web service that talks to a SQL Server 2005 Express database. Sounds fairly simple, right? Right.

Well, the client unzips it all to a directory, sets up the virtual application for the web service and starts trying to run the desktop portion. It starts up, pulls up all of the lookups that we have in the database and it all looks good. Now, I’m on the phone and elated at how smooth this is going. At this point, the client tries to create a new record which is going well (wow, even a little drop down calendar and oooh look at that, it’s all of the types of records that I could create so I don’t have to type there… wow… and more gushing) until they try to save… At this point, there is a wonderful error that pops up –

“Failed to update database ‘database name.mdf’ because the database is read-only.”

Crud! After getting them to check the file properties and other such things, I called Patrick Steele, who has been working with me on this project, to see what he thought about it. It’s been a long day, especially after last night’s SQL Server Express issues, so I’m not thinking all that clearly. Patrick, after running through some of the same questions that I had so I’m feeling a little better about my thinking, has the thought that it might be a security issue and that the ASPNET user might not have permissions to write to that directory. Sure enough, since the ASPNET user doesn’t have permissions the database loads in read-only mode. Fix that and the database and the app work just fine.


Development | SQL Server
Wednesday, March 15, 2006 2:11:56 AM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Tuesday, March 14, 2006
SQL Server 2005 Express Issues

Yesterday I had to make a quick change to a SQL Server 2005 Express database that I'm using for an internal to SRT Solutions tool. I decided that the easiest way to do it was to leverage the auto-connect feature of SQL Server 2005 Express by just FTPing the mdb down, making my change and ftping it back up. This has worked a number of times in the past. Not this time. I got a "login failed for user 'USERNAME'" when I tried to run the application. That was infuriating. I spent the next 4 hours or so working with the SQL Server Management Studio Express (Enterprise Manager lite for Express) assigning, unassigning permissions, users, attaching, detaching, backing up, restoring and everything else that I knew to do. I kept getting "User could not login to user's default database" and general login failures. My personal favorite that I saw yesterday was “Could not attach, DatabaseName is not a primary database file. (Microsoft SQL Server, Error: 5171)”. Now, I'm usually pretty good with googling the answers, but this time I'm really coming up short.

All of this is proving to me that I’m a programmer, not an administrator. I’m digging the new functionality in SQL Server 2005 but I’m not going to be the one to administer the databases that I write code for.

Long story short – I raised the white flag. I IMed Jason Follas and asked him if he knew what was going on. He figured out that it had something to do with the master file that SQL Express was using for the auto-connect. He found this forum topic. Here’s the relevant snippet:

“When you first run SSE this way, it makes a local copy of master etc. in C:\Documents and Settings\<your user account name>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS.  That's why it's so slow the very first time it loads under a new user profile.  It may even timeout the first time you ever use it under a given profile.

Based on your description, it's very possible something went bad in those files for some reason.  So, I would first make a backup and then delete the SQLExpress directory under C:\Documents and Settings\<your user account name>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data.  That will get rid of the local copy of master, etc.  Then, reboot. ”

Well – Lance (the author of the post and author of SQL Server 2005 Express Edition Starter Kit) was absolutely right. I killed the master files (after copious backups) and rebooted the machine. It’s working like a champ now. On reboot, it regenerated the master files needed and started right up.

Thanks Lance and Jason


Development | SQL Server
Tuesday, March 14, 2006 2:07:29 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Monday, March 13, 2006
Keith Elder, .NET Pimp

Keith Elder used to be one of the biggest PHP advocates around. Now, he runs a site called http://www.dotnetpimps.net where he is the foremost .NET Pimp. He pointed out on his blog that he’s put up and article on why he made the conversion. More at http://dotnetpimps.net/blogs/theelder/about.aspx.


Articles | Development
Monday, March 13, 2006 3:11:11 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Friday, October 14, 2005
log4net on the Compact Framework

I was reminded of log4net today when working on a project. The client asked a logging framework and the cool part is that it’s a Compact Framework application. Sure enough log4net supports the Compact Framework.

There are a couple of minor differences that you should be aware of if you are interested in using log4net on the Compact Framework. First of all, not all of the appenders make sense – like the RemotingAppender, the EventLogAppender and the OracleAppender. Secondly, there is no mechanism for retrieving assembly level attributes so you have to explicitly initialize and shut down the log4net engine.

 

static void Main()

{

      try

      {

            //This line configures the log4net engine.

log4net.Config.DOMConfigurator.Configure(

new FileInfo(@"\log4net.config"));

 

            frmLogin loginForm = new frmLogin ();

            loginForm.ShowDialog();

                  Application.Run(new frmMain());

            }

      }

      catch (System.Exception e)

      {

            _log.Error("Main", e);

      }

      finally

      {

            //This line shuts down the log4net engine

            log4net.LogManager.Shutdown();

      }

}

 

The other thing that was interesting is that the online help on http://logging.apache.org/log4net showed conversion patterns with %message%newline to print the message and then put a newline in the file. I had to use %m%n because the first one there printed as

 

My stringessage

ewline

 

which was not quite what was expected.

 

Have fun with log4net, it’s a great lightweight logging framework for the compact framework.


Compact Framework | Development | Utilities
Friday, October 14, 2005 12:47:18 AM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Thursday, February 10, 2005
NUnit Stands the Test - Follow up

Patrick Steele made a good point about my last post on Unit Testing. The code that I wrote just happened to use the Test keyword at the beginning of each of my methods. That’s not required. I just happed to like that convention because it reads well.

I do use the attributes and encourage everyone else to because, as Patrick also points out, TestDriven.NET and other tools don’t use it.

Look for another article on TestDriven.NET early next week.


Articles | Development
Thursday, February 10, 2005 7:33:23 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Tuesday, February 01, 2005
NUnit Stands the Test

Unit testing is only enjoyable and productive with the right toolset. NUnit is the foundation of this toolset. It is a unit testing framework for any .NET language.

NUnit leverages Reflection and Attributes to dynamically discover and execute your tests. The structure of a NUnit unit test is as follows.

Imports NUnit.Framework

 

<TestFixture()> _

Public Class DemoTests

    <Test()> _

    Public Sub TestGetIntegerFromConfigFile()

        'Some code that tests the desired functionality

    End Sub

End Class

 

You add the TextFixture attribute to notify NUnit that a class is a suite of tests. You add the Test attribute to mark a method as a single test case. You can have as many tests in a given test fixture as you want and as many test fixtures in a project as you want. The class that is the TestFixture has to be public and have a default constructor so that NUnit can use reflection to dynamically instantiate it. The methods decorated with the Test attribute have to be public, not take any arguments and return no values (Sub in VB.NET and void in C#) for the same reason.

The next step is to write the code that tests the desired functionality. It’s best if the tests are fairly straight forward and simple.

    <Test()> _

    Public Sub TestGetIntegerFromConfigFile()

        Dim demo As VBDemo.Demo = New VBDemo.Demo

        Dim result As Integer = _

            demo.GetFromConfigFile("MyValue", 0)

 

        Assert.AreEqual(1, result)

    End Sub

 

Assert.AreEqual is the really crucial bit of code from this test. This is one of several types of assertions. An exception of type NUnit.Framework.AssertionException is thrown if one of the assertions fails.

Assert.AreEqual(value a, value b)

Tests if b is equal to the expected value a

Assert.AreSame(reference a, reference b)

Tests if two references point to the same object in memory

Assert.IsFalse(Boolean)

Tests if the Boolean is False

Assert.IsTrue(Boolean)

Tests if the Boolean is True

Assert.IsNull(reference)

Tests if reference is Null as expected

Assert.IsNotNull(reference)

Tests if reference is something as expected

More complex tests can be accomplished with normal if statements and the Assert.Fail method.

If (Not SomeReallyComplexStatement) Then

   Assert.Fail()

End If

 

The code that this test exercises takes the name of a configuration setting and a default value. It will return the value from the configuration file or the default value. The test above assumes that the configuration file has a value as follows.

<configuration>

    <appSettings>

            <add key="MyValue" value="1" />

    </appSettings>

</configuration>

 

A small thing that I’ve discovered is that it’s helpful in cases where there is a config file that the tests are in a console application because VS.NET will manage the app.config automatically for you. In addition, if your tests are in a console application, they could be self sustaining and run whether someone has a NUnit test runner, like NUnit GUI, installed or not. You would have to call the tests from the Main of your console application. You know that the test failed when if there is a NUnit.Framework.AssertionException thrown. I have found that this is helpful in a “Clean” environment where developer tools are not allowed.

If the code is as follows.

Public Class Demo

    Public Function GetFromConfigFile( _

            ByVal settingName As String, _

            ByVal defaultValue As Integer) As Integer

 

        Return defaultValue

    End Function

End Class

 

And you run the test; the result will be as follows.

This is expected. Now you need to fix the code so that it will not result in a red bar.

Now modify the function as follows.

    Public Function GetFromConfigFile( _

            ByVal settingName As String, _

            ByVal defaultValue As Integer) As Integer

 

        Try

            Dim reader As System.Configuration.AppSettingsReader = _

                New System.Configuration.AppSettingsReader

 

            Dim resultAsObject As Object = _

                reader.GetValue(settingName, GetType(Integer))

 

            Dim result As Integer = _

                Int32.Parse(resultAsObject.ToString)

 

            Return result

        Catch ex As System.InvalidOperationException

            'Either the value didn't exist or it was not an Int32

            'Fall through to the default value below

        End Try

 

        Return defaultValue

    End Function

 

Once it is, you are rewarded with a green bar as follows.

Since the normal path is working, you need to test the alternative paths as follows.

There are a number of alternative paths that we can investigate. You can pass in garbage, Nothing (or null in C#), the name of an item that’s not an integer and so on. You could decorate the method with an additional attribute, ExpectedException, if we expected this code to throw an exception rather than handle all of its exceptions. For this example, you simply need the non existent config value and the non integer config value cases.

    <Test()> _

    Public Sub TestGetIntegerFromConfigFileNonExistant()

        Dim demo As VBDemo.Demo = New VBDemo.Demo

        Dim result As Integer = _

            demo.GetFromConfigFile("MyNonExistantValue", 0)

 

        Assert.AreEqual(0, result)

    End Sub

 

    <Test()> _

    Public Sub TestGetIntegerFromConfigFileNotAnInt()

        Dim demo As VBDemo.Demo = New VBDemo.Demo

        Dim result As Integer = _

            demo.GetFromConfigFile("MyNotAnIntValue", 0)

 

        Assert.AreEqual(0, result)

    End Sub 

Now you have a well tested function and tests that can be run over and over again.

We have covered the basics of setting up a unit test with NUnit. There are more features that we will cover in a future post. These include the ability to specify setup and teardown methods, categories of tests and suites of tests.

The code for this post can be downloaded below. It does require NUnit.


Articles | Development
Tuesday, February 01, 2005 2:35:12 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Thursday, September 16, 2004
Test Driven Development

Last night Eric Maino (http://www.meeteric.net) gave a talk at GANG (http://www.migang.org) about Test Driven Development (TDD). It was a very good talk that touched on a lot of the important issues involved in testing. It was a good overview of NUnit (http://www.nunit.org) and how TDD works. The slides will be up on the GANG web site soon.

 

One of the questions that came up was how GUI testing works with NUnit. I happened to attend a talk at SD West (http://www.SDExpo.com) by Elisabeth Hendrickson from Quality Tree (http://www.qualitytree.com) on that exact topic. She has been working on a test harness for GUI testing that’s built on NUnit. You can learn more about it here - http://www.qualitytree.com/autotest/dotnetgui.htm.


Development | Utilities
Thursday, September 16, 2004 8:58:14 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Friday, June 25, 2004
Snippet Compiler

I’ve found this little tool invaluable. I use it to test out RegEx expressions, String Formatting and tons of other useful little bits of code.

I used to keep a dummy project around for this purpose, but it’s a pain to load VS.NET every time that you want to test a quick string formatting and it’s too painful to test to that location in your real application just to see if it worked and see what it does.

I hope that you enjoy it as much as I do.


Development
Friday, June 25, 2004 1:56:05 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Thursday, June 17, 2004
History of Programming Languages

I thought that this was an interesting item. It’s the history of programming languages back to the mid-50s. There are a few things that I saw that were interesting – such as the fact that Fortran will be 50 this November and is the ancestor to a number of programming languages. There were a few inaccuracies, such as the fact that they skipped from VB 1.0 to VB 6.0 and completely missed all of the releases between.

Oh well, it’s an interesting thing anyways…


Development
Thursday, June 17, 2004 7:54:38 PM (GMT Standard Time, UTC+00:00)  #  Comments [0] 

 Tuesday, February 03, 2004
Databinding on the Compact Framework

Through careful use of databinding, your UI code can be very light weight.

There are two forms of databinding that we need to discuss, binding to properties of objects and binding to a list of objects – which often binds to properties of the individual objects.

First, let’s deal with the less talked about binding to properties. The code to setup a binding to a particular property on an object is fairly simple. The following snippet binds the text property of the _txtName textbox to the name of a person.

 

_txtName.DataBindings.Add("Text", person, "name");

 

This assumes several things. First of all, it assumes that the TextBox has a Text property. Second, it assumes that the person is not null and lastly, that the person has a valid property called name. Once you work through those assumptions, the TextBox in question will not only show but allow you to edit the person’s name with that one line of code. If, however, instead of a person object, you have a table with rows of people, you will bind as follows.