2005 2006 2007 2008 2009 2010 2011 2015 2016 2017 2018 aspnet azure csharp debugging docker elasticsearch exceptions firefox javascriptajax linux llblgen mongodb powershell projects python security services silverlight training videos wcf wpf xag xhtmlcss

Atlas October 2005

If you are anything like me, you consider a technology is new as long as it's marked as experimental. Atlas still qualifies for being new...or even, pre-new. In any case, the October 2005 version of Atlas has been released and can be downloaded at the link below.

http://atlas.asp.net/ You can find a list of changes here: http://weblogs.asp.net/atlas/

Note that unlike WCF and WPF, which in my mind are stable for production, Atlas is still experimental and is only in the technical preview stages. Not that it will explode into pieces, but there is little to no documentation on almost all of Atlas and given the early nature of the product, things are almost guaranteed to change.

Cool SMS/VS2005 Integration Feature

Today I discovered a very wierd feature regarding SQL Management Studio 2005 ("SMS") and Visual Studio 2005 (of course I'm using the FREE standards editions from the MSDN/Technet seminars)

OK so here it is...

  • Open SMS
  • Navigate to a table and modify it.
  • Copy the text of one of the columns
  • Go to an ASPX page in Visual Studio 2005 and paste.

If you did it right you will see the weirdest thing in the world: it pastes a GridView linked to a SqlDataSource, which it also pastes.

<asp:GridView ID="GridView1" runat="server"
     DataSourceID="SqlDataSource2"
     EmptyDataText="There are no data records to display."
     AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="ContactID"
            SortExpression="ContactID"
            HeaderText="ContactID">
        </asp:BoundField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    SelectCommand="SELECT [ContactID] FROM [Employee]"
    ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString1 %>"
    ProviderName="<%$ConnectionStrings:AdventureWorksConnectionString1.ProviderName %>">
</asp:SqlDataSource>

You will also find that it pastes the appropriate connection string directly into your web.config.

<add
 name="AdventureWorksConnectionString1"
 connectionString="[string omitted;  it was LONG"
/>

Cool, huh?

p.s. If you want to paste that particular name, as I wanted to do, you can always do the old school paste-into-notepad-copy-out-of-notepad trick that is a tried and true way to strip off Web Browser formatting.

"Common Language Runtime detected an invalid program"

If you've ever gotten this error you've probably been one of the most confused people in the world. In your debugging you may have seen that this message came from an InvalidProgramException.

Well, while this problem should be rare, here's an example harness of where you may see it.

// ThrowerHarness.cs
namespace ThrowerExample
{
  class ThrowerHarness
  {
    static void Main(string[] args)
    {
        try
        {
            Thrower.Start( );
        }
        catch (System.Exception ex)
        {
            System.Console.Write("Error: " + ex.Message);
        }
      }
    }
}

Alright, so where's it coming from?

In this case, it's actually coming from the IL.

// Thrower.il
.assembly ThrowerLib { }

.class public Thrower {
  .method static public void Start( ) {
    stloc.0
    throw
    ret
  }
}

This actually comes from invalid IL. In this case I'm putting 0 on the stack and then throwing...well, nothing really. This is not something a good compiler would create. If you see this it's probably a bug in the compiler or manually written IL.

Anyhow, to test the above do this...

ilasm /dll ThrowerLib.il
csc /r:ThrowerLib ThrowerHarness.cs

.NET Course I'm teaching

So, for a while now I've been teaching a .NET 2.0/C# 2.0/Object-oriented desing class and decided to come up with a simple class website.

I'm covering not only the basics, but also many advanced topics that you would only find in footnotes. Mainly I'm going to be covering stuff that I wish someone would explain to me in plain english!! So, I'm going to be spending time explaining the differences between ref and out, const and readonly, and try{}catch{} and try{}catch(Exception ex){} among other topics. You can bet that I'll be spending A LOT of time just on delegation!!

There are actually a few series going on at once: C# Language (Intro, Intermediate, Advanced), .NET Framework and Design, Object Orientation and Design Patterns, SOA and Enterprise Development, ASP.NET 2.0 (web-standards "tableless"/CSS!!!), and WinFX concepts are the series all going on in parallel.

As far as the intro ASP.NET stuff is concerned, there are great ASP.NET books out for that kind of stuff. I'm mainly focusing on the middle layer and more advanced concepts. I'm actually planning a lecture for Microsoft Atlas. Given my love for Firefox, .NET, and remote scripting (err, Ajax), that should be fun! Also in the course of things I plan on having a lecture to discuss .NET design guidelines and another lecture on the architecture and mechanics of the CLR! .NET assembly language (yeah, yeah, the IL) rules!

Now what is my supplemental book recommendation? Basically anything that Troelsen has written. His latest book "Pro C# 2005 and the .NET 2.0 Platform" is written incredibly eloquently.

I absolutely love .NET... and this should show! maybe I can get an MVP out of this :D hint hint!!

Anyhow, here's the page I threw togther for it. I'll be posting more information, links, and samples as time goes along.http://www.davidbetz.net/dotnetcourse/

New Blog Separation

Alright! So I decided to separate my professional information from my personal by creating a few separate blogs instead of having them all crammed into my website. From here on out, my website at davidbetz.net will be for my own stuff and this one here in particular will be for WinFX.

In the next few weeks I plan on putting up a lot of new information and hopefully I'll keep up a nice flow of blogging...

.NET 2.0 trycatch and trycatch(Exception ex)

One thing that I found rather interesting about .NET 2 that's different from .NET v1 is how v2 handles non-exception throwables differently.

First, a little background...

In case you don't know, the following two pieces of code are completely different.

try {
    Thrower.Start( );
}
catch (System.Exception ex) {
    System.Console.Write("Error!");
}
try {
    Thrower.Start( );
}
catch {
    System.Console.Write("Error!");
}

The difference is not that the first catches exceptions from unmanaged code and the second second doesn't, the first actually does catch those exceptions. The difference is also a bit more important than the mere fact that in the first one case we capturing the exception data in an exception type and in the other we aren't. The real difference stems from the fact that that in .NET you can throw things other than exceptions and in v1 you used the latter code to catch those non-exception throwables.

Here's an example of how to throw a non-exception. Yeah, you basically have to use the IL, or a compiler that doesn't check for this stuff.

// ThrowerLib.il
.assembly ThrowerLib { }

.class public Thrower {
    .method static public void Start( ) {
        ldstr "Oops"
        throw
        ret
    }
}

Here we are throwing "Oops". That's obviously a string, not something derived from System.Exception. In v1 you could use try{}catch(Exception ex) {} as a catch all for all exceptions, but that would not catch this error as it's not an exception. To catch this, you would also have a catch{} at the end.

OK, so how's v2 different? In this world, the non-exception item thrown is wrapped up in a real exception. So, in .NET v2 you can actually use your normal try {} catch(Exception ex){ } to also catch these non-exception throwables as well.

They are actually thrown as a RuntimeWrappedException. So the following app compiled an run in .NET v1 would explode into pieces, but would would gracefully end in .NET v2.

// ThrowerLib.il
.assembly ThrowerLib { }

.class public Thrower {
     .method static public void ThrowException( ) {
         ldstr "ThrowException exception from the IL world!"
         newobj instance void [mscorlib]System.Exception::.ctor(string)
         throw
         ret
     }

     .method static public void ThrowString( ) {
         ldstr "Weird exception!"
         throw
         ret
     }
}
// ThrowerHarness.cs
namespace ThrowerExample
{
    class ThrowerHarness
    {
        static void Main(string[] args) {
            try {
                Thrower.ThrowException( );
            }
            catch (System.Exception ex) {
                System.Console.WriteLine("System.Exception error: " + ex.Message);
            }
            catch {
                System.Console.WriteLine("Non System.Exception based error.");
            }

            try {
                Thrower.ThrowString( );
            }
            catch (System.Exception ex) {
                System.Console.WriteLine("System.Exception error: " + ex.Message);
            }
            catch {
                System.Console.WriteLine("Non System.Exception based error.");
            }
        }
    }
}

A few .NET 2.0 life simplifiers...

I'm starting to realize that not everyone knows everything or watches every blog in the cosmos, so I'll start including blog entires which will help people very efficiently master .NET 2.0 and WinFX.

One thing that will help people who currently fight with .NET 1.x is the following video I saw earlier this month. It's an explanation of some of the life simplifiers in .NET 2.0.

One of my favorites they talk about is is the addition of a Contains( ) method for things. Now instead of typeing "string".IndexOf("str") > -1, we can use Contains( ). My next favorite thing...well, about a tie, would be the additions of the String.IsNullOrEmpty(string) method to replace if(myString == null || myString.Length < 1). Nice...

OK, so here's the video...

"Favorite v2.0 Features in the Base Class Libraries"

XHTML 1.1 and DataBinding (Chapter Excerpt #2)

When doing serious data binding in ASP.NET you may want to reconsider using XHTML 1.1 or XHTML 1.0 Strict at all. The rule is simple: use the document type that can be deterministicly proven to be proper in your situation. Put another way, unless you have deterministicly proven that there will never be any invalid markup in the data, you should always use XHTML 1.0 Transitional.

If the binding data has some odd markup, then you will end up sending invalid XHTML 1.0 sent to IE breaking validation or sending invalid XHTML 1.1 markup to browsers such as Firefox, halting the rendering.

But what does it mean to be deterministicly proper? To put it simply it means to absolutely garuntee that the data will always be proper, that is, to be able to always predict the properness of data. This does not mean "well, it worked for 100,000 tests, so it's good enough", but rather it means that it absolutely will always work 100% of the time. You can get this level of determinism by looking at the symantics of what is going to be bound. For example, if you are binding a table with numbers, which will always be numbers, then you should have a level of determinism here. However, if you are binding a table with unvalidated under input, then you do not have determinism as you have no idea what a user will input. The user could input <b><i></b></i>, which will break the page. You have no idea. Having a proven, not demonstrated, view of data is what this is all about.

Here are a few guidelines that should help you with determining what is and not deterministic.

These things are never deterministic...

  • Unvalidated user input
  • Unvalidated external data
  • HTML
  • Anything else with angle brackets (<, >), except wellformed XML

Given symantical care, these things should be deterministic...

  • Wellformed XML
  • Alphanumeric strings
  • Base64 encoded data
  • Alphanumeric strings

To reiterate: only use the document type that is deterministicly proven to always be proper.

XHTML 1.1 Escaping (Chapter Excerpt 3)

One thing you will inevitably notice when working with the ultra strict XHTML 1.1 is how the ampersand (&) works. In HTML you never had to worry much about the ampersand, though you could use it to implant an HTML space (&nbsp;). Now given that XHTML is XML, you simply can't get by with the rules of HTML. In the XML world, if you want to save an XML document with an ampersand, then you have to escape it. If you are familiar with C-based programming languages, then you know all about special characters like this. In those languages you have to "escape" certain characters such as the backslash (\), with another backslash.

In fact, in Altova's XMLSpy application you will get a stopping warning if you have XML that looks something like <b>&</b>. The appropriate way to have an ampersand in XML is to write it as &amp;. This is very similar to what you could do with the   space in HTML and it's not that far off from what we already do. For instance, if you wanted to write &nbsp;. In fact if you want to write &amp;nbsp; you have to write &amp;amp;nbsp. So, if you know HTML, you already know the fundamentals of this rule. It's just that in the strict world of XHTML 1.1, you can no longer let little mistakes enter your code.

Now this rule about dealing with ampersands doesn't end with standalone ampersands, but also applies to ampersands in links. For instance, you can't just have a link like default.aspx?id=3&category=5. Your link has to transform to default.aspx?id=3&category=5.

Fortunately ASP.NET 2.0 is smart enough to deal with things like this in your databinding. For instance, look at the following code...

ASP.NET 2.0 Code

<asp:GridView ID="gvLinks" runat="server"></asp:GridView>

Code Behind

C#

String[] links = new String[] { "http://jampad.net/default.aspx?id=1&name=2", "http://jampad.net/default.aspx?id=1&name=2&frog=3", "Amburgers & Wootbeer" };
gvLinks.DataSource = links;
gvLinks.DataBind( );

Believe it or not, ASP.NET will output produce the following...

<table cellspacing="0" rules="all" border="1" id="gvLinks" style="border-collapse: collapse;">
    <tr>
        <th scope="col">Item</th>
    </tr>
    <tr>
        <td>http://jampad.net/default.aspx?id=1&amp;name=2</td>
    </tr>
    <tr>
        <td>http://jampad.net/default.aspx?id=1&amp;name=2&amp;frog=3</td>
    </tr>
    <tr>
        <td>A&W</td>
    </tr>
</table>

While I'm not a big fan of looking at table code, I am however a huge fan of automation. As you can see here, you don't have to fight with manually escaping data binding ampersands. Thus, you can give ampersands in XHTML data binding a vote towards deterministic.

'+' sign in e-mails

Announcement: Plus signs in e-mail addresses ARE VALID! I can't tell you how many time I went to a blog, a forum, or a (poorly design web...)store and my e-mails didn't pass validation. On some blogs I even get ASP.NET exceptions.

Plus signs in e-mails are commonly used for sub-addressing. When you have an e-mail like help@gmail.com and you don't want to give that to a webstore you can simply hand out help+nospam@gmail.com or help+Amazon@gmail.com or whatever and you're still good.

So PLEASE... if you wrote a buggy application or script that disallows '+' in e-mail addresses, fix your bugs.