Lance Larsen - .NET Architect Madison Wisconsin

All posts in Web

imageFYI – here’s a trick so we don’t need to specify our local computer’s name in our web.config connectionString – because if you have multiple developers working on the same code – they each have to have different web.config versions with their individual computer names.

Generally we see…

<connectionStrings>
     <add name=”MyContext” connectionString=”Data Source=MYCOMPUTER; Initial Catalog=MyDatabase; Integrated Security=True; MultipleActiveResultSets=True” providerName=”System.Data.SqlClient” />
</connectionStrings>

What we want…

<connectionStrings>
    <add name=”MyContext” connectionString=”Data Source=.; Initial Catalog=MyDatabase; Integrated Security=True; MultipleActiveResultSets=True” providerName=”System.Data.SqlClient” />
</connectionStrings>



How?

1) Open your “Sql Server Configuration Manager” application.
2) Expand your “Sql Server Network Configuration”.
3) Right-click on “TCP/IP”.
4) Scroll down to “TCP Dynamic Ports” (which will be blank by default) and enter “0” which enables it. 
5) Restart your SQL service and you’re DONE!

Thanks Travis Feirtag (@therobotgeek) for this trick!


imageSome days CSS needs a little love thrown it’s way… My OCD and I have an agreement – I do what it wants and it makes me a better maker. :)

Needed an elegant solution for customizing the standard <UL><LI> html bullets, and strongly dislike having to create a graphic for this – so after some non-trivial searching though not-so-good solutions came across some nice CSS to make this happen with FontAwesome – and always good to share nice solutions.  Enjoy.

Here’s the jsFiddle ( http://jsfiddle.net/lancelarsen/e08tp2ga/ )

Read more


image As I always emphasize – invest in yourself first!  So attend education events, user group meetings, sent aside time specifically to learn, etc.

Today I drove down from Madison to Chicago area to attend a free Microsoft WebCamp event. Worth the ~3+ hour drive. :)

“The new release of ASP.NET and Web Tools for Visual Studio 2013 make it easier than ever to build cutting-edge websites – and once you create a beautiful, interactive site, you can quickly deploy and scale it with Microsoft Azure! … no-fluff events created for developers, by developers.”

For those in Wisconsin, there is another one coming up in Waukesha on March 24th 2015 – but I didn’t want to wait… :)

Click here to Register — and here’s the Source and Slides.

Thanks to David Giard, our Microsoft Evangelist for leading the event.

Twitter: @DavidGiard, Blog: http://davidgiard.com


So AngularJs is pretty sweet – especially considering the power and elegance of how you can use “filters” to change the way values appear in the UI.  There is a built in “number” filter that you can apply to a number value and have it always display a set number of decimal places, i.e. “1″ -> “1.0″  But, we wanted a filter that did the following…

“1.56″ => “1.6″
“1.5″ => “1.5″
“1.0″ => “1″
“1″ => “1″

So I wrote a simple custom filter, setDecmial! 

As I love jsFiddle – here’s the live working version: http://jsfiddle.net/lancelarsen/Tx7Ty/

If anyone has suggestions for further improvements, always love to keep making the code better. :) 

<div ng-app="myApp">
    <div ng-controller="Ctrl">
        setDecimal Filter (1): <span>{{val | setDecimal:1}}</span><br>
        setDecimal Filter (2): <span>{{val | setDecimal:2}}</span><br>
    </div>
</div>

var app = angular.module('myApp', []);

app.filter('setDecimal', function ($filter) {
    return function (input, places) {
        if (isNaN(input)) return input;
        // If we want 1 decimal place, we want to mult/div by 10
        // If we want 2 decimal places, we want to mult/div by 100, etc
        // So use the following to create that factor
        var factor = "1" + Array(+(places > 0 && places + 1)).join("0");
        return Math.round(input * factor) / factor;
    };
});

app.controller('Ctrl', function ($scope) {
    $scope.val = 1.56;
});

imageLittle things make me happy…

Easy way to get the RGB value for images / web pages

Check out ColorZilla plugin for Chrome.


