How to execute a sql statement with a parameter in SQL Management Studio?

Today I got asked how to execute a parameterized SQL query in SQL Management Studio.

You can do it two ways:

Method 1 – Use SP_EXECUTESQL stored procedure to execute parameterized SQL

DECLARE @query NVARCHAR(MAX) = N'SELECT * FROM customer WHERE CustomerId = @id'
DECLARE @params NVARCHAR(MAX) = N'@id int';

EXEC SP_EXECUTESQL @query, @params, @id = 3;

If you need to do multiple parameters, it is pretty much the same syntax.

DECLARE @query NVARCHAR(MAX) = N'SELECT * FROM customer WHERE CustomerId IN (@id1, @id2)'
DECLARE @params NVARCHAR(MAX) = N'@id1 int, @id2 int';

EXEC SP_EXECUTESQL @query, @params, @id1 = 3, @id2 = 4;

Method 2 – DECLARE Variables to execute parameterized SQL

DECLARE @id INT = 3;
SELECT * FROM customer WHERE CustomerId = @id;

If you need to do multiple parameters, just declare another variable.

DECLARE @id1 INT = 3;
DECLARE @id2 INT = 4;
SELECT * FROM customer WHERE CustomerId IN (@id1, @id2);

An HTML5 3 column layout example in ASP.NET

A while back I posted about an html5 three column layout. Here is the same layout, only this post has built the web site using ASP.NET and it includes a Visual Studio project.

The _Layout.cshtml file looks like this:

@RenderPage("~/Layout/_Header.cshtml")
@RenderPage("~/Layout/_Nav.cshtml")
<div id="main">
   <article id="main-article">@RenderBody()</article>
    @RenderPage("~/Layout/_LeftAside.cshtml")
    @RenderPage("~/Layout/_RightAside.cshtml")
</div>
@RenderPage("~/Layout/_Footer.cshtml")

Then of course, the HTLM5 layout is in the header, asides, article, and footer. And even though the Article is placed before the asides, the asides can be either on the left or the right.

Download it here: HTLM5_ASPNET_ThreeColumnLayoutExample.zip


How to add a “Start” shortcut in Windows 8 (or Where is start in windows 8)

Too many people are paying for software to provide a complete return the windows Start pop-up menu. Well, Start is still there in Windows 8, it is just a little different. It is not a pop-up menu. It is hidden by default and only shows when you move your cursor to the bottom left corner.

I think many people would be happy if they simply had a shortcut to the Apps screen, which is equivalent to All Programs.

Well, lets create a shortcut and put in on the task bar. Follow these steps.

Note: Alternately, you can download and extract the zip file from here: AllPrograms.zip

Step 1 – Create the Windows 8 Apps Shortcut

  1. Right-click on the Desktop and click New | Shortcut.
  2. Where it prompts you to “Type the location of the item” enter this:
    %windir%\explorer.exe shell:::{2559a1f8-21d7-11d4-bdaf-00c04f60b9f0}
  3. Click Next.
  4. Name the shortcut what ever you want. “Start” or “All Programs” or whatever, it really doesn’t matter.
  5. Click Finish.

Step 2 – Change the Icon for the Windows 8 Apps Shortcut

You may want to change the icon too.

  1. Right-click on your newly created shortcut and choose Properties.
  2. Click Change Icon…
  3. Select a different icon from the list or download your own .ico file and use it.

Step 3 – Drag the Windows 8 Apps Shortcut to the task bar

  1. Right-click on your newly created shortcut and choose Pin to taskbar.
  2. Now if you want this icon all the way on the left, click and drag it there.

Windows 8 Negativity Debunked!

I love BSD and Linux and as an Open Source guy since 2001, many people expect me to hate Microsoft.  Well, guess what? I don’t hate Microsoft at all. I love the technology. I love most technology. So unlike many, I am not religious about my operating system. I am completely happy to spin up a Windows Server where it makes sense and a FreeBSD/Linux server where that makes sense. I am even known to run a PC-BSD desktop and avoid paying for a commercial operating system.

