Scott Hanselman

Microsoft patch causing network outages/DNS issues?

September 03, 2007 Comment on this post [18] Posted in Musings
Sponsored By

Something has changed in the last week and I'm not sure what it is. I'm getting every some DNS lookups failing on my Vista machine. I'm getting reports from readers running XP that are seeing it too. One person saying:

"Microsoft released a patch recently that increased packet sizes and it is causing network/DNS issues. "

Can anyone speak to this? I haven't got details yet, but I'm running Network Monitor and haven't caught one yet.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Hanselminutes Podcast 79 - LINQ to XML

September 03, 2007 Comment on this post [2] Posted in Learning .NET | LINQ | Podcast | Programming | XML
Sponsored By

My seventy-ninth podcast is up. I've been poking around with LINQ to XML and I reports my findings to Carl about life with XDocuments and XElements. We also talk about the bridge classes that link (no pun intended) System.Xml and System.Xml.Linq.

If you have trouble downloading, or your download is slow, do try the torrent with µtorrent or another BitTorrent Downloader.

Links from the Show

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Check out their UI Suite of controls for ASP.NET. It's very hardcore stuff. One of the things I appreciate about Telerik is their commitment to completeness. For example, they have a page about their Right-to-Left support while some vendors have zero support, or don't bother testing. They also are committed to XHTML compliance and publish their roadmap. It's nice when your controls vendor is very transparent.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Hanselminutes Podcast 78 - The 2007 Tools List

September 03, 2007 Comment on this post [2] Posted in Podcast
Sponsored By

My seventy-eighth podcast is up. In this show, Carl and I talk about this year's Ultimate Developer and Power Users Tool List.

If you have trouble downloading, or your download is slow, do try the torrent with µtorrent or another BitTorrent Downloader.

Links from the Show

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Check out their UI Suite of controls for ASP.NET. It's very hardcore stuff. One of the things I appreciate about Telerik is their commitment to completeness. For example, they have a page about their Right-to-Left support while some vendors have zero support, or don't bother testing. They also are committed to XHTML compliance and publish their roadmap. It's nice when your controls vendor is very transparent.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Improving LINQ Code Smell with Explicit and Implicit Conversion Operators

August 30, 2007 Comment on this post [9] Posted in Learning .NET | LINQ | Microsoft | Programming | XML
Sponsored By

It's fairly easy to learn a language - programming language or human language - and get to the point where you can be understood. Your intent may not be crystal-clear, but at least it can be figured out but the computer/listener. However, it takes time and consultations with "native speakers" to get to be really fluent and start writing poetry.

ScottGu has a fine sample on how to make a Simple Web-Based RSS Reader with LINQ to XML. In it he's filling an ASP.NET ListView using RSS XML as the data source. He makes a query like this to populate a collection of posts.

