Javascript scope in for loops

In JavaScript, you don’t need to define your variables.  You can just up and use any variable as you see fit, like this:

asdfasdf = 'asdfasdf';

However, these variables will then be created in a global scope, which is bad.

I came across a bug today that involved for loops and globally undefined variables.  It’s not as clear what is happening when the counter variable is first used inside the for declaration, but the result is the same: a global variable:

function test2() {
     for (i = 0; i < 10; i++) {
         alert(i);
         test3();
         alert('now i is ' + i);
     }
     alert('done');
 }

 function test3() {
     for (i = 0; i < 10; i++) {
         // do whatever here
     }
 }

In this example, test2()’s for loop will only run 1 time, because test3’s loop ends up using the same variable.

To fix this problem you need to define your looping variables:

for (var i=0; i < 10; i++)

This is a best practice that you should remember to do.

Comcast turns off your internet

Last night, all of a sudden, my internet connection went down.

After a while, all my web requests started getting directed to a comcast page that said:

“You will need to activate your account”

telling me that I needed to download some software.  Total BS.

The link for the software didn’t even work.  I tried rebooting the modem, the router, nothing worked.

So after an hour in a comcast support chat (using my EVDO card for internet) they finally added my modem back to their system (or whatever).

If you get this message, and you are not a new customer, either comcast f’ed up (which is likely because they are possibly the worst company on earth) or you didn’t pay your bill.

Skype on Mac OSX

My wife just tried to install Skype on her Mac OSX computer.  Of course it didn’t work.  Nothing ever works on Macs it seems.

After lots of messing around I found some random suggestion on some forum to try this:

1. Quit Skype
2. Go to the folder “ ~/Library/Application Support/Skype/ “
3. Delete the file “shared.xml”
4. Start Skype

Amazingly, this worked.

Disabling SSRS Subscriptions

As far as I could find, there is no easy way to disable (not delete) an SQL Server Reporting Subscription.

We thought we had found a way to do this by changing the schedule to “run once” and set an “End Date” so that it would never run after that date.

This seemed to work, but after a restart of the server this weekend, the job executed on monday morning.

I’m not sure why it happened Monday morning ( as opposed to Sunday morning, as the machine was restarted on Saturday night) or if has anything to do with today being the first of the month, but either way, this didn’t work and people were emailed some useless notice that they didn’t need.

So, if you are using this method to disable your SSRS reports, watch out.

Doing P2V with HyperV

I had recently tried to use my Windows Home Server to restore a laptop image to a VM running inside HyperV. 

In the event that my machine totally died (not just a hard drive issue) I could restore the image to a VM and work from there while I got back on my feet with a new laptop.

Unfortunately I didn’t find any way to make this work.  I ended up doing all this work and in the end, I couldn’t boot.

A disk read error occurred
Press ctrl+alt+delete to restart.

I tried everything I could think of, repairing the MBR, running fixboot, doing a windows repair, restoring the hal dll from i386 etc.  Nothing worked.

So I gave up on using WHS to create an instance of my dev machine, and fell back on just trying to do a P2V of any type.

I came across Disk2VHD, a free Systernals tool.  This tool includes the options to run on the source machine while online, and also provides an option to “Fix” the HAL (hardware abstraction layer).  I think this was part of my original problem (the HAL issue).  My computer was setup to use SATA drivers, and HyperV only supports virtual IDE drivers.  I think (but am not sure) that this is what was causing my problem w/ using the WHS restore.

So anyway, Disk2VHD runs super fast, and pretty soon I had a VHD to try out. 

Now, this VHD didn’t work either, but it would get me further along.  I would see the windows splash screen before the black screen of death.

