Displaying articles with tag DataSet

LINQ: OrderBy with nullable columns in TypedDataSets

Posted by gabriel, Mon Aug 11 12:24:00 UTC 2008

This article shows a tip on how you can do sorting using the LINQ OrderBy with Typed DataSets.

For our example I’ll use the the Northwind Database and it’s Customers table.

I’ll use two columns in this table. One is the ContactName which doesn’t have any null values and the Region column which has null values.

First lets populate the DataTable we want to query using TableAdapter I had previously generated in the DataSet.


CustomersTableAdapter ta = new CustomersTableAdapter();
DataSetNorthwind.CustomersDataTable dt = ta.GetData();

I want to use LINQ select all values ordering them by the Region column (which may be null). So here is the initial idea:


            var query = from r in dt.AsEnumerable()
                        orderby r.Region
                        select r;

Ok, no compilation errors but when you run the query you will get an error:

The value for column ‘Region’ in table ‘Customers’ is DBNull. -> System.InvalidCastException: ...

If you think about it this is expected. The nullable columns in the DataSet all have an IsNull method to check if the column is null or not and avoid this kind of error. We are going to use the IsRegionNull() method in our favor to help us solve the problem:


var query = from r in dt.AsEnumerable()
        orderby (r.IsRegionNull() ? "" : r.Region)
        select r;

I used the ternary operator to check if the column region is null or not. If it is then I’ll use an empty string as the value, if it’s not null I can call the Region property to get the value.

To see the ending result you can print all the values:


foreach (DataSetNorthwind.CustomersRow row in query)
{
     Console.WriteLine("{0} - {1}", row.ContactName, 
          row.IsRegionNull() ? "" : row.Region);
}

Of course you could have filtered out the rows with null regions, but the goal here is to show what you can do to order the rows when you have a column with values that may be null.

0 comments | Filed Under: Tips and Tutorials | Tags: DataSetdataset

DataSet Designer (Editor) Missing

Posted by gabriel, Sat Jun 14 15:03:00 UTC 2008

This week one of my co-workers had a strange problem with his VisualStudio 2008. All the sudden he couldn’t view the DataSet designer in any applications. When the DataSet file was clicked all that it showed was the XML file, no sign of the Designer.

We don’t know what happened but as I tried to figure out the problem I found that there’s very few information about this problem on the Internet, so I decide to blog about it so that it might help someone in distress.

If you’re having this problem you might first try to right click the DataSet file and then select the Open With… option. A list of editors will show, if you don’t see the DataSet Editor then you you have the same problem we did.

I have found a few posts about it, here are the solutions proposed:
  • Run the following command in the VisualStudio prompt: devenv /resetsettings
  • Run the following command in the VisualStudio prompt: devenv /setup
  • With Visual Studio setup disc select the Repair option.
  • Re-install Visual Studio.
All these are valid solutions and I found people on the web who claimed that this helped them but for my unfortunate teammate it didn’t help. The only thing that did it for us was:
  1. Remove VisualStudio
  2. Delete any folder left from VS installation
  3. Install VisualStudio

Finally, problem solved! I know this is not an optimal solution, far from it but at the end of the day it works. Nothing else did. I hope this post helps some else.

1 comment | Filed Under: | Tags: DataSet