Internet Sellout

Demand Unearned Rewards

All posts by sellout

DISKPART extend filesystem

Extending NTFS leaves the system volume 0 and the file system out of wack if it throws an error during the extend.
Tags: NTFS, DISKPART

Rip CD to Flac with Embedded Cue Sheet

I want an archive to be a single file with embedded information including album art and track list. I want the player to be able to open the file display the album art and have the track list show just as it would if I popped in a CD.
Tags: FLAC, music, cue sheet, EAC, FooBar2000

Dynamic SQL Pivot

This SQL is to demonstrate a dynamic pivot query. It is dynamic because the number of columns is not known in advance. Or because it is known but the number of columns is large but incremental. In this sample the pivot column is week of year.

 

SELECT * INTO #DATA FROM (SELECT 
  CAST('Women' as varchar(50)) as UserName
, CAST('1/1/2014' As datetime) As DateDied
, CAST(48 As int) As Age
, CAST(5 As int) As Dollars
, CAST(1 As int) As Heirs) X
INSERT INTO #DATA SELECT 'Men', '1/5/2014', 50, 300, 2
INSERT INTO #DATA SELECT 'Men', '1/20/2014', 55, 5000, 4
INSERT INTO #DATA SELECT 'Women', '2/20/2014', 23, 55, 0
INSERT INTO #DATA SELECT 'Women', '2/20/2014', 43, 22, 1

--this part will blow out the number of @days in the past and future of @date
DECLARE @date datetime = CAST(GetDate() as date)
DECLARE @days int = 365

;WITH CTE1(N) AS (SELECT N FROM (VALUES(1),(1))a(N)),
CTE2(N) AS (SELECT 1 FROM CTE1 x, CTE1 y),
CTE3(N) AS (SELECT 1 FROM CTE2 x, CTE2 y),
CTE4(N) AS (SELECT 1 FROM CTE3 x, CTE3 y),
CTEFINAL AS (SELECT TOP (@days) CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as int) AS RowNum FROM CTE4 x, CTE4 y)
SELECT 
Offset,
DateRangeDate As [Date]
, DatePart(yyyy,DateRangeDate) As [Year]
, DatePart(mm,DateRangeDate) As [Month]
, DateName(mm,DateRangeDate) As [MonthName]
, DatePart(dd,DateRangeDate) As [Day]
, DatePart(dy,DateRangeDate) As [DayOfYear]
, DatePart(wk,DateRangeDate) As [Week]
, DatePart(dw,DateRangeDate) As [DayOfWeek]
, DateName(dw,DateRangeDate) As [DayName]
, CASE WHEN DatePart(mm,DateRangeDate) IN (1,2,3)
 THEN 1 WHEN DatePart(mm,DateRangeDate) IN (4,5,6)
 THEN 2 WHEN DatePart(mm,DateRangeDate) IN (7,8,9)
 THEN 3 ELSE 4 END As [Quarter]
   INTO #DATEFACTS FROM 
(SELECT RowNum -1 As Offset, @date + RowNum -1 AS DateRangeDate  FROM CTEFINAL
UNION 
SELECT -RowNum As Offset, @date - RowNum AS DateRangeDate  FROM CTEFINAL) X
--Now you have some date facts

--This is your query
SELECT td.[Week], td.[Month], td.[Quarter], td.[Year], f.UserName, f.DateDied, f.Age, f.Dollars, f.Heirs
 INTO #TEMP FROM #DATA f WITH (NOLOCK)
INNER JOIN #DATEFACTS td ON td.[Date] = f.DateDied

--Edit Defaults Below 
DECLARE @col2prefix varchar(50) = 'A'
DECLARE @col3prefix varchar(50) = 'D'
DECLARE @col4prefix varchar(50) = 'H'
DECLARE @col2label varchar(50) = 'Avg Age Week'
DECLARE @col3label varchar(50) = 'Sum Dollars Week'
DECLARE @col4label varchar(50) = 'Avg Heirs Week'
DECLARE @col2data varchar(50) = 'Age'
DECLARE @col3data varchar(50) = 'Dollars'
DECLARE @col4data varchar(50) = 'Heirs'
DECLARE @order varchar(50) = @col2label
DECLARE @sort varchar(50) = 'DESC'
DECLARE @grouprow varchar(50) = 'UserName'
--Edit Defaults Above 