Currently for my desktop, I run Windows 8.

I really, truly enjoy running Windows 8. I am running it on both my laptop and my desktop. I love the Metro interface and find it works quite well for me. I use it to easily find and run my applications, just as I used Start in Windows 7. I love having both a Metro and a Desktop interface as well as having both Metro and Desktop apps.

I hear general complaints on the web.

“I miss the Start bar.”
“It’s harder to launch my apps.”
“I hate the metro interface.”

Really. Why? It is actually easier to use.

“Start is missing.” – Debunked!

Is Start really missing? No. It is there, just it has to pop up. And guess what? It even says “Start”.

Start Icon

The difference between the Start button in previous versions and the Start button in Windows 8 is not a long list.

  1. You don’t see a start button/icon until you move to the cursor all the way to the bottom left corner. However, the image that pops up does say “Start”.
  2. The screen doesn’t pop up over your current desktop view, instead it opens using the full screen.
  3. It shows both desktop apps and metro apps.

Well, are these three differences that big of a deal. Of course not. The first two are not missing features, just a different way to view things. The last one is actually a feature add.

Unfortunately, there are haters out there. Those who are religious about their operating system. Each operating system has those who are nut-job religious. Yes, I’ve seen nut-jobs for Microsoft, Apple OSX and iOS, BSD, Linux, Unix, Android. Heck, I’ve even seen nut-jobs for just one flavor of Linux (Red Hat, Debian, Ubuntu, Fedora, Slackware, Symbios, etc…). So naturally when nut jobs see a little blood in the water they attack.

Can improvements be made? Yes. I see no reason that Start has to be hidden by default. Historically, hiding or showing things has been an option. It is really the lack of the option that is all Microsoft missed. If so, that is not a big deal at all.

“It’s harder to launch my apps.” – Debunked

Windows 8 is the operating system for launching Windows applications. Historically, you have been able to launch apps in the following manner:

  1. Directly click the executable.
  2. Click a shortcut you can place anywhere (folder or desktop or Start bar)
  3. Click Start then move the cursor to All Programs, move the cursor again, and then scroll through All Apps.
  4. In Windows 7, they added the feature to click Start, just type in the first part of the program and the search would find the application for you.

Has anything changed in Windows 8. No, it hasn’t. I can still do all three with Windows 8.  What is different.

  1. Clicking Start is displayed full-screen.
  2. Getting to All Apps doesn’t require a lot of scrolling, instead it is Click Start. Right-click and choose All Apps.

These again are not feature losses but simply different views.

Go ahead and click Start, type in “No” and see that Notepad immediately shows up.

Go ahead and click Start, type in “Cont” and see that Control Panel immediately shows up.

It functions the same as Windows 7.

Is there room

“I hate the metro interface.” – Not debunked!

So let’s start with the fact that generic statements like this are just opinion and too general to really do anything about. What do you hate? Do you hate the full screen? Do you hate the way Metro Apps are opened? Lets take this to a more granular level.

“I hate seeing the full screen when I click start.” – Just opinion.

Well, this is opinion. No one can debunk or not debunk this. Personally, I don’t care either way. It works well for me, so I have no complaints.

Is there room for improvement? Yes.

I would like an option to see this in full-screen or in a window. Then those that hate it opening as full screen don’t have to have it open as full screen.

I would also like to be able to set whether to see the Start screen or the Apps screen is displayed when I click Start. However, this feature is easy to add using a shortcut. I will link to a post on how to create a shortcut to the Apps screen. You could even name the shortcut Start and put in on the bottom left.

“I hate that opening a metro app takes the whole screen”  – Not Debunked a real problem but fixable

Yes, metro apps take up the whole screen. Worse, some things, like pdf files, open in a metro app by default. I had to install Acrobat Reader to make this go away, but only for opening pdf files. Even worse is trying to figure out how to close a metro app for the first time.

