This is beautiful code

If you’ve ever listened to Hanselminutes, you’ve no doubt heard Scott
mention “code smell” or “pretty code”.  The new language features in VB9,
including LINQ and XML being a native type, make XML generation not only easy,
but beautiful.  Aside from the color scheme, look at how smooth this code
snippet is.

Dim _order As New XElement(<Order>
    <ShipperRef><%= h.UniqueRef %></ShipperRef>
    <UniqueRef><%= IIf(h.CustomerPo <> "", h.CustomerPo, h.JdeNumber) %></UniqueRef>
    <Comments><%= _orderComments %></Comments>
    <OrderType><%= h.OrderType %></OrderType>
    <Workflow><%= h.Workflow %></Workflow>
    <RORRelationship><%= h.RORRelationship %></RORRelationship>
    <Supplier><%= h.SupplierNumber.ToString %></Supplier>
    <Customer><%= h.Customer.ToString %></Customer>
    <FreightBillableParty><%= h.FreightBillableParty %></FreightBillableParty>
    <HAZMAT><%= h.Hazmat %></HAZMAT>
    <GroupAssignment><%= SetGroupAssignment(h.GroupAssignment) %></GroupAssignment>
    <Weight><%= _orderWeight.ToString %></Weight>
    <Volume><%= _orderCube.ToString %></Volume>
    <OrderContact><%= h.PrimaryContactAssignment %></OrderContact>
    <ShipmentContact><%= h.PrimaryContactAssignment %></ShipmentContact>

2008 theme is DesertNights
, and the code was copied with CopySourceAsHtml.

LINQ Error: String must be exactly one character long.

When working with some Linq today, I kept getting the following error:

String must be exactly one character long.

This one had me scratching my head for a while.  Turns out the problem was in the Linq to SQL designer, and the way the code generator interpreted results from the view I was using.

The problem stems from the designer translating a varchar(1) to a system.char(1), a fixed length variable, instead of a system.string, which can have a variable length.  If the value is blank or NULL (allowable in the database), the fixed length condition isn’t met and an error is thrown.

To remedy this, go back to the designer, pin the Properties sidebar open, and look at the elements which have been mapped.  Any one which is mapped to Char(1) should be changed to a String.  This should fix your problem.

Below, the image on the left shows the improper mapping, and the image on the right shows the fixed mapping.