Showing posts from 2016

SQL Query to Populate a Table with Random Data

My go to database for trying out new ideas is my CrittersAndThings database. Below is the layout of the first version from years ago. I use it even today when I need something simple to bang on. (The later versions get pretty crazy and sometimes it's a bit much for testing simple things.) The general idea is that the world (my test world, that is) has: critters -- living animal-like things such as people, orcs, fish, birds, bugs, sentient gelatinous masses, you name it things -- all the stuff that isn't a critter, such as plants, minerals, planets, buckets of mop water, clouds of gas, etc. Database Structure Table: Critters We store critters here. CritterID (PK, int, not null) Name (varchar(50), null) Height (int null) Weight (int null) Age (int null) CritterTypeID (FK, int, null) IsAlive (bit, null) Table: CritterTypes This table contains various types of critters. CritterTypeID (PK, int, not null) Name (varchar(50), null) Description (varchar(50)

List of First Names and Last Names for Test Data, Plus a Query to Generate a Random Full Name

I found this page to be very helpful when I was generating random names for test data: Naturally, I didn't read everything and just grabbed the two CSV files (one for first names, a second for last names). Next, I created two tables (FirstNames, LastNames) and imported the CSV files into their respective tables. The following query will assemble a Full Name from a randomly selected First Name and randomly selected Last Name: -- Generate random LastNameID DECLARE @RandomLastNameID int; DECLARE @UpperLastNameID int; DECLARE @LowerLastNameID int; SET @UpperLastNameID = 88799; SET @LowerLastNameID = 1; SET @RandomLastNameID = RAND() * @UpperLastNameID + @LowerLastNameID; -- Generate random FirstNameID DECLARE @RandomFirstNameID int; DECLARE @UpperFirstNameID int; DECLARE @LowerFirstNameID int; SET @UpperFirstNameID = 5494; SET @LowerFirstNameID = 1; SET @RandomFi

Handy Online Tool to Remove Line Breaks in Strings

I had to remove a bunch of line breaks in a SQL query (don't ask). I found this nifty tool that does just that. Copy and paste your string into the upper text area field, select "Remove line breaks only" or "Remove line breaks and paragraph breaks", and then click the "Remove Line Breaks" button. Your clean text will appear in the lower text area field. Put your mouse in there, click once, and the text is highlighted. Paste that into your clip board, and then paste that into whatever you want.

ASP.NET: Change page title based on database to which you're connecting

On occasion, you may find it useful to provide a visual cue that the user is not in an application's production environment. This is especially useful during a late night crunch when the developers, system administrators, and the Q.A. team are distracted and tired. Below is some code which allows you to change the page title and its style based on the database specified in a connection string. Note that we are parsing an Entity Framework connection string, not an ADO.NET connection string. Site.Master <div class="navbar navbar-inverse navbar-fixed-top">     <div class="container">         <div class="navbar-header">             <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">                 <span class="icon-bar"></span>                 <span class="icon-bar"></span>                 &

List of Refresher Tutorials on Advanced Transact-SQL Techniques

It never hurts to go back and look at the fundamentals. For instance, I had forgotten about the APPLY operator and was rusty on the PIVOT operator. has a good list of tutorials, Stairway to Advanced T-SQL , that hits most of the advanced fundamentals. Enjoy!

Fixing the error: "The element 'entityFramework' has invalid child element 'providers'. List of possible elements expected: 'contexts'"

If you're getting the error message: The element 'entityFramework' has invalid child element 'providers'. List of possible elements expected: 'contexts'. and you're using Visual Studio 2012 or Visual Studio 2013, then you can download and install  Entity Framework 6 Tools for Visual Studio 2012 & 2013 . These tools update the schema used to validate the configuration files for your projects. Entity Framework 6 Tools for Visual Studio 2012 & 2013

Most Likely Cause of the C# Error: "Non-static method requires a target"

I've encountered this error twice in the last two days, both times occurring while with a LINQ query and Entity Framework. And, in both cases, the problem stemmed from using a null value within the LINQ. Ultimately, the problem was farther upstream and not with my LINQ query at all (though better use of a try-catch statement would've been smart). The problem was an object with a null value being passed into the method. Once I figured that out, it was fairly easy to step through the debugger and see where my object wasn't being set to a legitimate value. So, if you get the error "Non-static method requires a target,", then verify that every value you're using within your LINQ statement contains a value, not a null. See the following posts on the web for more information and resolution to this error. Non-static method requires a target EF Non-static method requires a targe

ASP.NET Routing Not Working in IIS

After deploying a brand new website to IIS on Windows Server 2008 R2, I encountered numerous issues. First, the web pages had no no styling. I could "View Source" and see that the code was correct, and I could browse to the folder containing the style sheets (bootstrap, my new obsession) and bring up the style sheet itself, but the application couldn't find the style sheets at run time on its own. Next, none of the links worked. The page "StudentsList", which should bring up "StudentsList.aspx" returned a HTTP 404 status. After futzing around a bit, it dawned on me that both issues stemmed from routing problems. In no time, I found several suggestions -- including good ol' Rick Strahl's blog -- most of which pointed to the same fix. Add the runAllManagedModulesForAllRequests attribute to the <modules> tag of the <system.WebServer> section of the web.config file. See Strahl's entry for more details: https://weblog.wes

ASP.NET: Converting Web Forms to Content Pages

This is for my own reference while we convert some old sites to shiny new stuff. But, other people might find this useful. Every web page in the site was created as an ASP.NET Web Form. No Master Pages were used. To ensure a consistent look-and-feel and consolidate common pieces of code (such as the code that checks whether or not a user is logged in), the Web Forms should be converted to Content Pages. Follow the steps below to convert an existing Web Form to a new Content Page. Open the Web Form to the markup view. Add the following to the @Page directive at the top of the form: MasterPageFile="~/Site.Master" The resulting @Page directive should look something like this: <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Logon.aspx.vb" Inherits="SGBP.Logon" %>   Remove

How to Add New Routes to ASP.NET Friendly URLs

If you haven't discovered or used Friendly URLs in your ASP.NET Web Forms applications, then you should take a look. If you're familiar with routing in ASP.NET MVS, then Friendly URLs in Web Forms will look much (if not exactly) the same. There's a handful of great overviews, how-to's, and tutorials out there. The following two were enough to jump start me: Introducing ASP.NET FriendlyUrls -- cleaner URLS, easier Routing, and Mobile Views for ASP.NET Web Forms , by Scott Hanselman Walkthrough: Using ASP.NET Routing in a Web Forms Application , from the MSDN Library You  an retrofit an older Web Forms applications to use Friendly URLs. Or, if you're creating a Web Forms application

Web Deployment Overview for Visual Studio and ASP.NET, on MSDN

How to build and run your first deep learning network, by Pete Warden

Deep Learning, an An MIT Press book by Ian Goodfellow, Yoshua Bengio and Aaron Courville

How to Drop All Data from Your Database

Quick and handy script, copped from StackOverflow: -- DROP ALL ROWS IN ALL TABLES EXEC sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?' GO EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' GO EXEC sp_MSForEachTable 'DELETE FROM ?' GO EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL' GO EXEC sp_MSForEachTable 'ENABLE TRIGGER ALL ON ?' GO Full post on StackOverflow can be found here:

101 LINQ Samples

Am I the last person to know about this?! Over a hundred code samples showing essential LINQ operations. Samples are broken into the following categories: Restriction Operators Projection Operators Partitioning Operators Ordering Operators Grouping Operators Set Operators Conversion Operators Element Operators Generation Operators Quantifiers Aggregate Operators Miscellaneous Operators Custom Sequence Operators Query Execution Operators Join Operators Nice!

Quick and Easy Way to Convert Unicode to ASCII (C#)

///   <summary> ///  Given a string, replaces UNICODE characters with ASCII characters ///   </summary> ///   <param name="s"> the string to be scrubbed of UNICODE characters </param> ///   <returns> a string with only ASCII characters </returns> ///   <code> ///  string myString = "BÌĹĹ GÓÓĎ"; ///  System.Console.WriteLine(" Converted string = " + Utilties. ConvertUnicodeToAsciiV2( myString)); ///   </code> public   static   string  ConvertUnicodeToAsciiV2( string  s) {      return   Encoding .ASCII.GetString( Encod ing .GetEncoding(437).GetBytes( s)); }