This is really the only thing that bothers me about Windows 8. When I launch a metro app, it opens full screen and I have no option to make it just a window. The Metro app experience is not really that great for a desktop/laptop.

I want all my windows to be re-sizable and I want to have multiple windows up at once. So should Metro and Desktop modes be completely separated. Of course not. There is only one feature that is annoying. The fact that metro apps open full screen. Simply add an option to open metro apps in a window and now everything is fixed.

This is fixable. I may write a post on how to resolve this later. If I do, I will link to the solution here.

Conclusion

There was only one real problem, in that metro apps open full screen and you can’t run multiple apps at once. The rest are just options people want. If Microsoft makes these changes, the major complaints are resolved:

  1. Allow an option to hide or display the Start icon.
  2. Allow Start and Apps metro screens to be opened in windows.
  3. Allow Start to be configurable to either open Start or Apps metro screens.
  4. Allow a setting to make metro apps to be opened in windows and make it the default setting on desktops and laptops.

Wow! A brand new interface and really four features resolve the world’s major complaints, most of which are not really that big of complaints in the first place.

We haven’t yet discussed the new features in Windows 8 yet, such as Hyper-V, which saves me a lot of money on a separate VMWare Workstation license.

My Recommendation

Take another look at Windows 8!


Android Virtual Device (AVD) extremely slow

So I started to look at writing an Android app. I took a class on Android and wrote a prototype Android App in fall of 2011. Well, I have a need to write an Android App again.

I am a C# developer and while I am comfortable with Java, I prefer C# so I am going to use MonoDroid.

I installed everything very easily using the MonoDroid installer. However, when I went to launch an Android Virtual Device (AVD) it seemed to start but even after leaving it over night, it didn’t finish booting.

So I did some research and the recommendation was to install the Hardware Accelerated Execution Manager.

So I had to:
1. Open the Android SDK Manager and install the Intel x86 Emulator Accelorator (HAXM).

2. Run the installer I found here:
C:\Users\jbarneck\AppData\Local\Android\android-sdk\extras\intel\Hardware_Accelerated_Execution_Manager\IntelHaxm.exe

After that, opening an AVD was plenty fast.


Using knockout.js and MVVM with RadioButtons

In the example below, radio buttons selection list is created by binding to a list in the view model. I show how to bind to both the selected radio button’s value and name. I also show how to disable a button unless the radio button is selected.

<!-- Step 1 - Create the HTML File or the View -->
<!DOCTYPE html>
<html>
<head>
    <!-- Step 2 - Include jquery and knockout -->
    <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="scripts/knockout-2.2.1.js"></script>
    
    <!-- Step 3 - Add script to run when page loads -->
    <script type="text/javascript" >
        $(document).ready(function(){

			<!-- Step 4 - Add a RadioButtonModel -->
            function RadioButtonModel(inText, inValue, inGroupName) {
                this.text = inText;
				this.value = inValue;
				this.name = inGroupName;
            }

			<!-- Step 5 - Create ViewModel -->
            function SurveyViewModel() {
                var self = this;
                
				<!-- Step 7 - Create an observable list -->
                this.list = ko.observableArray([
                    new RadioButtonModel("Good", 10, "Q1"),
                    new RadioButtonModel("Average", 5, "Q1"),
                    new RadioButtonModel("Poor", 1, "Q1")
                ]);
				
				<!-- Step 8 - Create a selected item -->
				this.selected = ko.observable(0);
				
				<!-- Step 9 - (Optional) Create a computed value to get the selected text -->
				this.selectedtext = ko.computed(function() {
					var text = "";
				    ko.utils.arrayForEach(this.list(), function(item) {
						var selectedItem = self.selected();
						if (selectedItem == item.value)
							text = item.text;
					});
					return text;
				}, this);
				
				
				<!-- Step 10 - Create a computed value to require a selection before submitting -->
			    this.canClick = ko.computed( function() {
                    return this.selectedtext() != "";
                }, this);
                
            }
            
            <!-- Step 10 - Create a computed value to require a selection before submitting -->    
            ko.applyBindings(new SurveyViewModel());
        });
    </script>
