Linq To Entities with a SQL Compact Database

A while ago I wrote this (Dutch) article on how to perform LINQ to SQL for your SQL Compact databases, since Visual Studio did not understand this, we had to do it manually, as described in the article just mentioned.

But since the release of .NET Framework 3.5 SP1, and the therein included ADO.NET Entity Framework, it is now possible to generate a model out of a SQL Compact database!

Unfortunately this is not (yet) possible for Smart Device applications (Windows Mobile), but who knows what the future might bring!

C#: Smaller syntax for event handlers.

Using lambda functions you can shorten your event handlers.

E.g.:

With a normal event hander:

class Test
{
	private Timer timer;

	private void Timer_Elapsed(object sender, ElapsedEventArgs e)
	{
		Console.WriteLine(string.Format("Object: {0} sends: {1}", sender, e));
	}

       public Test()
	{
		this.timer = new Timer();

		this.timer.Elapsed += new ElapsedEventHandler(this.Timer_Elapsed);

		this.timer.Interval = 100;
		this.timer.Start();
	}
}

With an anonymous function:

class Test
{
	private Timer timer;

        public Test()
	{
		this.timer = new Timer();

		this.timer.Elapsed += delegate(object sender, ElapsedEventArgs e)
					 {
						 Console.WriteLine(string.Format("Object: {0} sends: {1}", sender, e));
					 };

		this.timer.Interval = 100;
		this.timer.Start();
	}
}

And with an anonymous lamba:

class Test
{
	private Timer timer;

        public Test()
	{
		this.timer = new Timer();

		this.timer.Elapsed += (sender, e) => Console.WriteLine(string.Format("Object: {0} sends: {1}", sender, e));
		//or you can explicitly type your parameters:
		this.timer.Elapsed += (object sender, ElapsedEventArgs e) => Console.WriteLine(string.Format("Object: {0} sends: {1}", sender, e));

		this.timer.Interval = 100;
		this.timer.Start();
	}
}

And with a named lamda:

class Test
{
	private ElapsedEventHandler elapsedEventHander;
        private Timer timer;

        public Test()
	{
		this.timer = new Timer();

		this.elapsedEventHander = (sender, e) => Console.WriteLine(string.Format("Object: {0} sends: {1}", sender, e));

		this.timer.Elapsed += this.elapsedEventHander;

		this.timer.Interval = 100;
		this.timer.Start();
	}
}

Which one to take? The one that suits you and your current application / case!

Sidenote: sorry for the layout, I will fix it ASAP. Fixed 🙂

CodeRush Express – Useful tool for C# developers in VS.NET 2K8

(I quote):

Developer Express and Microsoft are proud to announce a new version of CodeRush licensed exclusively for C# developers working in Visual Studio. The new product is called CodeRush Xpress, and it includes a fresh selection of hand-picked features taken from CodeRush and Refactor! Pro.

And I love it! It has very handy functions for refactoring your code very fast!

I recommend it for everyone!

Usefull links (both contain the downloads):

If you are interested in learning the features you can access these movies:

Dustin Campbell also presented a video on this tool, among other useful Visual Studio 2008 shortcuts  on PDC 2008, you can find it here (click below for wmv-hd download).

Java: casting Object to int

I found something odd when writing a Java program yesterday.

In a JTable you have an int to display (the amount ordered e.g.), but when getting the value of a cell you obviously get an Object, not an int.

In order to get the Object into an int variable, you might experience this issue:

//this does not works (obviously)
int element = this.jTableProducts.getElementAt(4, 4);

//this works! (obviously, Integer is a subclass of Object)
Integer element = (Integer)this.jTableProducts.getElementAt(4, 4);

//this odly enough does not works
int element = (int)this.jTableProducts.getElementAt(4, 4);

//but this works
int element = (Integer)this.jTableProducts.getElementAt(4, 4);

This is due the autoboxing of Integer to int (and vice versa).

Java: My FileFilter implementation

This is my javax.swing.filechooser.FileFilter implementation (for the JFileChooser):

FileFilter fileFilter = new FileFilter()
{
	@Override
	public boolean accept(File file)
	{
		return file.getAbsolutePath().endsWith(".xml") || file.isDirectory();
	}

	@Override
	public String getDescription()
	{
		return "xml settings files";
	}
};

It accepts .xml files, but allows browsing through directories

SQL Server 2008 Express Edition + Visual Studio 2008 x64 User instance problems.

As described in this connect article there is an issue with the integration of SQL Server 2008 Express Edition ans Visual Studio 2008 if you are running on an x64 system (the OS itself! Not runing 32-bit Vista on a Core 2 Duo) (although some report this bug on a 32-bit OS).

This issue is currently unresolved, but you still can open the database in the server explorer if (and only if) you modify the connection string NOT to use a User Instance.

You can do this by rightclicking the database in the server explorer, modify connection and set ‘User Instance’ to ‘False’ (last item)

User Instances are not allowed anymore since SQL Server 2008, as described here.

In order to make it work as it should be you can either install a 32bit SQL Server 2008 Express (which I do) or do as described here.

Update: Back to SQL Server 2005 Express Edition, all the rest fails as described.

SQL Server Management Studio Express 2005 Security Error (29506)

Should you ever want to install SQL Server Management Studio Express 2005 you MUST run it as Administrator.

When you try to install it on Vista (by double clicking the .exe) it extracts the files to a temp folder.

Then it launches the setup, asking you some questions. After that UAC asks you to gain Administrative access.

No problem so far, but at the end the installer notifies you of error number 29506, it cannot modify the security properties.

Solution is described here, run the installer as Administrator from cmd.