Thursday, February 24, 2011

Windows 7 and Program Files

With Windows 7, if you think you are putting something in the Program Files directory, you may actually be storing the file somewhere else.   Windows virtualizes this directory so that the files are stored in another location.

For example, if you drop an Access database into this location, and the Access database is updated, the updated copy will be stored in c:\Users\UserName\AppData\Local\VirtualStore\ProgramFiles.  All future references to this file will be virtualized to this location.  If multiple users are on the machine, they will each have their own copy of this directory, and a ‘Shared’ Access database will no longer be shared.

Wednesday, February 23, 2011

401.2 Error on IIS7

I was getting a 401.2 error on a website that I had moved to IIS7.  The website required Windows Authentication.  To resolve the issue:

  • Make sure the website users have access to the underlying files.
  • In the Application setup in the IIS console
    • Go to Basic Settings for the Application (in the right pane), and click on ‘Connect As …’.  Selection ‘Application User’.
      image
    • Click the Authentication icon in the IIS section.
    • Verify that ASP.NET Impersonation and Windows Authentication are both enabled. 
      image

Friday, February 11, 2011

Alternating Background Colors in a Tablix Group

When alternating background colors in a group in a tablix, you cannot use the traditional formula for alternating rows (which would be something like: =iif(RowNumber(Nothing) Mod 2, "White", "#E5E5E5")  )

Instead you must use the following:

  1. To go the Code for the report (found in the Report Properties dialog)
  2. Add an even number function, and an internal variable to track the current row:
     Public _evenRow As Boolean  
    Public Function EvenRow() As Boolean
    _evenRow = Not _evenRow
    return _evenRow
    End Function

  3. Go to the “Group Properties” for the Row Group that you want to alternate, go to variables, and add a variable called EvenRow. The expression for this variable should be =Code.EvenRow().  This will be executed each time the group is created.
  4. Finally, in the background property for the group row, you can enter a modified version of the original expression: =IIF(Variables!EvenRow.Value=true,”Red”,”Green”)

Tuesday, January 25, 2011

Fixing a Broken Event Broker Queue

When a database is restored from a backup, a lot of times the Service Broker is disable. Therefore any actions scheduled by the Event Broker will not happen.

To check if an Event broker is enabled:

 SELECT is_broker_enabled, [name], database_id FROM sys.databases WHERE database_id = db_id()  

To reenable the broker:

 ALTER DATABASE NameOfTheDatabase SET ENABLE_BROKER 

Saturday, January 15, 2011

Retrieving Identities on Bulk Inserts

If you need to insert a bunch of records at one time, and need to get the identity of all the inserted records, you can use a cursor.  But sometimes, using a cursor just feels wrong.  The “Inserted” key word comes to the rescue!  Use it like the following.  In SQL2008 they have further improved this so that you don’t even need the temp table.

     CREATE TABLE #newNotes (ID INT)  
INSERT INTO Note (NoteText)
OUTPUT INSERTED.NoteId INTO #newNotes
SELECT Note.NoteText
FROM Note
WHERE initial_report_number = @initialReportNum
INSERT INTO ClaimNote (claim_number, NoteId)
SELECT @claimNum, #newNotes.ID
FROM #newNotes
DROP TABLE #newNotes

Wednesday, December 29, 2010

Waiting for a Save to Complete in an Async Manner

Silverlight is by its design an asynchronous environment.  But sometimes, you really just need to wait for an action to complete before letting the user move on to any other operations.  Ideally, you want to do this in a manner that does not lock the UI thread so that the app is still responsive.  

The best way to do this is to have the method that is doing the action raise an event when the event is complete.  To do this, first declare the event that will be raised, along with any event arguments you want to include with it.

           #region "Events"  
public class SaveCompletedEventArgs : System.EventArgs
{
public bool SaveSuccessful { get; set; }
}
public event System.EventHandler<SaveCompletedEventArgs> SaveCompleted;
#endregion

Then when the Save is completed raise the event (SaveCompleted in this case).

 _Context.SubmitChanges(  
(submitOperation) =>
{
saveSuccessful = false;
IsBusy = false;
if (!submitOperation.HasError)
saveSuccessful = true;
SaveCompleted(this, new SaveCompletedEventArgs() { SaveSuccessful = saveSuccessful });
},null);

Note that in the code above, when the SubmitChanges is run, the program will continue with the next line and return control to the caller.


In the caller, you would do something like the following to handle the event. Use VS2010 to automatically create the event handler for you.

 _ViewModel.SaveCompleted += new EventHandler<ViewModels.AddShoppingListViewModel.SaveCompletedEventArgs>(_ViewModel_SaveCompleted);  

Monday, November 22, 2010

Divide by Zero Error in ReportingServices

Occasionally you need to do division in a ReportingServices report, for example where you have to calculate percentages.  But when your divisor is zero you get an ugly ‘#Error’ in the field where the division is done. 

Checking for a zero in the divisor seems like an obvious solution, but unfortunately the ‘IIF’ statement that you would use to check for this evaluates the True and False parts of the answer regardless of the result of the result it is evaluating.   So if Y = 0, and your field has the following: IIF(Y=0, 0, X/Y), you will still get the #Error message. 

The fix for this is to put the following code in the report:

  Public Function Divide(ByVal first As Double, ByVal second As Double) As Double  
If second = 0 Then
Return 0
Else
Return first / second
End If
End Function

Then in the field replace the IIF statement with the following Code.Divide (X/Y).


That should take care of it.