</head>
<body>
    <!-- Step 11 - Create a div containing the html for one radio button and bind to foreach: list -->
    <div data-bind="foreach: list">
        <input type="radio" data-bind="attr: {name: name}, value: value, checked: $root.selected" /><span data-bind="text: text"></span>
    </div>

    <!-- Step 12 - Add html elements to see other properties -->
    <p data-bind="text: selected"></p>
    <p data-bind="text: selectedtext"></p>
    
    <!-- Step 13 - Add a button and bind its enable state -->
    <button type="submit" id="btnSubmit" data-bind="enable: canClick">Submit</button>

</body>
</html>

Hope this helps you.


Using MVVM with HTML5 and JavaScript

The MVVM pattern is an excellent pattern for separating the UI or View and the Code or ViewModel and Model.

With Knockout.js the MVVM pattern can be used with HTML5.

Previously I wrote this post: Using jQuery to enable/disable a button based a text box

This same thing could be done using binding and the MVVM pattern, with jQuery and Knockout.js.

Here is a single file example of using knockout.js and with databinding.

<!-- Step 1a - Create the HTML File or the View -->
<!DOCTYPE html>
<html>
<head>
    <!-- Step 2 - Include jquery and knockout -->
    <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="scripts/knockout-2.2.1.js"></script>
    
    <!-- Step 3 - Add script to run when page loads -->
    <script type="text/javascript" >
        $(document).ready(function(){
        
            <!-- Step 4 - Create a ViewModel -->
            function BasicViewModel() {
            
                <!-- Note: In this case the model is just a string and a bool -->
                <!-- Step 5 - Create a text property to bind the text to -->
                this.text = ko.observable('');
                
                <!-- Step 6 - Create a bool property to bind the button enabled state to -->
                this.canClick = ko.computed(function() {
                    return this.text() != null && this.text() != '';  
                    }, this);
                }
          
            <!-- Step 7 -  Activates knockout.js bindings -->
          ko.applyBindings(new BasicViewModel());
        });
    </script>
</head>
<body>

    <!-- Step 8 - Create a HTML Elements with bindings -->
    <input data-bind="value: text, valueUpdate:'afterkeydown'" />
    <button type="button" id="btnEnter" data-bind="enable: canClick">Enter</button>
</body>
</html>

Anyone who knows MVVM will surely realize that while this example is all in a single file, it is likely that you will have a separate viewmodel javascript file in a production environment.


Support Tools for LDMS 9.5 Released

Support Tools for LDMS 9.5 Released

Go here for more information:

LANDesk Support Tools


SQL INSERT statement syntax is poorly designed

Am I on the only in the technical community that is completely annoyed by SQL’s INSERT statement syntax? Especially with long INSERT queries.

Why in the world do we have to have the column and its value separated.

Imagine this table, which is an overly simplified example only version of a Person table.

FirstName MiddleName LastName Birthday UserName Password PhoneNumber Address SignupDate LastUpdated LastPasswordChange

Please not that this table only has 10 columns. I am working with tables with three of four times as many columns. Also these columns are pretty simple. Many columns have name like PK_D or other cryptic titles.

So wouldn’t it be nice to have a syntax that more easily connects the column and the value?

Lets look at the insert syntax:

INSERT INTO Person (FirstName, MiddleName, LastName, Birthday, UserName, Password, PhoneNumber, Address, SignupDate, LastUpdated, LastPasswordChange)
VALUES ('John', 'J.', 'Doe', '3/27/1977', 'jdoe', 'p@ssw0rd!, '555-555-5555, '1234 ABCD Street SomeCity, Utah 81234', '12/1/2012', '3/20/2013', '3/13/2013')

