Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives


April 2005 - Posts

VB.Net versus C#

I don't want to start another discussion on which language is superiour. But I do find it interesting what others think. At The Code Project, two articles sparked my interest.

Complete comparison for VB.net and C#
This article explains about advantages, differences and new features of VB.NET and C#, including VS 2005.

Not another C# versus VB article
This article explains the benefits of a Microsoft .Net application development organization coding in C# versus coding in VB.

Visual Studio Express Editions Beta 2

As we all expected, Microsoft has released beta 2 of the Express Editions. In my opinion, the Express series are an excellent way to familiarize yourself with the new Visual Studio products. They are, of course, not as fully loaded as the Team System versions. But they offer enough to build .Net Windows forms or web applications. Most home users don't need Team System, or don't even have the hardware required to run that version. And even professional developers can learn a lot about the new Visual Studio by downloading and installing the express version.

Links to the download pages can be found in my earlier blog about Visual Studio Express series. The links here show you the top cool things for each express version:

  1. Top 12 Cool Things about Visual Web Developer 2005 Express Edition
  2. Top 14 Cool Things about Visual Basic 2005 Express Edition
  3. Top 10 Cool Things about Visual C# 2005 Express Edition
  4. Top 10 Cool Things about Visual C++ 2005 Express Edition
  5. Top 10 Cool Things about SQL Server 2005 Express Edition
  6. Top 10 Cool Things about Visual J# 2005 Express Edition

I can really recommend these of you don't want to install the full Visual Studio 2005.

Off topic

I bumped into this one tonight, just had to share this with you all. Will this be the way we will communicate with our kids?

The hitchhikers guide to Visual Studio 2005 Team System

Rob Caron posted two interesting articles on his weblog about Visual Studio 2005 Team System, under the title Hitchhikers guide to Visual Studio 2005 Team System. Rob's blog is an excellent source of information on stuff related to Team System. It contains loads of links to other blogs, targeted at various audiences like architects and developers.

Part 1 describes the evolution of Visual Studio, including the Express Editions and Team System. Part 2 describes pricing and licensing of the new versions, with the exception of Team Foundation Server. This, he states, will be in Part 3 which is not yet available. I will add a link to that article as soon as it hits the web :-)

Visual Studio Tip of the Day

This morning I happened to find Sara Ford's blog on MSDN. Don't know why I bumped into this blog, but after reading some of her postings I decided to share it with you. Sara Ford is a tester on the Visual Studio Core team. On of the categories on her blog is about tips for the Visual Studio environment. She posts tips on Visual Studio (2003) as well as 2005.

Here are a few of her tips and tricks:

There are more tips and tricks in her blog. So have look.

Effective C#

Cohen Shwartz Oren wrote an article on Effective C# for the CodeProject web site. He describes a number of ways to improve your C# code. He explains, among others, the following items, complete with very comprehensive examples.

  1. Prefer Length property when checking string size. (performance)
  2. Prefer StringBuilder for string concatenation. (performance)
  3. Avoid Boxing and Unboxing as much as possible. (performance)
  4. Use string.Equal instead of ==. (performance)
  5. Use Native Image Generator in case of long initialization. (performance)
  6. Use for instead of foreach. (performance)

I have not listed all items here. I recommend however to have a look at the full article.

There is one item I disagree with. It is listed here as item 4. The advice is to use string.Equals instead of == when you compare two strings. But a better way to compare two strings for equality is string.Compare. This page in the FxCop documentation explains why.

My first Visual Studio 2003 to 2005 migration

While waiting to get my hands on the new Beta for Visual Studio 2004, I downloaded the Visual C# 2005 Express Edition Beta. I wanted to start migrating some of my class library projects, to see what problems I might run into when I was going to switch from VS2003. As it turned out, migrating from 2003 to 2005 is really easy, at least for my class libraries...

When I selected the 'old' solution in Visual C# 2005, the start-up form for the conversion wizard opened.. It's a standard wizard form, explaining what the wizard will do and so on. Just click the Next button to see what will happen.

The form allows you to create a backup copy of your project. I obviously selected Yes. I've been doing migrations before and I sometimes lost work because a 'Wizard' magically ruined my code. Clicking next shows you a complete list of projects in the solution that will be converted. Nothing left to do but click the finish button. The wizard then starts converting the solution. Once it has finished, the wizard form will show a link to a conversion report.