imageYou should almost always store your date/times in your database as UTC – you may think it’s not important now, but as soon as your application needs to display times to users in different time zones ( to paraphrase Yoda ) …it will be, it will be… 

But do you need to know and store your user’s timezone to display it correctly? 

Nope, JavaScript and momentjs to the rescue…! :)

Read more


large_jquery_logoIn my blog “NuGet – Limiting updates for jQuery and Bootstrap” – we looked at continuing to utilize NuGet to keep us up to date on the latest jQuery 1.x version ( even though 2.x is out ).  Reason being that we needed to stick with 1.x because we are still supporting IE 7/8. :(

So… jQuery 1.x and 2.x have feature parity – but you want to use jQuery 2.x when you can, simply because it’s a smaller download as they were able to take out a lot of IE7/8 “fixes”.  Note, jQuery’s post on conditionally include one or the other based on the version of the browser.

But we also like to use the cool MVC bundling and minification feature – can we have the best of all worlds? Oh ya… :)

Read more


What’s the Holy Grail of Application Profiling?  Well, it should be…

  • Rich Data – Giving us detailed logging ability, recording which method is being called, where it’s being called from, when it starts, when it stops, overall execution time –and- if any exceptions are thrown.
  • Low Impact – Adds just a fraction of a second to the methods being profiled, highly performant. ( MS-Word says “performant” isn’t a word – Wiktionary and I disagree <grin> )
  • Minimal Code – Follows the D.R.Y. principle ( Don’t Repeat Yourself), less code == easier maintenance and generally better performance.
  • Loosely Coupled – Easy to add to a project, with little to no code added to the existing code.

So… How do we create the Holy Grail of Application Profiling?  We use a very cool technology called AOP ( Aspect Oriented Programming ). 

AOP complements Object-Oriented Programming (OOP), giving us even more tools that increase modularity and allowing us to elegantly handle “crosscutting concerns” such as logging, exception handling and other common challenges.  One of the most popular AOP tools is PostSharp (http://www.sharpcrafters.com/), which allows us to “IL Weave” our profiling code ( the act of adding code as IL instructions into the assemblies at compile time )!  Sweet…

So let’s start at the end – what do we get? 

We’ll if you start a brand new “MVC 4 Web Project” in Visual Studio – by adding a couple references and ONE line in your AssemblyInfo.cs file…

 

[assembly: ProfileAspect(AttributeTargetTypes = "ProfileWeb.Controllers.*")]

We can start logging to SQL and, for example, easily extract really rich data on pages hit and their load times such as…

image

Nice… Not bad for ONE line of code… Intrigued?  I know you would be…  :)

Download the Profiler Code (44 MB)

Read more


So if you haven’t looked – in VisualStudio projects under the “Properties” folder – there is a file called “AssemblyInfo.cs” – it contains many useful assembly attributes for your project including AssemblyTitle, AssemblyCompany, AssemblyCopyright, AssemblyVersion, etc.

My goal was to make a simple and elegant static class to retrieve these values – on top of that wanted to be able to retrieve the values within another project ( class library ) from the calling project, ie. what is the AssemblyTitle that is calling this class library.  I was specifically doing this for logging purposes – wanting to capture the project and version.  Accomplished both as follows…

How to get the values…
.

   1:  string company = ApplicationInfo.Company;
   2:  string product = ApplicationInfo.Product;
   3:  string copyright = ApplicationInfo.Copyright;
   4:  string trademark = ApplicationInfo.Trademark;
   5:  string title = ApplicationInfo.Title;
   6:  string description = ApplicationInfo.Description;
   7:  string configuration = ApplicationInfo.Configuration;
   8:  string fileversion = ApplicationInfo.FileVersion;
   9:  Version version = ApplicationInfo.Version;
  10:  string versionFull = ApplicationInfo.VersionFull;
  11:  string versionMajor = ApplicationInfo.VersionMajor;
  12:  string versionMinor = ApplicationInfo.VersionMinor;
  13:  string versionBuild = ApplicationInfo.VersionBuild;
  14:  string versionRevision = ApplicationInfo.VersionRevision;

 

Read more