Ok, so you can start to see how even with these easy and well-known columns, it starts to get confusing as to which order you need to enter data. Also, you have to scroll right to left a lot. Also, in whichever order you type your column names, you have to enter you data.

Even with every value on its own line, this is still not very clear.

INSERT INTO Person (
FirstName, 
MiddleName, 
LastName, 
Birthday, 
UserName, 
Password, 
PhoneNumber, 
Address, 
SignupDate, 
LastUpdated, 
LastPasswordChange)
VALUES (
'John', 
'J.', 
'Doe', 
'3/27/1977', 
'jdoe', 
'p@ssw0rd!, 
'555-555-5555, 
'1234 ABCD Street SomeCity, Utah 81234', 
'12/1/2012', 
'3/20/2013', 
'3/13/2013')

Now it is a lot of lines, and you have just swapped extensive left and right scrolling to up and down scrolling. Remember, this is a small table. Some tables have many times more columns.

Of course you can get around some of this using default values and allowing NULLs so you don’t have to insert values for those columns. But if you have 30+ required columns, which the database I am working with has, then that doesn’t help. No, I am not in control of the database or I would likely change it.

Why is the syntax not something that is order insignificant?

INSERT INTO Person
FirstName='John', MiddleName='J.', LastName='Doe', Birthday='3/27/1977'
UserName='jdoe', Password='p@ssw0rd!, PhoneNumber='555-555-5555,
Address='1234 ABCD Street SomeCity, Utah 81234', SignupDate='12/1/2012',
LastUpdate='3/20/2013', LastPasswordChange='3/13/2013'

Or all on its own line:

INSERT INTO Person
FirstName='John', 
MiddleName='J.', 
LastName='Doe', 
Birthday='3/27/1977'
UserName='jdoe', 
Password='p@ssw0rd!, 
PhoneNumber='555-555-5555,
Address='1234 ABCD Street SomeCity, Utah 81234', 
SignupDate='12/1/2012',
LastUpdate='3/20/2013', 
LastPasswordChange='3/13/2013'

Is it just me who thinks that the standard property=value syntax is a quite a bit more readable and a lot more writable?

I am sure there are all kinds of reasons why this syntax isn’t used. I am not saying property=value syntax is a fix to the horrible INSERT syntax. I am just saying that the INSERT syntax is horrible and I would recommend that future version of SQL provide and more readable a more writable alternative.


How to export table data into a SQL insert script?

Have you ever had a table that you wanted to drop and recreate but you need all the data inside it. Perhaps it took you a lot of time to generate this data.

Well, you can save the data out as a SQL insert script if you want. Here is how:

  1. Open Microsoft SQL Server Management Studio and login.
  2. Right-click on the database and choose Tasks | Generate Scripts.
    Note: The first time you do this you will see and Introduction page. Click Next. You can click the check box next to Do not show this page again and you won’t see this screen.
  3. On the Choose Objects screen, click Select specific database objects.
  4. Expand Tables and click the check box next to the table you want to save and click Next.
  5. On the Set Scripting Options screen, click Advanced.
  6. Scroll down and under Types of data to script select Data only and click OK.
  7. Under File name select a file name to save to and click Next.
  8. Click Next, Next, Finish.

You now have a script to insert your data.


Sharing checked out source between Visual Studio instances on Virtual Machines using a network share

I have a lot of Virtual Machines and having to checkout a large code base from source control to each box is annoying and time consuming. There are a couple of branches that take almost an hour to get the latest code.

I am using Visual Studio and TFS. Visual Studio and TFS is installed on all my VMs. So I set out to have the VM host store the source and then all the Virtual Machines map a drive to the host.

At first this appeared to work just fine, but then I started to run into errors. I fixed the errors one at a time.

There were a few things I had to do to make this work and I thought I would share them:

System Changes

  1. Map the drive and make it persistent. That means the drive stays mapped after reboot.
  2. Add the mapped drive as a local intranet site in Internet Explorer’s Security settings.

