Extend Visual Studio 2005 DataSet Designer Using Partial Classes

One of my favorite features of Visual Studio 2005 is the easy to use DataSet designer. If you haven’t tried using the DataSet designer to create your data access layer try it out. Brian Noyes has written a nice tutorial here.

However, one issue I have with the DataSet designer is that it doesn’t give you the ability to create a SqlDataReader from the design interface. Fortunately there is a way around this limitation. Using partial classes you can extend the DataSet designer’s built in functionality to add your own methods. Brian Noyes touches on this in his tutorial I referenced above.

I have a little cheat to share that makes adding a SqlDataReader method to your TableAdapter a snap.

First off, you need to use the designer to create a TableAdapter. Have it create a GetData method for you.

Next you need to create a new class file for your project. I recommend naming it something like ‘<the name of your DataSet xsd file>Custom.cs’. That way it’s easy to identify.

Now you need to extend the namespace of your DataSet, and create a partial class for the TableAdapter.

Here comes the cheat.

Highlight the name of your TableAdapter you just created a partial class for. Right-click on it, and click on “Go to Definition.” This will take you to the Microsoft generated code for your TableAdapter. Below the definition code you should be able to find the ‘GetData’ method. Copy that entire method block and retrofit it into the partial class you created for your SqlDataReader. Now simply replace the section of Microsoft generated code that creates and returns a DataTable with code that returns a SqlDataReader.

When you’re done you’ll end up with a class file that looks something like the following.

using System;
using System.Data;
using System.ComponentModel;
using System.Data.SqlClient;

namespace DataSetDirectTouchTableAdapters
{
    public partial class displayResponseQuestionTableAdapter : Component
    {

        public virtual SqlDataReader GetReader(System.Nullable ResponseSet, System.Nullable Question)
        {
            this.Adapter.SelectCommand = this.CommandCollection[0];
            if ((ResponseSet.HasValue == true))
            {
                this.Adapter.SelectCommand.Parameters[1].Value = ((int)(ResponseSet.Value));
            }
            else
            {
                this.Adapter.SelectCommand.Parameters[1].Value = System.DBNull.Value;
            }
            if ((Question.HasValue == true))
            {
                this.Adapter.SelectCommand.Parameters[2].Value = ((int)(Question.Value));
            }
            else
            {
                this.Adapter.SelectCommand.Parameters[2].Value = System.DBNull.Value;
            }
            this._connection.Open();
            return this.Adapter.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
    }
}

After you rebuild your project you can access the GetReader method via Intellisense! Good times. 🙂

Localhost doesn't work? Can't debug your .Net web app? This may be your answer.

For my very first post I’d like to share the solution to a problem I solved a month or two ago, but it was such a pain in the ass I’m certain someone will find this post useful.

When you run the debugger in Microsoft Visual Studio .Net 2003 & 2005 for a website project, by default Visual Studio opens up your site in a new web browser using the localhost path. Unfortunately for me, my localhost path was hosed for, what was then, some unknown reason. So when I clicked the debug button my browser would look for a few minutes for the site, then give me a page not found error. Along with this failure to find the page, while my browser was looking, my CPU utilization was running at 100%!

To get around this problem in Visual Studio 2003 I would simple start the debugger, and then as soon as my browser window opened, I clicked Stop. I then typed in my computers IP address in place of localhost in the address bar, and the browser would then happily find my website. This worked fine in 2003 because it relied on IIS to host my developing web application.

Unfortunately this solution did not work when I migrated to Visual Studio 2005. By default when you debug a web application in 2005, the site is opened in a virtual IIS web server. So my old tricks didn’t work. Now unable to debug my web apps I had to find the solution to my original problem. Why isn’t my localhost working?

I was able to ping my machine name, my IP address, localhost, and even 127.0.0.1. My Host file was untainted and correct. I even tried altering the Host file to make localhost point to my IP address. I could access websites in IIS using every address method EXCEPT localhost!

So I remoted into a development box I knew localhost worked on. I then ran IPCONFIG on that box and my own. Then I tried to make the results of my IPCONFIG identical to the computer that worked.

I noticed my computer had a lot of extra crap in the IPCONFIG that looked something like this:

c:\>ipconfig
Windows IP Configuration
Ethernet adapter Ethernet:
 Connection-specific DNS Suffix  . : wcoast.corp.example.com
 IP Address. . . . . . . . . . . . : 157.54.139.57
 Subnet Mask . . . . . . . . . . . : 255.255.252.0
 IP Address. . . . . . . . . . . . : 3ffe:ffff:8311:f282:1460:5260:c9b1:fda6
 IP Address. . . . . . . . . . . . : 3ffe:ffff:8311:f282:b973:4db8:97e2:e978
 IP Address. . . . . . . . . . . . : 3ffe:ffff:8311:f282:200:39ff:fe0e:fc35
 IP Address. . . . . . . . . . . . : fec0::f282:200:39ff:fe0e:fc35%1
 IP Address. . . . . . . . . . . . : fe80::200:39ff:fe0e:fc35%4
 Default Gateway . . . . . . . . . : 157.54.136.1
                                            fe80::210:ffff:fed6:58c0%4
Tunnel adapter 6to4 Tunneling Pseudo-Interface:
 Connection-specific DNS Suffix  . : wcoast.corp.example.com
 IP Address. . . . . . . . . . . . : 2002:9d3b:8b39::9d3b:8b39
 Default Gateway . . . . . . . . . :
Tunnel adapter Automatic Tunneling Pseudo-Interface:
 Connection-specific DNS Suffix  . : wcoast.corp.example.com
 IP Address. . . . . . . . . . . . : fec0::f70f:0:5efe:157.54.139.57%1
 IP Address. . . . . . . . . . . . : 3ffe:ffff:8311:f70f:0:5efe:157.54.139.57
 IP Address. . . . . . . . . . . . : fe80::5efe:157.54.139.57%2
 Default Gateway . . . . . . . . . : fe80::5efe:157.56.253.8%2

I googled some of the strange IP addresses then I learned about IPv6.

IPv6 is a new IP address protocol that basically gives us more addresses than IPv4. As the internet grows we’re gradually running out of available IP addresses IPv6 is the solution. IPv6 snuck onto my computer at some point during a Windows update. This is all well and good but for some reason it was killing my localhost path! After I uninstalled it, WHALAH! Localhost started working again, and I could properly debug my web apps in Visual Studio 2005.

To remove ipv6 in Windows XP with SP2, Windows XP with SP1, or Windows Server 2003 go to Control Panel -> Network Connections then double click the network card / adaptor you’re using. Under “This connection uses the following items” section you should see “Microsoft IPv6 Developer Edition” or “Microsoft TCP/IP version 6”. Select it and click uninstall.

If that doesn’t work, or you have Windows XP with no service pack installed, try running “netsh interface ipv6 uninstall” or “ipv6 uninstall” in Windows command prompt. Then go patch your OS!

If anyone knows why IPv6 was killing my localhost path, or you know how to have IPv6 installed and keep localhost working, I’d love to hear from you.

Happy debugging.

References:
IPv6 for Microsoft Windows: Frequently Asked Questions