The report shows you a detailed overview of all the files that have been converted. Any errors or warnings are also displayed. For me, this meant that my entire class library solution was migrated to VS 2005. No errors, no warnings. If all migrations I have planned for the next couple of weeks run this smoothly, then Microsoft has done a great job of making it easy for you to switch from one version to the next.

One thing did bug me though. The Xml documentation in my code contained a seealso tag. To my surprise, the compiler warned me that it could match two different overloads for the method in my seealso tag. And since I always compile with the “Treat warnings as errors” property on, my code did not compile. Why would the compiler complain about a seealso tag in my Xml documentation. Not sure what to think of this.

Must have tools

Everyone knows (or at least should know) about these tools. James Avery wrote this excellent MSDN item about a number of these tools. But since then, I've been pointed at other tools which can be useful in a number of situations. So therefor this post. I will keep it up-to-date with tools I think can be useful when you develop .Net applications. It will not give you an elaborate description of each tool, as in the James Avery item, but it will be a simlpe list with use ful tools. If you want to learn more about these tools, simply follow the links.

  1. NUnit. Allows you to write tests in the language of your choice to test a specific function of your application
  2. TestDriven. Makes it easy to run NUnit tests with a single click, anywhere in your Visual Studio solutions.
  3. NDoc. Automatically generates documentation for your code using the XML generated from your C# XML comments, or the XML comments from VB Commentor.
  4. VB Commentor. Gives VB.Net developers the option to create XML documentation, just like C# developers
  5. Nant. Helps you create a build process for your project.
  6. CodeSmith. A template-based code-generation tool that uses a syntax similar to ASP.NET to generate any type of code or text.
  7. FxCop. A tool that can be used to help create better applications by enabling you to examine an assembly and check it for compliance using a number of different rules. FxCop comes with a set number of rules created by Microsoft, but these rules can be extended.
  8. SnippetCompiler. A small Windows®-based application that allows you to write, compile, and run code.
  9. ASP.Net version switcher. Can be used to switch the version of ASP.NET that a virtual directory is running under.
  10. Visual Studio .Net Project convertor. Can be used to switch a project file from Visual Studio .NET 2002 to Visual Studio .NET 2003
  11. Regulator. A full-featured tool that makes it easy to build and test regular expressions.
  12. .Net Reflector. A class browser and decompiler that can examine an assembly and show you just about all of its secrets.
  13. Reflector.FileDisassembler. An add-in for .Net Reflector to export the disassembled code to files.
  14. DataSet QuickWatch. Allows you to view DataSet information in a grid. For VS 2002/2003.
  15. DataSet Debugger Visualizer. Similar to DataSet QuickWatch, this time for VS 2005.
  16. DevMetrics.  A C# code analysis tool that gathers software metrics on size (files, classes, members, statements, lines, comments) and complexity (cyclomatic code complexity, statements per member, members per class).

Please note that these tools are all freeware or open-source. I'm also aware that some of the things that these tools can do will be integrated into Visual Studio 2005 Team System. But a lot of development is still done using Visual Studio 2003 or 2002. And not everyone will have access to VS2005 Team System.

Solving "Access denied" exceptions in WebServices

This morning I wrote the NUnit test class for a WebService method I am currently building. This method stores data in different data stores and uses other webservices to do this. All these services were running on my machine. But when I started NUnit, the test failed when I tried to make a call to one of the other webservices. All I got was: System.Net.WebException: The request failed with HTTP status 401: Access Denied.

Why was I getting this? I checked the access to the directories, to make sure the ASPNET account had enough access rights, and that was OK. Same for the IUSR account. So I tried to call the webmethod by viewing the .asmx file in the webbrowser. That worked, so why was my webservice getting an Access denied result when my webservice called that method? The code seemed to be fine:

public int GetCustomerId(string name)
{
   int
customerId = -1;

   WebserviceCustomer.Interface customer = new WebserviceCustomer.Interface();
   customerId = customer.GetCustomerIdByName(name);
   return customerId;
}

This made me check the internet to see what might be the problem. What I found was that, even when the user credentials are all correct and identical, you need to set the credentials before making the actual call to the other webservice. This posting in particular on http://www.dotnet247.com pointed me to the solution. The code now looks like this:

public int GetCustomerId(string name)
{
   int
customerId = -1;

   WebserviceCustomer.Interface customer = new WebserviceCustomer.Interface();
   // Make sure the credentials for the webservice call are the current credentials
   customer.Credentials = System.Net.CredentialCache.DefaultCredentials;
   customerId = customer.GetCustomerIdByName(name);
   return customerId;
}