Visual Studio Changes

  1. Make sure to always log into to TFS in Visual Studio with the same user account. There will be TFS problems if you use a different user account.
  2. Add the loadFromRemoteSources tag to the section of the devenv.exe.config file and set it to true:

    Note: I had to run Notepad++ as administrator to make this change.

    Visual Studio 2010:
    C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.configVisual Studio 2012
    C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe.config

  3. Add the loadFromRemoteSources tag to other .config files.
    Note: In order to debug a web service, I had to also add this tag to the  WcfSvcHost.exe.config.

    Visual Studio 2010:
    C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\WcfSvcHost.exe.config

    Visual Studio 2012
    C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\WcfSvcHost.exe.config

    In that IDE folder there were a 22 .config files in VS 2010 and 32 in VS 2012. I am not sure which ones will ever need this. There are two schools of thought: 1. Add the loadFromRemoteSources tag to all of the config files or 2. Add them only when you encounter and error. Since I am in a lab environment and everything is on one physical box (the host and the hosted VMs) I went ahead and added this to all the config files.

  4. Sometimes when running Visual Studio as administrator, the mapped drive appears off line. Simple click File | Open | File and browse to the network drive. You don’t have to actually open anything. Just click cancel. It seems as soon as you browse to the mapped drive, Visual Studio knows about it and everything works fine.This doesn’t seem to work as well in VS 2012 on Windows Server 2012.  You may even have to have the drive mapped twice. Once as the logged in user and once as the Administrator. To do this open two command prompts: 1. A regular command prompt. 2. A command prompt running as Administrator. Run the mapped drive command in both: (Yes, use the same drive letter in both on Windows 2013 or Windows 8)

    net use z: \\pcname.domain.tld\yourshare /PERSISTENT:YES

Be aware you’ll get more prompts

I noticed than when running executables orVBScripts or PowerShell scripts, that I was prompted. This is because your running them from a network share, which is completely fine as long as you trust the network share.

Conclusion

Having all your Virtual Machines share the same source repository is possible and saves a lot of time checking out code as you don’t have to do it an each Virtual Machine.

Doing this for multiple users did not work well as the we used TFS and the source seemed to be tied to the user.

I have not yet tried to share between VS 2010 and VS 2012. I hope that works…I’ll update when I know.


Creating a QUnit Test Project in Visual Studio 2010

You may want to Unit Test your JavaScript code and one library to do this with is QUnit.

QUnit is based on the idea of opening a web page to run your JavaScript tests. So here is how I create a new QUnit project in Visual Studio.

This article assumes that you have the following installed already:

  • Visual Studio 2010 or later
  • The NuGet Add-in for Visual Studio

Step 1 – Create a Visual Studio Project

  1. Open Visual Studio.
  2. Go to File | New | Project.
  3. Select ASP.NET Empty Web Application.
  4. Give the project a name:
    Note: For this walk-thru, I named mine QUnitExample
  5. Click OK.

Step 2 – Import QUnit

Method 1 – Using NuGet

This step requires internet access.

  1. Right-click on your Project and choose Manage NuGet Packages.
  2. Search for QUnit and locate QUnit for ASP.NET MVC.
  3. Click Install next to QUnit for ASP.NET MVC.
  4. Click Close.

You should now see a folder called Content with a QUnit.css file and a folder called Scripts with a QUnit.js file.

Note: The NuGet package had old files, so I had to update them manually following Method 2.

Method 2 – Just downloading the files

If the NuGet package is not up to date, you may want to download the files manually. Follow the steps above to get QUnit through NuGet and if it doesn’t work, download the latest files from the QUnit web site and replace the ones NuGet added to your project with the latest ones.

