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.

Friday, July 30, 2010

Outlook 2010/IMAP/Google Apps

With Outlook 2010 over IMAP to Google Apps/Gmail, the default setting is that when you delete an item, it is moved into ‘Trash’ instead of archived. This is a problem if you count on having those Archived items out there for future reference. (Trash is discarded after 30 days).

To change this:

  • Click on File/Account Settings
  • Doubleclick the e-mail account you want to change.
  • Click More Settings
  • Go to the Delete Items Tab
  • Select '”Move deleted items to the following folder”
  • Select the “All Mail” folder in the Tree View. image
  • Click OK
  • Click Next and let it finish the setup.

That should do it. Now all deleted items will be Archived.

Thursday, July 8, 2010

Restoring a Windows State When Navigating Back to a Page

When navigating to another page and back again using the Silverlight Navigation Framework, sometimes it is useful to return to the page and see it exactly as it was.  Fortunately, this is easy to do if you are using an MVVM framework. 

When navigating away from the page, save the ViewModel in the App.Current.Resources collection.


protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (App.Current.Resources["theViewModel"] != null)
App.Current.Resources.Remove("theViewModel");
App.Current.Resources.Add("theViewModel", this.DataContext);
base.OnNavigatedFrom(e);
}

When returning to the page, pull it back out again and assign it to the page.


         protected override void OnNavigatedTo(NavigationEventArgs e)  
{
if (App.Current.Resources["theViewModel"] != null)
this.DataContext = App.Current.Resources["theViewModel"];
}

Thursday, June 24, 2010

Syntax for programmatically retrieving a recordset from RIAServices

         private ObservableCollection<MyEntity> _MyEntityList;  
public ObservableCollection<MyEntity> MyEntityList
{
get{ return _MyEntityList; }
set
{
_MyEntityList = value;
RaisePropertyChanged("MyEntityList");
}
}

public void LoadData()
{
_Context.Load(_Context.GetMyEntityQuery(), LoadBehavior.RefreshCurrent,
DataLoaded, null);
IsBusy = false;
}

public void DataLoaded(LoadOperation<MyEntity> loadOperation)
{
if (loadOperation.HasError)
{
System.Windows.MessageBox.Show(loadOperation.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
loadOperation.MarkErrorAsHandled();
MyEntityList = null;
}
else
MyEntityList = new ObservableCollection<MyEntity>(loadOperation.Entities);
IsBusy = false;
}