Making the most of Visual Studio 2005

As we are nearing the release of the Beta 2 (and hopefully soon after that, the release version) of VS2005, I started looking at converting some of the tools and frameworks we use at our company. I bumped into this article on MSDN on how to make the most of the new features in VS2005. It's still in a preliminary status, but it has some good information about the following features:

It also features a link to a page on Upgrading applications to Vs2005, but that unfortunately does not (yet) have much information.

Visual Source Safe 2005

Everyone knows by now that Visual Studio 2005 will include Team System and that Team System will include a new Source Safe system, which is based on SQL Server 2005. 

But Microsoft will also release a new version of the 'normal' source safe. The full roadmap for Visual Source Safe can be found here. On that roadmap, you can find the introduction to Visual Source Safe 2005. Some of the new features look promising. I think the following items are especially nice:

  • Remote Web Access over HTTP. Use Visual Studio to connect to Visual SourceSafe databases from anywhere in the world where HTTP or HTTPS Web access is available.
  • Enhanced Performance and Stability. Work faster with significantly improved LAN access speeds, particularly when using Visual SourceSafe along with Visual Studio in slow and remote RAS configurations. While maintaining the compatibility of its current client-based file share architecture, Visual SourceSafe provides significantly faster access speeds for common operations, particularly improving performance in slow and remote network situations. Robust improvements reduce the need to analyze, administer, and repair databases.
  • Increased Capacity. Data storage is increased to 4 GB, and archive limits are also enlarged.

And there's more, so definitely worth taking a look at when you cannot use Team System.

 

Crystal Reports in Visual Studio 2005

When Visual Studio 2005 was announced, I always assumed that Crystal Reports would no longer be integrated. When Reporting Services was introduced, I always assumed that it would replace Crystal Reports for report development. 

But I was wrong! When Visual Studio 2005 will be shipped, they will still integrate and support Crystal Reports. According to this, Crystal Reports 10 will be shipped with VS2005. And it's already available in the beta's. For those of you that have already worked with Crystal Reports in previous versions of VS, check out the new features. And I'm glad, because I used CR in some of my .Net projects and it never let me down.

Using the DataAdapter to add schema information about your table.

There are circumstances where you need to add schema information regarding a table to a DataTable object. You might want to know which columns define the primary key, or you want to know which column is an identify column. Well, here is one option.

Create a command object that the following query:

SqlCommand cmd  = new SqlCommand();
cmd.Connection  = sqlConnection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SET FMTONLY ON; SELECT * FROM [tableName]; SET FMTONLY OFF;"

As you can see, I set the SQL FMTONLY setting to ON. When I then perform the select statement, the statement will not return any data, just the metadata. This is what we want, because we don't know how many rows will be in the result set when we perform this select statement. And we don't want to wait for the query to finish, right?

You then create the DataAdapter. The DataAdapter supports a property MissingSchemaAction. When you set the value for this property to MissingSchemaAction.AddWithKey, the DataAdapter will add all relevant information about the table to the DataTable object. After setting the property, you can fill the dataset in the normal manner:

using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
    // The dataadapter must add the schema information based on the database
    da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
 
    // Fill a DataSet using default values for DataTable names, etc
    DataSet dsInfo = new DataSet();
    da.Fill(dsInfo);
 
    // Do something with all the new meta information 
}

After running this code, your DataTable object will not only contain the basic columns, but also all information on key columns, identity columns and all other information available to the database.

 

Soundex algorithm in C#

For the project I'm working on now, I needed to convert customers from one database to another. While converting, I had to check the customer name against that of a customer retrieved using a webservice. If the name did not match, then that customer was not to be converted to the new database. Not a big thing, but as it turned out, a lot customer names did not match up. But looking at the log created by the conversion, one could assume that they were identical enough to allow conversion anyway.

I started looking for a soundex algorithm. You've all heard of them. SQL server even supports soundex. I found the code for 4 different (!!!) implementations of the soundex algorithm at code project. All are implemented in C#.

By converting the names from each source into soundex values, and then comparing those values, I was able to convert a lot more clients. For example, it matches Janszen and Janssen. To make sure these clients are the same, you will need to perform additional checks. In my case, I could always validate postcode and housenr to make sure I was looking at the same customer.

(First blog posted using Bloggie!)