Step 3 – Create an HTML file to display test results

  1. Right-click on the project and Choose Add | New Item.
  2. Find and choose HTML Page.
  3. Give the page a name.
    Note: I named mine QUnitTestResults.htm.
  4. Open the QUnitTestResults.htm file for editing.
  5. I made the DOCTYPE tag simpler in line 1:
  6. Enter something in the Title: Line 4.
    Note: I called mine QUnit Test Results.
  7. Add a Link to the qunity.css file: Line 5.
  8. Add a script in the body that calls the qunit.js file: Line 8.
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>QUnit Test Results</title>
  <link rel="stylesheet" href="/Content/qunit.css">
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="/Scripts/qunit.js"></script>
  <script src="/Scripts/tests.js"></script>
</body>
</html>

Step 4 – Create your first QUnit Test

  1. Right-click on the Project and choose Add | New Folder.
  2. Rename the folder to TestScripts.
    Note: I like to keep the test scripts separate from the other scripts.
  3. Right-click on the TestScripts folder and Choose Add | New Item.
  4. Find and choose JScript File.
  5. Give the JavaScript file a name.
    I named mine ExampleTests.js.
  6. Open the ExampleTests.js file for editing.
  7. Add a test method to the ExampleTests.js file.
test("hello test", function () {
    ok(1 == "1", "Passed!");
});

Note: This test is straight from the QUnit home page.

Step 5 – Add the Tests to your HTML file

  1. Open the QUnitTestResults.htm file for editing.
  2. Add a script in the body that calls the ExampleTests.js file: Line 9.
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>QUnit Test Results</title>
  <link rel="stylesheet" href="/Content/qunit.css">
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="/Scripts/qunit.js"></script>
  <script src="/Scripts/tests.js"></script>
  <script src="/TestScripts/ExampleTests.js"></script>
</body>
</html>

Step 6 – Run the tests

  1. Right-click on the QUnitExample project and choose Set as StartUp Project.
  2. Right-click on QUnitTestResults.htm and choose Set As Start Page.
  3. Click Debug | Start Debugging.

Step 7 – Start testing your JavaScript files

Now you probably want to recreate this project in as an additional project to your production solution (if you haven’t done this already).

It is time to start testing your JavaScript file. If you add the script to your project as a link, then you don’t have to maintain two copies of it.

  1. Right-click on the Scripts folder and choose Add | Existing Item.
  2. Browse to the JavaScript file that you want to test and select it but don’t add it yet.
  3. Click the down arrow next to Add and choose Add as link.
  4. Right-click on the TestScripts folder and Choose Add | New Item.
  5. Find and choose JScript File.
    Note: My file is called MySample.js.
  6. Give the JavaScript file a name.
    Note: I named mine MySampleTests.js.
  7. Open the ExampleTests.js file for editing.
  8. Start adding tests.

Conclusion

This method allows for rudimentary testing of your JavaScript. However, it does not integrate with Visual Studio, or Code Coverage, or Gated Check-ins. So there is a lot more work that needs to be done.


Changing ol and li html tags to have “Steps” and Hanging indent.

Let’s say you want to have a list of items. However, you want the word “Step” before each number in the list. You may be tempted to not use the ol and li tags and just write the list.

Step 1 – Do something.
Step 2 – Do something else.

However, it will be obnoxious to type “Step” every time. The list doesn’t scale either. If the list gets long and you want to reorder the list, or even add a step, you have to change every number. Also, what if you want a hanging indent? How are you going to do that?

Well, just use these CSS classes.

ol.Steps {
  counter-reset: section;
  list-style-type: none;
  padding: 0;
  padding-left: 55px;
}

ol.Steps li {
  text-indent: -55px;
}

ol.Steps li:before {
  counter-increment: section;
  content: "Step " counter(section) " - "; // Change to ": " if you like
}

Here is some sample html:

<ol class="Steps">
  <li>The first thing to do.</li>
  <li>The second thing to do.</li>
  <li>The third thing to do. However, this step is really, really long. In fact, it is so long that the instructions to do this step wrap all the way into multiple lines.</li>
  <li>The fourth thing to do.</li>
</ol>