var posts = from item in rssFeed.Descendants("item")
    select new
    {   
        Title = item.Element("title").Value,
        Published = DateTime.Parse(item.Element("pubDate").Value),
        NumComments = Convert.ToInt32(item.Element(slashNamespace + "comments").Value,
        Url = item.Element("link").Value,
        Tags = (from category in item.Elements("category")
                orderby category.Value
                select category.Value).ToList() 
}

Seems pretty straight forward. He's grabbing a number of things from the <item> tag in the RSS and putting them into an anonymous type. See how he's using "select new" rather than "select new Foo"? My code smell doesn't like the .Values, but I'm not that worried yet.

I added my own feed and ran his code and got a NullReferenceException on this line. Remember it's all one line, so that can be confusing. It was hard to tell which of these expressions was telling me something was null. I guessed though, that it was the line looking for <rss:comments> on the blog post. DasBlog, the engine I use, doesn't include a <comments> element when there's zero comments. Perhaps it's right, perhaps not, regardless, it doesn't include. So, for DasBlog, zero comments means no <comments> element. This code doesn't handle that because the call to item.Element(slashNamespace + "comments").Value is a NullReferenceException as item.Element(slashNamespace + "comments") is null.

So, how do I say "make NumComments = the value of <comments> unless it's null, then make it zero?" First, we tried this.

NumComments = Convert.ToInt32(item.Element(slashNamespace + "comments") != null ? item.Element(slashNamespace + "comments").Value : "0")

The use of ? :, as in <expression> ? true : false is very comment in early C#. Wordy, but it works. Of course, we're indexing twice into item.Element, once to see if it's null and again to get the value it if it's not.

This didn't smell right to me, but a few things smelled bad both here and before. I don't like seeing all the .Value properties. Also, the Convert.ToInt32 seemed dodgy. I kind of felt like I shouldn't have to do that, or that I should called XmlConvert, rather than Convert.

I also felt that I should be able to use the ?? operator, as in x = y ?? z, meaning make x = y if y's not null, else x = z. But that darned .Value property gets in the way.

A few newish C# things can make it cleaner (Thanks Anders).

Explicit vs. Implicit Conversions

Classes can use the explicit or implicit keywords to control how types are converted. You use implicit if you can guarantee that no data will be lost in the conversion and you use explicit if you can't guarantee that. Implicit conversions are like long foo = bar, where bar is an int. Ryan Olshan has some good examples like:

public static implicit operator int(MyClass myClass)
{
    return myClass.Value;
}

Then later, you'd do something like int x = someMyClassInstance and the conversion is implicit.

Back to the LINQ to XML RSS, example there's a whole pile (24, actually) of explicit conversion operators defined for the XElement class. Here's the one for int? (Nullable int):

public static explicit operator int?(XElement element)
{
    if (element == null)
    {
        return null;
    }
    return new int?(XmlConvert.ToInt32(element.Value));
}

See how it calls .Value for me? It does exactly what I want. Also, because it'll return null, now I can do this (changes in red):

var posts = from item in rssFeed.Descendants("item")
    select new 
    {   
        Title = (string)item.Element("title"),
        Published = (DateTime)item.Element("pubDate"),
        NumComments = (int?)item.Element(slashNamespace + "comments") ?? 0,
        Url = (string)item.Element("link"),
        Tags = (from category in item.Elements("category")
                orderby category.Value
                select category.Value).ToList()
    };

I changed a few calls to .Value to explicit string casts to emphasize the point since the explicit operator for string is just this. Same with DateTime where the call to Parse becomes a cast.

public static explicit operator string(XElement element)
{
    if (element == null)
    {
        return null;
    }
    return element.Value;
}

Now before you go off on how this can be used for evil. Yes, of course it can. However, there's a very unambiguous expectation presented with explicit and implicit. You only get to use them as a class designer if you can promise they will work as expected.

In this case, I think it makes the code much cleaner. Here's the before and after again:

BEFORE:

var posts = from item in rssFeed.Descendants("item")
    select new
    {   
        Title = item.Element("title").Value,
        Published = DateTime.Parse(item.Element("pubDate").Value),
        NumComments = Convert.ToInt32(item.Element(slashNamespace + "comments") != null ? item.Element(slashNamespace + "comments").Value : "0"),
        Url = item.Element("link").Value,
        Tags = (from category in item.Elements("category")
                orderby category.Value
                select category.Value).ToList()     
    };

AFTER:

var posts = from item in rssFeed.Descendants("item")
    select new 
    {   
        Title = (string)item.Element("title"),
        Published = (DateTime)item.Element("pubDate"),
        NumComments = (int?)item.Element(slashNamespace + "comments") ?? 0,
        Url = (string)item.Element("link"),
        Tags = (from category in item.Elements("category")
                orderby category.Value
                select category.Value).ToList()
    };

Now NumComments will handle missing <comments> elements and things look tidier. Very cool. Thanks ScottGu and Anders for their help, direct and indirect.

It's going to take a while but a new, more refined sense of smell is in my future, I think. One doesn't have to necessarily remember that ?? and conversion operators exist, but rather to have a better "sense" when reading/writing code that there ought to be a cleaner way and then go looking for it.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Fixing Instance Failure when connecting to SQL Server 2005 Express

August 30, 2007 Comment on this post [8] Posted in ASP.NET | Learning .NET | Programming
Sponsored By

I was getting Instance Failure when connecting to my SQL Server Express 2005 Database in my C# ASP.NET application.  Very weird.

Instance failure. - Windows Internet Explorer

<connectionStrings>
    <add name="NorthwindConnString" 
connectionString="Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient"/> </connectionStrings>

Trial and error just taught me that the problem was "Data Source=.\\SQLEXPRESS;" - It's the double \\. That's an escape sequence in C#. Everything worked when I switched the connection string to .\SQLEXPRESS.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.