What followed was a weird set of steps trying to figure out why things wouldn’t work, but in the end, I never ended up needing to disable anything…. so I’m just going to try to write down the steps I took as best I can.

  1. After the first BSOD I booted to safe mode (using F8).
  2. Safe mode worked, and it seemed to find some new hardware, as I would have expected, but I don’t think it found any of the drivers it needed (so maybe I had ejected the virtual windows CD or something).
  3. Booting normally gave the black screen of death again.
  4. I then found this page with instructions on how to replace the HAL.  Really these instructions were how to do Disk2VHD to a working VM, but I had already done most of the steps except the hal replacement.  I’m going to replace those instructions below:
    1. 1)Downloaded Disk2VHD (v 1.3) from sysinternals (free) and unzipped on the laptop
      2)Find a place to store the new vhd – I used a 320GB external USB, but any storage device that pc can see and is big enough to hold your hard disk should work
      3)Run Disk2VHD and input the name and location for the new file
      4)On your virtual host create a new VPC (I use Virtual PC 2007) and add that new vhd you just created as the primary hard disk.
      5)Make sure you have an ISO of WinXP or an actual disk. If using an iso you’ll need to mount it so it can be booted from.
      6)Fire up the new vpc and have it boot from that XP disc.
      7)Let it run setup and then select option 2 (R) to get repair console
      Login in to windows (need admin pw)
      9)Type “cd system32” press enter
      10)Assuming your CD (or mounted iso) is drive D – Type “expand d:i386halacpi.dl_” press enter
      11)Type “copy halacpi.dll hal.dll” press enter and then press y
      12)Reboot
      13)Press F8 to get to safe mode
      14)After the message that new stuff was installed, click OK for reboot
      15)After system boots back up, log in and install VM Additions
  5. IIRC I still got the black screen of death.  It’s possible that at this point behind the scenes it was doing a check of my disck or something, but I can’t tell in the VM if the HDD is going or not so I just assumed it was locked.
  6. I booted to safe mode and used MSconfig to disable everything, and also used /SOS, and some other options.
  7. What followed was just a series of successful boots with me enabling more and more until I had everything enabled and it all still worked, so I’m not sure why I was getting the BlackSOD before I started this set of SafeMode boots.
  8. Another thing I did at some point (maybe more than once) was try to install the VM Integration Services, which says that it’s updating the HAL, so maybe that had something to do with it too.

The next 2 things that I’m going to try are, running a WHS restore to a VM that is already working (I don’t think this will matter, but we’ll see), and 2 trying to use the Disk2VHD on a machine that has a VHD mounted as a secondary drive.  I don’t konw if this is even possible, but I was thinking that I could use WHS to create the VHD, mount the VHD as X: on some machine, and then tell Disk2VHD to create a VHD from X: and instruct it to to the “Fix HAL” thing.  I’m guessing that will not work as well, but we’ll see.

 

Connecting dynamically to mygeneration MyMeta.dbRoot

The amount of time that this cost me is astounding for such a stupid issue.  (And, I’m going to lay the blame fully on whoever wrote the dbRoot object in the MyGeneration project.  Total improper use of properties).

The situation was this: I was trying to dynamically wire up all the plumbing that the mygeneration windows app does for you manually from the ZeusCmd command line.

Doing this would allow me to switch from DB to DB within my code gen application.

Because ZeusCmd runs first (it references my app and passes in what is needed), if you had previously saved a connection in your MyGeneration app, then this connection is used when ZeusCmd runs.  I didn’t want this to happen because as I move from location to location different DB servers are available or not available, so if the last DB server I connected to was at office A, I didn’t want my code gen crashing onload when I am out of said office.

So I ran MyGeneration and changed my connection to be <None> and saved the settings.  Then in my code gen, I tried to manually create the dbRoot object (named meta here) that was previously created for me my ZeusCmd and my saved MyGeneration settings.

Code like this:

meta.Connect("SQL", "Provider=SQLNCLI;Server=cmayt61p;Database=DBName;Trusted_Connection=yes;")

Seemed to do the trick.

However, my codegen started blowing up on my later in the process.  After tracking down the error I find that the mygeneration result column object can’t figure out the .net type that would be the same as a sql server “int”.  Strange, never had these problems before.

A lot of debugging leads me to find that:

meta.DbTarget
meta.Language 

Are both returning “” when before they were returning “SqlClient” and “VB.NET” when I was not dynamically connecting.

So here is where things get bad… anytime I try setting these properties to their values:

meta.DbTarget = "SqlClient"
meta.Language = "VB.NET"

Nothing happens.  The values don’t appear to stick.

Eventually I even start trying to set the LanguageMappingFileName and DbTargetMappingFileName properties as well:

meta.DbTarget = "SqlClient"
meta.Language = "VB.NET"
meta.LanguageMappingFileName = "C:Program FilesMyGeneration13SettingsLanguages.xml"
meta.DbTargetMappingFileName = "C:Program FilesMyGeneration13SettingsDbTargets.xml"

I tried doing these property sets before connecting and after connecting.