Here is how it looks:

  1. The first thing to do.
  2. The second thing to do.
  3. The third thing to do. However, this step is really, really long. In fact, it is so long that the instructions to do this step wrap all the way into multiple lines. No really. It has to go one multiple lines, even if the screen is really, really big.
  4. The fourth thing to do.

Get Active Directory User’s GUID and SID in C#? (Part 2)

Yesterday we learned how to Get the Current User’s Active Directory info. Today we will learn how to get a named user’s Active Directory info?

Get a named User’s Active Directory info?

The first steps are the same as yesterday.

Step 1 – Create a new Console Application project in Visual Studio.

Step 2 – Add a .NET reference to System.DirectoryServices.AccountManagement.

Step 3 – Populate the Main Method as shown below.

using System;
using System.DirectoryServices.AccountManagement;

namespace NamedUserAdInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "Rhyous";
            PrincipalContext context = new PrincipalContext(ContextType.Domain);
            UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
            Console.WriteLine("Name: " + user.Name);
            Console.WriteLine("User: " + user.UserPrincipalName);
            Console.WriteLine("GUID: " + user.Guid);
            Console.WriteLine(" SID: " + user.Sid);
        }
    }
}

The only difference between the current user and a named user is that there is a static value for the current user called UserPrincipal.Current whereas for a named user, you need the user name.

Writing a program that does both

OK, so lets make a program that takes a parameter and does both. Here it is.

using System;
using System.DirectoryServices.AccountManagement;

namespace GetAdUserInfo
{
    class Program
    {
        static UserPrincipal user = UserPrincipal.Current;

        static void Main(string[] args)
        {
            ParseArgs(args);
            OutputUserInformation();
        }
        
        private static void OutputUserInformation()
        {
            Console.WriteLine("Name: " + user.Name);
            Console.WriteLine("User: " + user.UserPrincipalName);
            Console.WriteLine("GUID: " + user.Guid);
            Console.WriteLine(" SID: " + user.Sid);
        }

        private static void ParseArgs(string[] args)
        {
            foreach (var arg in args)
            {
                if (arg.StartsWith("user=", StringComparison.CurrentCultureIgnoreCase))
                {
                    string[] splitArgs = arg.Split("=".ToCharArray());
                    string userName = string.Empty;
                    if (splitArgs.Length == 2)
                        userName = splitArgs[1];
                    if (string.IsNullOrWhiteSpace(userName))
                    {
                        Syntax();
                    }

                    // set up domain context
                    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

                    // find a user
                    user = UserPrincipal.FindByIdentity(ctx, userName);
                    continue;
                }

                if (arg.StartsWith("user=", StringComparison.CurrentCultureIgnoreCase))
                {
                    Syntax();
                }

                // if arg not found treat it like /?
                {
                    Console.WriteLine("Argument not found: " + arg);
                    Syntax();
                }
            }
        }

        private static void Syntax()
        {
            String fullExeNameAndPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            String ExeName = System.IO.Path.GetFileName(fullExeNameAndPath);
            Console.WriteLine(ExeName + " user=[username]");
        }
    }
}

Here are the sample projects.
GetAdUserInfo.zip


Get Active Directory User’s GUID and SID in C#? (Part 1)

Get the Current User’s Active Directory info?

Here is how to get the currently logged in users Active Directory GUID and SID.

Step 1 – Create a new Console Application project in Visual Studio.

Step 2 – Add a .NET reference to System.DirectoryServices.AccountManagement.

Step 3 – Populate the Main Method as shown below.

using System;
using System.DirectoryServices.AccountManagement;

namespace GetAdUserInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Name: " + UserPrincipal.Current.Name);
            Console.WriteLine("User: " + UserPrincipal.Current.UserPrincipalName);
            Console.WriteLine("GUID: " + UserPrincipal.Current.Guid);
            Console.WriteLine(" SID: " + UserPrincipal.Current.Sid);
        }
    }
}

See Part 2 – Get a named User’s Active Directory info?