declare @col1 varchar(MAX), @col2 varchar(MAX), @col3 varchar(MAX), @col4 varchar(MAX), @colorder varchar(MAX)
declare @sql varchar(MAX)

-- Replace all [Week] below with your pivot column value
;WITH CTE AS (SELECT DISTINCT [Week] FROM #TEMP)
		select   @col1 = COALESCE(@col1 + ', ','')
		+ 'MAX(' + @col2prefix + CAST([Week] as varchar(100)) + ') AS ' +  QUOTENAME(@col2label + ' ' + CAST([Week] as varchar(100))) 
		+ ',MAX(' + @col3prefix + CAST([Week] as varchar(100)) + ') AS ' +  QUOTENAME(@col3label + ' ' + CAST([Week] as varchar(100)))
		+ ',MAX(' + @col4prefix + CAST([Week] as varchar(100)) + ') AS ' +  QUOTENAME(@col4label + ' ' + CAST([Week] as varchar(100)))
		,@col2 = COALESCE(@col2 + ', ','') +  @col2prefix + CAST([Week] as varchar(100))
		,@col3 = COALESCE(@col3 + ', ','') +  @col3prefix + CAST([Week] as varchar(100))
		,@col4 = COALESCE(@col4 + ', ','') +  @col4prefix + CAST([Week] as varchar(100))
		,@colorder = COALESCE(@colorder + ', ','') + '[' + @order + ' ' + CAST([Week] as varchar(10)) + '] ' + @sort
		FROM cte ORDER BY [Week]

		SET @sql = 'SELECT ' + @grouprow + ', ' + @col1 + 'FROM ( SELECT ' + @grouprow + ',' + @col2 + ',' + @col3 + ',' + @col4 + ' FROM ('
		+ 'SELECT ' + @grouprow + '
		, ''' + @col2prefix + ''' + CAST([Week] as varchar(100)) As ' + @col2prefix + '
		, ''' + @col3prefix + ''' + CAST([Week] as varchar(100)) As ' + @col3prefix + '
		, ''' + @col4prefix + ''' + CAST([Week] as varchar(100)) As ' + @col4prefix + '
		, ' + @col2data + ', ' + @col3data + ', ' + @col4data + ' FROM #TEMP R
		) R2
		PIVOT ( AVG(' + @col2data + ') FOR ' + @col2prefix + ' IN (' + @col2 + ')) AS P2
		PIVOT ( SUM(' + @col3data + ') FOR ' + @col3prefix + ' IN (' + @col3 + ')) AS P3
		PIVOT ( AVG(' + @col4data + ') FOR ' + @col4prefix + ' IN (' + @col4 + ')) AS P4
		) AS X
		GROUP BY ' + @grouprow + ' ORDER BY '	+ @colorder



		EXEC (@sql);

DROP TABLE #TEMP
DROP TABLE #DATEFACTS
DROP TABLE #DATA

ASP.NET Write to Windows Event Logs

I like applications to write to the Windows Event Logs unless they are going to be verbose. Windows Event Logs can be collected and forwarded with a variety of tools for continuous monitoring. Sometimes logging errors is going to be way to noisy and an alternative is needed. But it is a good idea to write at least some things to the Windows Event Logs. .NET has a framework for the config files that can be used so that if you need to change the logging behavior it is somewhat configurable as opposed to a code change. The basic way to write is this System.Diagnostics http://msdn.microsoft.com/en-us/library/xzwc042w(v=vs.110).aspx technique. But App.config and web.config can have something like:

  <system.diagnostics>
    <sources>
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="EventLog"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information"/>
    </switches>
    <sharedListeners>
      <add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="MyLogs"/>
    </sharedListeners>
    <trace autoflush="true" indentsize="4"></trace>
  </system.diagnostics>

 

 in this example, MyLogs is the event source. Not all users, especially weak web app pool users can create an event source and you need one before you can write. If it is a windows app, maybe you can do that in the app code or maybe you can push it with group policy. If it is a web server then maybe just running this command:

eventcreate /ID 1 /L APPLICATION /T INFORMATION  /SO MyLogs /D "MyLogs Event Source exists now if it didn't before."

This will create the Event Source.

Now in ASP.NET VB.NET, this works: 

        Try
            My.Log.WriteException(exCurrentException, Diagnostics.TraceEventType.Warning, System.Configuration.ConfigurationManager.AppSettings("ApplicationName"))
        Catch
        End Try

Or for a Windows application 

        Try
            My.Application.Log.WriteException(exCurrentException, Diagnostics.TraceEventType.Warning, System.Configuration.ConfigurationManager.AppSettings("ApplicationName"))
        Catch
        End Try

 It is not clear to me if a C# developer needs to use the Microsoft.VisualBasic namespace and assembly or if these settings in the config files mean anything for anything out of this namespace.

Advice from a Sell Out: Don’t Let Your Job Determine Your Career

Learn things you think will help your career by creating useful projects for yourself or friends using technology you want to explore. Learning by doing is best but doing without your job's business requirements lets you actually explore.

If your job requires licenses to do it, if you can’t own them (afford them) yourself or the vendor doesn’t have a licensing model that allows you to get your hands on the equivalent technology for educational and non-commercial purposes, maybe that’s not a technology that you want to learn.

Be interested in technologies that will evolve and survive. We live in an era where often something great and simple becomes a piece of crap over time only to be forked and have the process start over again. There have also been cycles where the formalization and standardization of technology has led to complexity that makes it unattractive to new developers. Often they reject a standard and implement a new more straight forward approach to solve the immediate problem. The beauty and elegance of the solution for the initial use case draws contributors until the beauty is marred by attempts to re-add all the lesser use cases. There are so many ways for a technology to die or become dormant. Sometimes you can’t help yourself, but try to assess the future of a technology when creating synergy between your career and your side projects.

If you like the advice from Mr. Frank, you may like other business insights with these tags, and these special links:

Just-In-Time-Development

Risk-Voodoo-1

Risk-Voodoo-2

Learn Ham

I am starting a post on my struggles convincing spamassassin's Bayes classification system to learn ham. When I su into the user account I can run this command:

sa-learn --dump magic

and it tells me how many ham and spam it thinks it has seen. when I train spam it goes up, but not when I train ham.

Turning Off IPv6 On Rackspace Cloud Server Blows

Blow Hole - Sunny CoveI was trying to diagnose some issues with ASP.NET Web Forms post backs so I turned off IPv6 even though none of my websites were bound to IPv6 addresses. I unchecked it on the adapter property page. I had no logical reason for doing this. Then Windows Update stopped working. This was from a Rackspace Cloud Server. I cannot imagine what kind of tunneling is taking place in various parts of the internet with IPv6 getting rolled out (finally).

Oh and by the way, I had trouble uploading this picture with BlogEngine.NET and I saw this article:

http://blogengine.codeplex.com/workitem/12382

I did what the user said (in settings unchecked optimize which minifies) and it solved the problem, however I am on IIS 8 not IIS Express.

Videos of the Blow Hole:

http://www.youtube.com/results?search_query=sunny%20cove%20blow%20hole&sm=3

whats my preferred IP anyway

What is my preferred IP anyway?

Not this:

Set-NetIPAddress -IPAddress xxx.xxx.xxx.xxx -SkipAsSource 1

You can do that in Power Shell on all the IP addresses you don't want to be chosen as your preferred IP address for your windows 2012 machine.

This will tell you what is set:

Netsh int ipv4 show ipaddresses level=verbose

I have read that once you start managing this with Power Shell you will lose information if you try and edit with the GUI.

.NET 4.5.1, IE 11, IIS 8 - oh my

This is not going well...

The page size of asp.net had increased when comparing to 2008 R2 IIS 7.5 ASP.NET 4.0

C:\Windows\System32\Inetsrv\Appcmd.exe  set config -section:httpCompression   -[name='gzip'].staticCompressionLevel:9  -[name='gzip'].dynamicCompressionLevel:4

 All of a sudden they got smaller...

This modified the applicationHost.config file.