CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

John Papa [MVP C#]

.NET Code Samples, Data Access, and Other Musings

February 2006 - Posts

  • Use the Right Tool for the Right Job - ExecuteScalar or ExecuteNonQuery

    Would you use a drill to pre-drill a hole for a nail to hang a picture? I wouldn't either. Just hammer the nail into the wall. OK, need something more code related? Would you define a class with a single property and no other members just to store a string? Me neither.

    public class foo{

       public string goo = "";

    }

    This might seem like an obvious point to most, and I sure hope it does. But recently I worked on a project that just made me think "WTF"?  I saw some ADO.NET code that was intended to execute an UPDATE statement against a table in a SQL Server database. It worked just fine and dandy, but I had to take a second look because the code was issuing the ExecuteScalar method. At first I though, maybe the code was actually returning a value and not executing an UPDATE statement, so I looked at it again thinking I missed something. Nope, it was just and UPDATE statement.  So then I wondered if the UPDATE statement was also returning a value after the UPDATE ... looked again and nope, it wasn't. This code was simply doing something like this:

       string sql = "UPDATE tableX SET columnA = value1 WHERE columnB = value2";

    So why was this code using ExecuteScalar to run this SQL? I started looking around the project that I inherited and found several other places that ExecuteScalar was used ... most of which were also issuing a simple action query with no intention of getting a result back. The code looked like this:

       cmd.ExecuteScalar();   

    That's not psuedo code, there was nothing on the left hand side to store the return value of the ExecuteScalar method. So I started to refactor the code to change the places that were using ExecuteScalar but not actually getting a return value into code like this:

       cmd.ExecuteNonQuery();

    Use ExecuteScalar to execute a query to return a single scalar value and use ExecuteNonQuery to execute an action query and return no values. Is this basic stuff you already know? Very likely (or hell yeah!). In fact, that is why I looked so hard when I first saw this code ... I thought, geez, I must be missing something. Like most jobs, using the right tool for the right job just makes more sense.

     

  • Jack Bauer - The Developer

    Very funny post by Steve Joubert, if you like the TV show 24. Check it out here.

    For a non-programming list, check this Jack Bauer list out.

  • SQL Server 2005 - Using XQuery and Large Datatypes

    The March 2006 issue of MSDN Magazine is now posted online. My latest Data Points column answers a few questions I have received from readers reagarding how the XQuery works with the SQL Server 2005 XML datatype as well as the new large datatype enhancements. 

    You can read the article online here.

     

    In the upcoming issues I'll be getting into some more of SQL Server 2005's new features including exception handling, EventData and DDL triggers. I'll also focus on a SQL Server Reporting Services article in the near future.

     

    Posted Feb 12 2006, 12:49 PM by John Papa with no comments
    Filed under: ,
  • Interviewing - Understanding the Real Question

    A week or so ago I posted some interviewing tips including some do's and some don'ts. Since that post I have had several former colleagues and former employees of mine ask me to post about some of my more unorthodox interview techniques. When I interview a candidate and I think that person has the right stuff technically and in experience, I look for the “other” factors. What “other” factors? …

    … Have you ever watched that famous scene from one of the several Robin Hood movies where Robin or one of his merry men (hard not to chuckle here) are taking aim with the arrow and someone says something to the effect of “OK, you can make the shot. But can you do it amidst distractions?”.  And then of course he misses the shot when the girl whispers in his ear. My point … don’t let anyone whisper in your ear when shooting an arrow… no no no …. what I mean is that people get all pumped up for interviews. They practice what they say and are comfortable with it. So when I know I like the qualifications but I am unsure how they will perform when under pressure, I try some different techniques.

    OK, it is not rocket science … its just a way of finding out how a person is going to handle a late night at the office, figuring out a solution to a problem they have never faced, the production web server crashing or some other work related crisis. Its just another test … nope, it ain’t perfect and it can be beaten, but it certainly has helped me weed out some candidates. Some people are great in all areas until I get to this point in the interview. I start asking them questions that get progressively more difficult to see how the react. Once I get to the point where they don;t know the answer it gives me a lot of insight. Most people get a little nervous (which is fine) at this point and some freeze completely. I have had several people just not answer at all when asked a question they don’t know the answer to. The thing I look for is the people who can handle themselves in a pressure filled situation when the answer is not obvious. Warning signs for me in this situation are:

    • the person won’t let us move on until they find the right answer (sometimes they will wait forever unless I make them move on to the next question)
    • yelling (yup, I had a guy just start yelling out answers. he wasn’t mad, I don;t think, just nervous yelling)
    • sudden case of the BS (this is never good … never lie … never never never!)

    What I am really looking for is:

    • simply a brief “I don’t know”
    • even better answer is “I can find out by doing XYZ”
    • calm demeanor
    • thinking out loud … considering possible options

    The key here is to realize that sometimes the question verbalized is not the question that you are answering. In this example I don’t care if the person knows the 7 layers of networking in order or the definition of 3rd normal form … what I care about is seeing how they respond to pressure. I want to hire people who can solve problems, not people who have memorized syntax or text books. People who can work with others by identifying a problem and going a step further and offering a few solutions and making a recommendation.

    I always ask the questions calmly, professionally and always let the person go with it where they will. I really let them drive the conversation to see where they take it. I don’t do this very often as I only call upon this technique when I think I have a winner but I just need to see how they will do under pressure. In fact, some of the best people I have hired were the worst resumes that got in the interview door but just had the “IT” factor. This technique helped show me that these people could handle pressure and a lot of them really blossomed into great developers, business analysts, and managers.

    Do I know what I am talking about? Eh, who knows. It has worked for me. Just something to consider when interviewing … always relax and be yourself and you’ll be a step ahead.

  • Setting a Stylesheet via DHTML

    Ever wanted to designate a style sheet via DHTML? Well, I hadn't needed to until recently. I have an application that reads an XML data island which tells the page the file name of the style sheet to use. (It could be any file of a hundred files, so I don;t want to load alternate style sheets in this case.) So here is the solution I decided on ...

    First ... I created a link tag with an empty href reference. The DOM doesn't store add an item to the document's stylesheet collection unless is has an href attribute.

    <link id="mainStyle" href="" type="text/css" rel="stylesheet">

    Next, I created some client side javascript that finds locates the mainStyle stylesheet reference and associates it to a specific URL where the stylesheet can be found.

    <script>
    <!--//
       document.styleSheets.item("mainStyle").addImport("../css/mystyles.css");  
    //-->
    </script>

    Of course, I don't hard code the path to the css file. Instead, I read the path from the XML data island and use that. Not much to it, but it does the job. :-)

  • The Forgotten Woes

    We're all celebrating the recent advancements in .NET and other technologies like LINQ, XML in SQL Server and the fascinating tools like CodeRush and ReSharper. But it struck me this week that we haven;t gone far enough in some areas ... like DHTML. For example, have you tried to write client side code in a browser application lately? I have, and its not as easy as it should be. Intellisense only bring up a portion of the actual methods and properties that the objects support and some objects don;t show up at all. Yeah, yeah, I understand the DOMs are different based on browser brands and version .. ... but what I don't agree with is how the built in tools have not progressed to the point where we get intellisense and reliable client side debugging. I have never been able to consistently get client side debugging to work on my PC. Yes, some days it works, some it doesn't. The point is that all of my server side tools are so far ahead of the curve in these matters, so why should I have to look up the DOM, find the object I want, then search to find some example of using it that makes sense?! I have to think someone has a tool that does this ... there is even an intellisense tool for writing T-SQL.

    I'd love to be wrong on this ... so if someone out there knows of a tool that helps with DHTML/JScript intellisense please reply to this post. I am all ears.

     

  • Full Day of ADO.NET 2.0 at VSLive Orlando

    I just confirmed that I’ll be presenting a full day workshop at VSLive in Orlando in May 2006 in addition to a regular 1 hour session titled Exploiting System.Transactions and ADO.NET 2

    I'm really looking forward to the workshop ... I have a lot of material set aside for it that I am sorting through. In fact, I put together an abstract for VSLive that was waaaaaay too long for the brochure, so they had to reel me in :-) Anyway, here is the VERY brief summary of what I'll be discussing and demonstrating:

    Building Data Driven Solutions: Developing with ADO.NET 2.0

    John Papa, ASPSOFT

    Data access using ADO.NET is at the core of most .NET enterprise applications. While ADO.NET 1.x is powerful, its successor ADO.NET 2.0 contains several new features that can benefit an application. In this workshop we will review the ADO.NET 2.0 model and discuss its how its integral parts work together and where they can benefit an application. We’ll address:

    ·        ADO.NET’s connected and disconnected objects

    ·        Performance and maintenance in your enterprise solutions

    ·        Detection and handling of concurrency violations using ADO.NET and SQL Server

    ·        SQL Server 2005 and data access strategy improvements

    ·        Building a solid Data Access Layer (DAL), how to choose the right one for an application, and how the Enterprise Library’s DAAB wraps ADO.NET 2.0

    I've got a lot to cover in this session and obviously the abstract/description above barely scratches the surface. If you are into data like I am, it should be very interesting.

    Posted Feb 05 2006, 10:19 PM by John Papa with no comments
    Filed under:
  • Be Careful - It's a Scam!

    It seems that somebody has a scam going on where they send you this notice in the mail (USPS mail) asking if you want to get MSJ magazine subscription. Stephen Toub at MSDN Magazine caught wind of this from a reader of MSDN Magazine and has stated this is a scam. OK, if you follow MSDN Magazine you probably already know that MSJ (Microsoft Systems Journal) no longer exists. It is one of the predecessors of MSDN Mag. Anyway, I suggest that if you get a notice in the mail asking you to subscribe to MSJ do not send your money to these people.

    Thanks to Stephen and to the all the readers who caught wind of this scam for letting us all know.

     

    It seems ComputerWorld even followed up on this scam with this article.

  • Exploiting System.Transactions at VSLive

    Just confirmed that I’ll be at VSLive in Orlando in May 2006. I'll be presenting on one of my favorite topics: System.Transactions.  The session is titled Exploiting System.Transactions and ADO.NET 2 and is now tentatively scheduled for May 17th at 11:45am. You can check out the preliminary schedule online.

    My boss, Jon Goodyear, is also presenting at VSLive Orlando in May. His session is titled New Debugging Features for Visual Studio 2005 and is at 4:30 on May 17th. So we've got 2 reps from ASPSOFT there :-)

    Here is a brief summary of what I'll be demonstrating/discussing:

    Exploiting System.Transactions and ADO.NET 2
    The.NET Framework v2 includes the System.Transactions namespace that enhances transactional support for managed code. In this session, I will discuss how System.Transactions can handle transactions without using other common practices such as deriving from a ServicedComponent, using interception, or using reflection. Designed to integrate with SQL Server 2005, System.Transactions supports the intelligent and automatic promotion of local lightweight transactions to fully distributed transactions. It also introduces a new class called LightweightTransactionManager, which is a faster alternative to using the DTC for local transactions. Example will include modifying the default transactional settings of the implicit local transaction, the isolation level, timeout period and transaction’s context. I will also demonstrate how and when the System.Transactions namespace delegates the promotion of a lightweight transaction to a fully distributed transaction as well as how to use explicit transactions, and manually choose when and if to enlist in a transaction context.

     

     

    Posted Feb 01 2006, 12:01 AM by John Papa with 5 comment(s)
    Filed under:
More Posts