Wednesday, October 24, 2012

How to Kill all Processes for a Specific Database

Usually you would just use the Activity Monitor like shown below...

The mundane repetition of right click each process and selecting
Kill Process becomes annoying after the n_th process. 
...buuuuut if you need to restore your Dev Database with fresh data, you need to kill all of the individual connections/processes before attempting a restore. Well when you are doing development work you can have over 10+ connections and using the Activity Monitor is very annoying to close all of them. Therefore I have come up with the following tSQL query:

SELECT
 dbid, 
 db_name(dbid) as DatabaseName,
 spid,
 KillStatement = 'KILL ' + CAST(spid AS varchar(10)) + '; '
FROM sys.sysprocesses
WHERE dbid = 15 -- Put your Databases ID here

The idea behind this query is to first find out what you database's ID is, enter the ID in the where clause and generate the KILL statements. Copy and paste the KILL statements and execute all of them at once. You will be warned about killing your own connection though like this:

Msg 6104, Level 16, State 1, Line 1
Cannot use KILL to kill your own process.

No worries though you can just kill that process in the Activity Monitor or just close the query window.

Enjoy

Friday, October 12, 2012

Worst Visual Studio 2010 Bug I Have Ever Seen

The Background
This bug was so bad I had to post about it. This is by far the WORST Visual Studio 2010 (VS2010, VS) bug I have ever seen in my life. No one likes bugs, but we can arguably say that some bugs are preferred over other if given the choice. So there are bugs that pretty much everyone has encountered such as random program shut downs. You are using it and then *poof* it is gone. Visual Studio 2003, 2008 and 2010 have all done that to me for one reason or another - it is just irritating, but much more than "#@$@% now I have to open all my crap up again!"

Well, I have found the mother of all visual studio bugs - the worst part is I don't know if it is limited to just my system or if anyone else can reproduce it. I know I can reproduce it, but trust me I don't want to. What I am about to uncover is going to sound ridiculous, but it is what happened:

Perfect Storm Conditions
My Conditions:
  • Visual Studio 2010
  • Web Application 
  • Web Application is hosted in IIS7 locally for testing
  • HTML Editor is Open for an ASPX file
Awakening the Beast
All I did, I kid you not, is write the following statement (click to view):


This reads:
<ItemTemplate>
 <asp:HyperLink ID="lnkAllocation" 
       NavigateUrl='<%# Bind("ClientID", "~/AllocationDetails.aspx?clientID={0}") Bind("AllocationID"), "&allocationID={0}" %>'
       runat="server"
       Text="Details" />
</ItemTemplate>

I don't want to get into what I was trying to accomplish because that is no excuse for what happens next.

The Horrific Symptoms
After copying and pasting that information in, Visual Studio 2010 froze for about 5 solid minutes. After that delightful event, I thought VS was just taking a crap like it does so often. No, it was much worse - after the 5 minutes it came back. "Okay..." I thought - that was weird. I try loading my site in my browser and nothing - just kept spinning. I thought maybe my cached IIS files were corrupted, so I cleared them out. Tried again.

This time I noticed that my whole PC was freezing up. I checked CPU usage, w3wp.exe (IIS7 Worker Process) was using 50% CPU on my 8 core machine. One of the cores was being maxed out, thrashed! That is horrifically bad. Still not understanding what happened. I restarted IIS, tried again, same problem.

I tried using IIS Express (formerly known as Casini). Same exactly behavior - the process named WebDev.WebServer40.EXE was now using 50% CPU, yay for CPU Thrashin!

What ever the hell it was that was causing this had everything to do with what I just pasted in. This partially makes sense - but holy shit - WHAT A BUG!

The Stupid Fix to the Stupid Problem
I deleted the code. The problem psudo went away. Every time I would paste something into VS, the whole IDE would hang for 5 minutes or more. I restarted VS and all was well at that point. Sadly, I could reproduce this behavior, because I needed to make sure what I just witnessed was real. I spent about 2 hours figuring this out because I couldn't get any work done until it was fixed. Talk about incentive.

