Posts

Showing posts from 2013

Pretty Print Array of Arrays in .NET C#

Image
I had a need to pretty-print  an array of arrays while parsing some JSON recently. I couldn't find anything I liked, so I thought I'd post what I ended up with here to save others some time. https://gist.github.com/aaronhoffman/4232d28b769b3e95b143cf609a9e27cc Example Output: [[1,2,3],[3,5,7],[2,4,6]] [[1,2,3]] [[1,2,3],[3,5,7,9,11],[2,4,6]] public string PrettyPrintArrayOfArrays(int[][] arrayOfArrays) { if (arrayOfArrays == null) return ""; var prettyArrays = new string[arrayOfArrays.Length]; for (int i = 0; i < arrayOfArrays.Length; i++) { prettyArrays[i] = "[" + String.Join(",", arrayOfArrays[i]) + "]"; } return "[" + String.Join(",", prettyArrays) + "]"; } Hope this helps, Aaron

Flatten HTML Document to List of Tags, Attributes, and Values

I had a need to flatten a set of HTML documents to a list of the HTML tags in their head sections.  I thought this bit of code might be useful for someone in the future. This uses the CsQuery library which is a port of jQuery in C#:  https://github.com/jamietre/CsQuery CsQuery also has a NuGet Package:  https://www.nuget.org/packages/CsQuery //Note: Get HTML from somewhere... var html = ""; var cq = CsQuery.CQ.Create(html); var head = cq["head"]; var nonScriptHeadTagsQuery = from t in head.Children() where t.NodeName != "SCRIPT" && t.NodeName != "LINK" select new { Tag = t, TagId = Guid.NewGuid() }; var nonScriptHeadTags = nonScriptHeadTagsQuery.ToList(); var htmlTags = nonScriptHeadTags .SelectMany(tagInfo => tagInfo.Tag.Attributes, (tagInfo, attribute) => new { TagInfo = tagInfo, Attribute = attribute }) .Select(x => new { TagId = x.TagInfo.TagId, TagType = x.TagInfo.Tag.NodeName, A

ASP.NET MVC 4 Bulk Add New Users

Image
---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- I recently developed a website to replace the built in ASP.NET Configuration Website Administration Tool provided by Microsoft and Visual Studio. Source available here:  https://github.com/StoneFinch/SmpMaintenance Direct link to download source code: https://github.com/StoneFinch/SmpMaintenance/archive/master.zip This tool has the ability to add new users to a ASP.NET MVC 4 website in bulk.  For example, if you have a list of usernames and passwords that you want to set up new accounts for, you can use the "Bulk Add New Users" functionality. Users can also be added to Roles via this page.  Multiple Roles are separated by spaces. Hope this helps, Aaron

ASP.NET MVC 4 SimpleMembershipProvider Website Administration Tool, ASP.NET Configuration

---------------------- Updated for ASP.NET MVC 5 and Identity 2.0. Includes an Azure Site Extension: http://aaron-hoffman.blogspot.com/2016/08/aspnet-mvc-5-user-admin.html ---------------------- ---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- The built in ASP.NET Configuration Website Administration Tool provided by Microsoft and Visual Studio left something to be desired. I have developed an alternative that can be used with ASP.NET MVC 4 SimpleMembershipProvider. Code available here:  https://github.com/StoneFinch/SmpMaintenance Current functionality at the time of this writing:   - Add New Users   - Bulk Add New Users   - Search Users   - Add New Roles   - Edit Existing Users   - Reset User Password (on Edit User page)   - Add Users to Roles   - Remove Users from Roles Hope this helps, Aaron

ASP.NET MVC 4 SimpleMembershipProvider, Web Security Roles Add User To Role SQL, Remove User From Role SQL

Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's System.Web.Security.Roles.AddUserToRole() method and RemoveUserFromRole() method are called: Text below in bold is dynamic and represents the UserName and RoleName property passed into the AddUserToRole()  method (or the UserId,   RoleId associated with the UserName,   RoleName ). Roles.AddUserToRole("UserName", "RoleName"); exec sp_executesql N'SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)',N'@0 nvarchar(25)',@0=N' USERNAME ' exec sp_executesql N'SELECT RoleId FROM webpages_Roles WHERE (RoleName = @0)',N'@0 nvarchar(5)',@0=N' RoleName ' exec sp_executesql N'SELECT COUNT(*) FROM [UserProfile] u, webpages_UsersInRoles ur, webpages_Roles r Where (u.[UserName] = @0 and r.RoleName = @1 and ur.RoleId = r.RoleId and ur.UserId = u.[UserId])',N'@0 nvarchar(8),@1 nvarchar(8

ASP.NET MVC 4 SimpleMembershipProvider, Web Security Roles Create Role SQL, Delete Role SQL

---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's System.Web.Security.Roles.CreateRole() method and DeleteRole() method are called: Text below in bold is dynamic and represents the RoleName property passed into the CreateRole() method (or the RoleId associated with the RoleName ). Roles.CreateRole("RoleName"); exec sp_executesql N'SELECT RoleId FROM webpages_Roles WHERE (RoleName = @0)',N'@0 nvarchar(8)',@0=N' RoleName ' exec sp_executesql N'INSERT INTO webpages_Roles (RoleName) VALUES (@0)',N'@0 nvarchar(8)',@0=N' RoleName ' Roles.DeleteRole("RoleName"); exec sp_executesql N'SELECT RoleId FROM webpages_Roles WHERE (RoleName = @0)',N'@0 nvarchar(8)',@0=N

Knockout.js binding syntax bind to root, self, current context

When using knockout.js  if your ViewModel is an array and you want to bind to the entire ViewModel, the syntax to bind to the ViewModel root, when the root is the current context: <ul data-bind="foreach: $data"> <li> <p data-bind="text: SomeTextProperty"> </p> </li> </ul> The XAML syntax equivalent when the DataContext is an array: <itemscontrol ItemsSource="{Binding Path=.}"> ... </itemscontrol> Hope this helps, Aaron

ASP.NET MVC 4 SimpleMembershipProvider, WebMatrix WebSecurity CreateUserAndAccount Create New User SQL

---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's WebMatrix.WebData.WebSecurity.CreateUserAndAccount() method is called: Text below in bold is dynamic and represents the UserName and Password properties passed to the CreateUserAndAccount() method as well as the UserId generated as part of user creation. Similarly, the name of the UserId and UserName columns, and the name of the UserProfile table may also be different based on your specific configuration. exec sp_executesql N'SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)',N'@0 nvarchar(23)',@0=N' UserName ' exec sp_executesql N'INSERT INTO [UserProfile] ([UserName]) VALUES (@0)',N'@0 nvarchar(23)',@0=N' UserName ' exec sp_executesq