I tried returning to having MyGeneration setup the meta object form me (and all these properties have their values) before dynamically connecting (and losing the DbTarget and Language property values).

I even tried creating a brand new dbRoot object, as well as trying to read some hidden values in the IZeusInput object.

But, in the end, it all came down to the this 1 little thing:

You have to set the mapping file properties before you can set the DbTarget and Language properties.

So this will work just fine:

meta.Connect("SQL", "Provider=SQLNCLI;Server=cmayt61p;Database=MSiUpgraded;Trusted_Connection=yes;")
meta.LanguageMappingFileName = "C:Program FilesMyGeneration13SettingsLanguages.xml" meta.DbTargetMappingFileName = "C:Program FilesMyGeneration13SettingsDbTargets.xml" meta.DbTarget = "SqlClient" meta.Language = "VB.NET"

But this won’t:

meta.Connect("SQL", "Provider=SQLNCLI;Server=cmayt61p;Database=MSiUpgraded;Trusted_Connection=yes;")
meta.DbTarget = "SqlClient" meta.Language = "VB.NET" meta.LanguageMappingFileName = "C:Program FilesMyGeneration13SettingsLanguages.xml" meta.DbTargetMappingFileName = "C:Program FilesMyGeneration13SettingsDbTargets.xml"

It just so happened that everywhere that I was testing this stuff in my code, I always set the DbTarget and Language properties first, and they would never stick.

This is not a proper use of properties.  If a property set has conditional logic that will prevent it from setting, there is something wrong here.  At minimum something should happen, an exception being thrown maybe?  The proper thing to do in my mind would be to make DbTarget and Language readonly properties and have functions to set their value, which could return something indicating failure (or throw an exception).

Property gets and sets should do just that, get and set properties.  There shouldn’t be logic in there deciding if and when to actually perform the set that you have coded.  That is the job of a function.

UPDATE: And I discovered as well that you have to connect first before you set any of these values, or none of them will stick.  So you have to 1) connect 2) set the file paths 3) set the language and dbtargets, in that order or nothing works.

Removing all extended properties from SQL Server

About a year ago, we tried using a tool to get our SQL schema into a souce control system.  It was supposed to work like this: every night the process would run and checkin each object as a file into TFS. 

Well, aside from not working, it also added Extended Properties to all our objects in SQL Server. 

Not really a big deal, except when we would run our database comparison tools between production and development these would cause it so that almost everything would appear out of sync.

I ended up writing my own service to check in all our SQL objects into TFS, but we were left with all these Extended Properties all over the place. 

So I finally got around to cleaning them up using the script below:

select 'EXEC sp_dropextendedproperty
@name = ''' + sys.extended_properties.name + '''
,@level0type = ''schema''
,@level0name = ' + object_schema_name(extended_properties.major_id) + '
,@level1type = ''' +
    CASE WHEN
        xtype = 'U' THEN 'table'
        WHEN
        xtype = 'FN' THEN 'function'
        WHEN
        xtype = 'V' THEN 'view'
        WHEN
        xtype = 'P' THEN 'procedure' end
    + '''
,@level1name = ''' + object_name(extended_properties.major_id) + ''''
from sys.extended_properties
INNER JOIN sysobjects ON sys.extended_properties.major_id  = sysobjects.id
where extended_properties.class_desc = 'OBJECT_OR_COLUMN'
and extended_properties.minor_id = 0
and extended_properties.name like 'VSS%'

This script will produce a record set that you can copy and paste into another query window to run to clean out all the extended properties. 

Note: all the generated extended properties that this tool created on our DB started with “VSS” so that is why I have that extra item in my where clause.  You’ll probably want to remove that.

Tricking the HP WHS update to run on original WHS machines

Here is the article on how to install the update on the EX47X machines.

When HP released the HP MediaSmart Server 2.5 Update for the EX48x machines, they also [1] announced the full software would not be available to first generation users due “to hardware differences” anhd “the underlying software architecture”. Based on that statement, Nigel Wilks (Cougar) and Alex Kuretz (Yakuza) started work on a way[2] to make the update available to EX47x owners, dubbed “SanEncore” after the code names for the EX47x (San Juan) and the EX48x (Encore).

Cougar and Yakuza have also developed a Windows Home Server Add-In to fake the Update package into thinking it is running on a 2.1 machine, and handles some of the missing configuration items so that the 2.5 update is sucesful.