The Gotcha
I have to do more research as to why this happened, but for now, all I can say is NEVER EVER try to bind two values to a single control's property! Microsoft should really fix this, but they probably won't - they are working on VS2012 - much like 2K won't fix the audio bugs for Bio Shock 2 because they are too busy working on DLC and new games. Not cool.

Thursday, October 11, 2012

The Bestest Office Chair Evar

My Ass Hurts
Are you looking for a new office chair because you hate how the one you sit in now hurts your ass after about 4 hours because the spongy crap they use in the chair is completely flattened out and is useless after about 6 months? Yeah me too!

My Ass No Longer Hurts
That's why I just spent too much money on an awesome office chair I use at work. My office chair doesn't have a cushion, it uses bungee cords to suspend my rear and my back.

I highly super strongly recommend this chair: http://www.cb2.com/studio-office-chair/f1138 (Euro Style Link: http://www.gotoeurostyle.com/prod_detail.asp?product_id=310)

UPDATE 2013.06.07: You can get this chair much much cheaper on Amazon - search for CB2 Bungee Chair, I bought another chair for me at work for $177. The company that manufacturers this chair is named Euro Style. Here is an example on Amazon.

CB2 Studio Office Chair
Two Hundred Dollars?!
Trust me it is worth the 200 bucks you are going to spend on it. I kid you not, I usually refuse to spend more than 25-50 dollars tops on an office chair, but the cheaper chairs all have the same problem, nice sturdy frame, but they are all usually very uncomfortable to sit in for about 10 hours.

Why do you sit in your chair for 10 hours? Are you crazy?
Why yes, I am crazy, I am what is called a programmer or developer and we are crazy. We sit for a living and we only use the muscles in our fingers and eyes for 8-20+ hours at a time non stop. Our brains fizzle out from time to time which is why it is nice to have a nice chair to sit on while we go into our brain dead states as we sip coffee. All joking aside, this chair rocks - it feels like you are sitting on nothing for the most part.

Monday, October 8, 2012

Simple Class Creator Published on Google Code

05/25/2014 UPDATE: I moved the code to a different project. I have made a number of changes since 2012.

11/14/2015 UPDATE: I moved my whole repository from GoogleCode to GitHub because GoogleCode shut down.

My Simple Class Creator is now available for download via SVN, click on the following link:
https://github.com/dyslexicanaboko/dyslexic-code/tree/master/SimpleClassCreator

Let Me Reiterate

Like I have explained before, this program is NOT intended to replace an ORM - the purpose of this program is to help people quickly model tables or queries (virtual tables) in a database. You can quickly create classes/objects for something you want to work with or manipulate without having to work with DataTables directly.

Realistically - who wants to write the basics of a class anymore - it is mundane annoying work. Hence why I wrote this program, I no longer write classes from scratch if they already exist in a table or as a query in the database.

Feel free to download or browse the code here:
https://github.com/dyslexicanaboko/dyslexic-code/tree/master/SimpleClassCreator

Features

  • Use either a Table Name or a Query to generate your code
  • Supports C# and VB.Net code generation
  • Supports WCF Attribute Tags
  • Choice of building out or using short hand for Properties - C# only
  • Choice of including a prefix for private class members - IE: _name vs. m_name
  • If you are generating this class from a single table then full starter CRUD is generated, otherwise just a generation method is created where a DataTable can be passed in and a List of your object is returned.

Why would I want to use this?

Well I don't know about you - but I hate writing class definitions, it is boring work. I also have to work in two languages, I use C# and VB.Net simultaneously so it helps to be able to generate either type of object from the same database without having to be dependent on a strict ORM. My templates are over simplified so nothing crazy is happening in the background, no contexts, the class generated is single tier POCO.

For the Potential Haters

For the people who want to tear apart my code because of my approach - don't bother me with your opinion because I don't care - I am serious. This is very simply for MODELING. So in other words if you don't like the approach DON'T USE IT. I will be upfront and say it is not efficient, but I don't care because this program is not running in a loop, it is running for less than a second to generate a class. I have saved hours of work by using this program and I am not tied down to an ORM.