miércoles, 18 de noviembre de 2009

Installing the AdventureWorks database

Technology: SQL Server 2008

Problem: Install the AdventureWorks database in order to get test information. The AdventureWorks are sample databases provided by Microsoft, and they can be downloaded from codeplex here: http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=MSFTDBProdSamples&DownloadId=86538&FileTime=128993370526430000&Build=15987

Solution:

In this zip you get all the information you need to create a SQL Server 2008 database with sample data: http://msftdbprodsamples.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34032#DownloadId=86540

After downloading the zip, you have to run the script named instawdb.sql . To do this, you need to:

1) 1. Run it in the SLQCMLD Mode. You do this in SQL Server Manager, by selecting Query -> SLQCMLD Mode

2) 2. Enable FILESTREAM. Here it is explained how to achieve this:

http://techpunch.wordpress.com/2008/08/29/how-to-enable-filestream-feature-on-sql-server-2008/

3) 3. Make sure the process has access to the folder in which the MDF file is going to be created (you assure this by giving to everyone group the full access permission to the folder in which the MDF is going to be created– you could remove this afterwards)

4) 4. Uncomment the setvar lines, and make sure that both paths are correctly set with the information that is in the .zip:

:setvar SqlSamplesDatabasePath "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\"

:setvar SqlSamplesSourceDataPath "C:\Program Files\Microsoft SQL Server\100\Tools\Samples\"

viernes, 13 de noviembre de 2009

Save/Read a file from Silverlight 3.0 in the client machine

Problem: For security reasons, it is not allowed to save a file in any part of the client’s file system using FileStream class or similar. To achieve this, you can either use IsolatedStorage, or the Save/Open File Dialogs added in Silverlight 3.0 version.

Solution 1: Using Isolated Storage

Isolated Storage allows you to read/write files in the client machine, but on a folder Silverlight designates. This could be useful to implement a disk cache, for instance.

The folder in which the file is stored is not intended for the user to access. Its name is something like this: AppData\LocalLow\Microsoft\Silverlight\is\ngns4lzo.xrg\24n0wuei.lxq\1\s\s03b4fsd1eqjjyya0pbbphul0qd3a5mowxinuc0cprdrsg1fp0aaacga\f

Usage example:

IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

IsolatedStorageFileStream fs = new IsolatedStorageFileStream("tst.txt",FileMode.Create,FileAccess.Write, FileShare.ReadWrite, file);

IsolatedStorageFileStream inherits FileStream.

Solution 2: Using SaveFileDialog / OpenFileDialog (available from Silverlight 2.0/3.0 version)

In order to save or read files from any given location in the client machine, you need to use SaveFileDialog / OpenFileDialog. These are used in a similar way to the Windows Forms open/save dialogs.

With both dialogs, the only available operations are OpenDialog and a Stream property to which you could write or read.

Limitations of this solution: Because of security reasons, the dialog can only be initiated by the user.Therefore Open/Save dialogs can only be called from event handlers such as button clicks.

You cannot debug the method in which the open or save file dialogs are opened, and you cannot open two file dialogs on the same handler, otherwise you will get this error: “Dialogs must be user-initiated”


jueves, 15 de octubre de 2009

Implementing CustomVisualStateManagers to see in which visual state is your object

Technology: Silverlight 3.0

Problem: I need to know which method from Silverlight framework changes the VisualState of a control and when.

Solution: Develop a Custom VisualStateManager and override the GoToStateCore method (which is the only method that can be overriden), doing a Debug.WriteLine with Debug information. I decided to write the information on debug instead of setting a breakpoint, because the breakpoint might alter the behavior of the VisualStateManager. Another thing to remember is to call the base method when overriding GoToStateCore.

In order to set the CustomVisualStateManager as the default VisualStateManager to handle the changes, you can set the XAML in this way:

You could also set it through the static method: VisualStateManager.SetCustomVisualStateManager(FrameworkElement,VisualStateManager);

martes, 13 de octubre de 2009

Silverlight 3.0 - LostFocus in Popup Controls

Technology: Silverlight 3.0

Problem: I want to close a Popup when a Custom Control that contains the popup losses focus.

Considerations:

LostFocus is a Routed Event

This means that if a child triggers a lostFocus event, it will bubble up to the parent. I don’t think this makes too much sense because if a focus changed from a child to another child of the same parent, then the parent should not have a LostFocus event triggered (because the focus is still in one of its children)…but that is how it works.

LostFocus and the Popup control

When one of the Children is a popup, the LostFocus event won’t bubble up to Popup the container (This happens for the LostFocus and for other routed events as well).

Example: If I click on tst2.1, and then on tst2.2, the event LostFocus will be triggered for the stack panel SP2, but it won’t trigger for SP1.


Solution:

There are two things to address, 1) The fact that the LostFocus event will not get to the parent control, and 2) The fact that lostFocus being triggered does not mean that the control lost focus (but one of its children did).

To solve 1), have a panel container inside the Popup, and register to the lostFocus event in that control. You know that all the lostFocus for the popup children will trigger the container LostFocus.

To solve 2), in the LostFocus event for your control, check if the current focused element (FocusMager.GetFocusedElement) is inside your control. You can do this using VisualTreeHelper class.

martes, 6 de octubre de 2009

Binding validation failed because the binding's MsmqAuthenticationMode property is set to WindowsDomain but MSMQ is installed with Active Directory in


Technology: Message Queues with WCF

Problem: After implementing a MQ service with WCF I got the error "Binding validation failed because the binding's MsmqAuthenticationMode property is set to WindowsDomain but MSMQ is installed with Active Directory integration disabled."

Solution: In this case I did not need security, so I just configured the "Security" TAB in a custom binding configuration, setting MSMQProtectionLevel to None and MSMQAuthenticationMode to None.

martes, 15 de septiembre de 2009

Type 'Microsoft.VisualBasic.PowerPacks.LineShape' is not defined.

Technology: Microsoft Visual Basic (migration from VB6.0 to .NET)

Problem: After migrating a VB6.0 to a .NET one, I get the error "Type 'Microsoft.VisualBasic.PowerPacks.LineShape' is not defined". in a machine that does not have the SP1 of VS 2008. After installing power packs 3.0 and restarting the app, I still get the same error

Solution: Download and add reference to power packs 2 ( Microsoft.VisualBasic.PowerPacks.dll ) aparently power pack 3 does not sustitute power pack 2

viernes, 11 de septiembre de 2009

Linq To SQL: Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

Technology: LinQ to SQL

Problem: When inserting a record using LinQ to SQL in a table that has properly defined a column with an autogenerated ID, on submit linq to sql will thow the error:

Linq To SQL: Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

This problem appeared when i was trying to delete all records of a relationship, and adding new ones.
The relationship table had an Id column, which was not set

Solution:
Apparently, linq to sql "mixed" the old records that i deleted with the new ones, so I solved the problem calling "submitChanges" after deleting the records of the relationship and before adding the new ones.