ASP.NET MVC 4 SimpleMembershipProvider, WebMatrix WebSecurity Login SQL

---------------------- ASP.NET MVC 4 Membership Overview:  http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html ---------------------- Using SQL Profiler, below is the SQL that is executed when the ASP.NET MVC 4 SimpleMembershipProvider's WebMatrix.WebData.WebSecurity.Login() method is called: The UserName below (in bold ) is dynamic, it represents the UserName parameter passed to the Login() method. Similarly, the name of the UserId and UserName columns, and the name of the UserProfile table may also be different based on your specific configuration. exec sp_executesql N'SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)',N'@0 nvarchar(25)',@0=N' UserName ' exec sp_executesql N'SELECT COUNT(*) FROM webpages_Membership WHERE (UserId = @0 AND IsConfirmed = 1)',N'@0 int',@0=1 SELECT m.[Password] FROM webpages_Membership m, [UserProfile] u WHERE m.UserId = 1 AND m.UserId =

Edit Default Visual Studio 2012 Item and Project Templates

After adding a new file or project within a Visual Studio solution there are certain settings that I always update.  Follow the steps below to edit the default templates so these settings become the default. Visual Studio 2012 Project and Item Template files are located here: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplates Add "public" to all new C# class files: Edit the Class.cs file by adding the word "public" in front of the word "class" The Class.cs can be found here: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs After you're done, it should look like this: using System; using System.Collections.Generic; $if$ ($targetframeworkversion$ >= 3.5)using System.Linq; $endif$using System.Text; $if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks; $e

JavaScript Null Checking, Undefined and ! Unary Operator

Image
Google searches of "JavaScript null check" and "How to check for null in JavaScript" were returning results that left something to be desired in my opinion, so I compiled this list. See the code live here: JsFiddle:  http://jsfiddle.net/aaronhoffman/DdRHB/5/ Code also available here: https://github.com/aaronhoffman/utils/blob/master/JavaScript/NullChecks.js Comparison Chart: Hope this helps, Aaron p.s. I used a great tool Regex Pixie  http://www.regexpixie.com/  by StoneFinch  http://stonefinch.com/  to generate most of the code in this example (disclaimer, I currently work for them, however I would never promote a tool I don't use myself.)

Microsoft Excel 2013 Sort Pivot Table By Calculated Field

Image
Want your data to really pop ? Check out my latest project: Sizzle drag-and-drop interactive data visualization Sorting a Calculated field (or Value field) in Excel 2013 Pivot Tables may not be very clear.  Unlike the Row fields there is no option button in the column header allowing you to easily sort the column, but it is possible. In the example below, I have a Pivot Table with "ListItemAbv" as the Row and "Count of ListItemAbv" as a calculated field (there is also a filter on "RankOrder", but that is not relevant to the example) Notice that, by default, the Pivot Table is sorted by "ListItemAbv."  There is also an option button visible in the header column (A:3).  You can use that option button to easily sort the Pivot Table by "ListItemAbv." However, there is no option button available to sort the Pivot Table by the Calculated field "Count of ListItemAbv".  To sort by that calculated field, select

How to Turn Off Microsoft Arc Touch Mouse Scroll Sound Vibration

Image
I recently purchased a Microsoft Arc Touch Mouse and wanted to turn off the middle button wheel scroll sound vibration. It is possible, however perhaps not obvious.  First you need to download the "Mouse and Keyboard Center" executable available on this page https://www.microsoft.com/accessories/en-us/products/mice/arc-touch-mouse/rvf-00052#techspecs-connect . (direct link Win10 x64: https://go.microsoft.com/fwlink/?linkid=849754  ) Run the executable and install the software.  After the software is installed, turn off "Vibration." See image below: Hope this helps, Aaron p.s.  Want to make that next report really pop ? Check out this new tool I'm working on : drag-and-drop interactive data visualization

ASP.NET MVC 4 Membership, Users, Passwords, Roles, Profile, Authentication and Authorization

---------------- The easy way: Azure Site Extension to Add/Edit Users and Roles:  http://aaron-hoffman.blogspot.com/2016/08/aspnet-mvc-5-user-admin.html https://www.nuget.org/packages/AspNetUserMaintenanceAzureSiteExtension/ (old:  https://www.siteextensions.net/packages/AspNetUserMaintenanceAzureSiteExtension/ ) ---------------- ---------------- TLDR: Search for this line in your project and update it: WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); ---------------- There is a new Membership Provider in ASP.NET MVC 4 that can be used for Membership, Users, Passwords, Roles, Profile, Authentication and Authorization!  It is called the Simple Membership Provider .  It uses the WebMatrix WebData WebSecurity class as a facade. The Old Way You will no longer need to execute the old .NET 2.0 aspnet_regsql.exe like this: C:\Windows\Microsoft.NET\Framewor