Silverlight: Solution to System.ExecutionEngineException

Posted on Wednesday, December 31, 2008

Today I ran into the charming System.ExecutionEngineException while trying to display a JavaScript alert using HtmlPage.Window.Alert(message). I was able to correlate the throwing of the exception to when I tried to display the alert in the handler of a KeyDown event leading me to suspect a threading issue. The solution I discovered was to asynchronously call the HtmlPage.Window.Alert method from the main thread using the Dispatcher.BeginInvoke() method. Specifically:
((Page)Application.Current.RootVisual).Dispatcher.BeginInvoke(new Action<String>(HtmlPage.Window.Alert), message);

 

Sql Server: Delete/Remove/Clean Up Backup History Script

Posted on Monday, December 29, 2008

If you have suddenly discovered that your msdb database is gigabytes large due to the storage of backup/restore history, you may be tempted to use sp_delete_backuphistory to trim down the history. Do not be tempted! sp_delete_backuphistory is poorly written and will often take days to finish if you have not or seldom maintain your backup history. After seeing logs indicating that sp_delete_backuphistory had run for 100+ hours before ultimately failing, I put together a backup history maintenance script that cleaned up the million plus rows in each of the backup history tables in just over an hour. Also, feel free to try the other alternative sp_delete_backuphistory script that I found.
declare @DaysToRetain INT
set @DaysToRetain = 7

SET NOCOUNT ON

--delete restorefile
delete restorefile from restorehistory rh
join restorefile rf on rh.restore_history_id = rh.restore_history_id
where rh.restore_date < GetDate()- @DaysToRetain

--delete restorefilegroup
delete restorefilegroup from restorehistory rh
join restorefilegroup rfg on rfg.restore_history_id = rh.restore_history_id
where rh.restore_date < GetDate()- @DaysToRetain

--delete restorehistory
delete restorehistory 
where restore_date < GetDate()- @DaysToRetain

--delete backupfile
delete backupfile from backupset bs
join backupfile bf on bf.backup_set_id = bs.backup_set_id
where bs.backup_finish_date < GetDate()- @DaysToRetain

--delete backupset
delete backupset
where backup_finish_date < GetDate()- @DaysToRetain

--delete backupmediafamily
delete backupmediafamily from backupmediaset bms 
left join backupset bs on bms.media_set_id = bs.media_set_id
join backupmediafamily bmf on bmf.media_set_id = bms.media_set_id
where bs.backup_set_id is null

--delete backupmediaset
delete backupmediaset from backupmediaset bms 
left join backupset bs on bms.media_set_id = bs.media_set_id
where bs.backup_set_id is null

SET NOCOUNT OFF

 

Multiple submit buttons on a .NET page

Posted on Wednesday, December 24, 2008

Since .NET pages can only have one managed <form> element, some problems arise when pressing enter to submit a login request on a page with a search bar and a login form. Since the ASP button controls convert to submit html inputs, the browser can not determine that because the user hit enter after typing in their password in the login form, that the submit button for the login form should be pressed instead of the search button for the search bar. One solution to this problem is to make the search bar using actual html elements instead of ASP controls and then use javascript to grab the search query and put it into a get request made by changing window.location. My solution was to whip up some javascript to determine the correct submit button to press using proximity of the text field that had focus when the enter key was pressed to the submit button using the DOM model of the page. Here is what I came up with:
<script type="javascript">
// SubmitManager
var activeObject = "";
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
function FindSubmitButton(element,down)
{
var found = false;
if(element.hasChildNodes()){
for(var i in element.childNodes){
if(element.childNodes[i].nodeName != undefined){
if(element.childNodes[i].hasChildNodes()){
found = FindSubmitButton(element.childNodes[i], true);
if(found){
break;
}
}
else{
if(element.childNodes[i].type == "submit"){
found = element.childNodes[i];
break;
}
}
}
}
}
if(!down){
if(found == false){
if(element.nodeName != "#document"){
return FindSubmitButton(element.parentNode, false);
}
else{
return null;
}
}
else{
return found;
}
}
else{
return found
}
}
function ManageKeyPress(e)
{
var code;
var allow = true;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if(code == 13){
allow = false;
var button = FindSubmitButton(activeObject, false);
if(button != null){
button.click();
}
}
return allow;
}

function InitializeManager(){
elements = document.getElementsByTagName("INPUT");
for(var i in elements){
if(elements[i].type == "text"){
elements[i].onfocus = function(){
activeObject = this;
}
elements[i].onkeypress = ManageKeyPress;
}
}
}
addLoadEvent(InitializeManager);
</script>
The SubmitManager can be included right into an existing page without any extra work, but it would probably be best to put the code into a .js file and then include it that way:
<script type="javascript" src="SubmitManager.js"></script>