.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘microsoft visual studio’

An update is available for Visual Studio 2010 SP1 to add support for designing and developing applications for Microsoft .NET Framework 4 Platform Update 1

Symptoms
This article also applies to the following products:Microsoft Visual Studio 2010 SP1 Professional
IntroductionAn update is available for Microsoft Visual Studio 2010 Service Pack 1 (SP1) that adds support for designing and developing applications for Microsoft .NET Framework 4 Platform Update 1.
This update contains the updates for the .NET Framework 4 Platform Update 1 that are described in the following Microsoft Knowledge base (KB) articles: 2478063?(http://support.microsoft.com/kb/2478063/) Microsoft .NET Framework 4 Platform Update 1 – Runtime Update2495593?(http://support.microsoft.com/kb/2495593/) An update is available for Visual Studio 2010 SP1 to add support for designing and developing applications for Microsoft .NET Framework 4 Platform Update 12495638?(http://support.microsoft.com/kb/2495638/) Multi-Targeting Pack for the Microsoft .NET Framework 4 Platform Update 1 is availableFor more information about the specific features that are enabled as parts of the .NET Framework 4 Platform Update 1, click the following article number to view the article in the Microsoft Knowledge Base:
2478063?(http://support.microsoft.com/kb/2478063/) Runtime update for Microsoft .NET Framework 4 Platform Update 1

Resolution
Download information The following files are available for download from the Microsoft Download Center:

Collapse this imageExpand this image
Download thepackage now.(http://go.microsoft.com/fwlink/?LinkID=209198)
For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591?(http://support.microsoft.com/kb/119591/) How to obtain Microsoft support files from online services Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file. Command line switches for this updateFor more information about the various command-line switches that are supported by this update, visit the following MSDN website:
Command-line options that are supported by this update(http://go.microsoft.com/fwlink/?LinkId=195005)For more information about Windows Installer, visit the following MSDN website:
General information about Windows Installer(http://msdn.microsoft.com/en-us/library/Aa367449)PrerequisitesTo install this update, you must have Windows Installer 3.1 or a later version installed.
To obtain the latest version of Windows Installer, visit the following Microsoft website:
Download information for Windows Installer 4.0 Redistributable(http://www.microsoft.com/downloads/en/details.aspx?familyid=5A58B56F-60B6-4412-95B9-54D056D6F9F4&displaylang=en)
Known issuesYou encounter the following known issues if you uninstall any one of the packages that are installed as parts of the .NET Framework 4 Platform Update 1:If you uninstall update 2495593, the Visual Studio toolbox support for State Machine model is removed.If you uninstall update 2495638, the Platform Update target framework and reference assemblies are removed. When you uninstall update 2495638, the uninstall operation regresses IntelliSense and compilations for Platform Update 1 features and for Workflow State Machine Designer.If you uninstall update 2478063, the runtime components that applications for Platform Update 1 require are removed.Restart requirementAfter you apply this update, you do not have to restart the computer unless the files that are being updated are locked or used.
However, you have to restart Visual Studio to display the new toolbox buttons in Visual Studio if Visual Studio 2010 SP1 is running when you install this update.Update removal informationTo remove this update completely, you have to remove the updates that are described in the following articles individually by using Add or Remove Programs in Control Panel:2478063?(http://support.microsoft.com/kb/2478063/) Microsoft .NET Framework 4 Platform Update 1 – Runtime Update2495593?(http://support.microsoft.com/kb/2495593/) An update is available for Visual Studio 2010 SP1 to add support for designing and developing applications for Microsoft .NET Framework 4 Platform Update 12495638?(http://support.microsoft.com/kb/2495638/) Multi-Targeting Pack for the Microsoft .NET Framework 4 Platform Update 1 is availableNote These three updates are installed as parts of the .NET Framework 4 Platform Update 1.
File information The global version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time item in Control Panel.
Collapse this tableExpand this table
File nameFile versionFile sizeDateTimeWorkflowDesigner.pkgdef5,46628-Mar-201118:15WorkflowDesigner.extension.vsixmanifest75928-Mar-201118:15

INFO: Microsoft Application Blocks for .NET – Exception Management Application Block Overview

Symptoms
This article describes the Microsoft Application Blocks for .NET topic “Exception Management Application Block Overview.”
The Exception Management Application Block provides a simple, extensible framework for handling exceptions. Without affecting your application code, you can do the following: Log exception information to the event log by using only one line of application code.Extend the application block by creating your own components that log exception details to other data sources or notify operators.You can use the Exception Management Application Block as a building block in your .NET Framework-based application.
Resolution
“Exception Management Application Block Overview” contains the following sections: Introduction
The Exception Management Application Block is an exception management framework that you can use as a building block in your own .NET Framework-based application. If you use this block, you reduce the quantity of custom error handling code that you must create, test, and maintain. You may also make your application more robust and easier to debug.
The Exception Management Application Block helps you to do the following: Manage exceptions in an efficient and consistent way.Isolate exception management code from business logic code.Handle and log exceptions by using minimal custom code.What Does the Exception Management Application Block Include?
The Exception Management Application Block is made up of three Microsoft Visual Studio .NET projects, full source code, and comprehensive documentation.Downloading and Installing the Exception Management Application Block
A Microsoft Windows Installer file that contains the Exception Management Application Block projects, source code, and comprehensive documentation is available.
The install process creates a Microsoft Application Blocks for .NET menu on your Programs menu. An Exception Management menu appears on the Microsoft Application Blocks for .NET menu. The Exception Management menu includes options to start the documentation and to start the Exception Management Application Block Visual Studio .NET solution.Using the Exception Management Application Block
This section discusses how to use the basic features of the Exception Management Application Block. For additional information about these topics, and other related topics, see the documentation that is included with the Exception Management Application Block download.
The topics that this section discusses include: Publishing ExceptionsReferencing the Exception Management Application BlockConfiguring the Exception Manager Application Block
You control the behavior of the Exception Management Application Block by using the following standard .NET Framework-based XML application configuration files: Web.config (for an ASP.NET application)Appname.exe.config (for a Windows-based application)You can also apply configuration settings at the computer level by using the Machine.config file.Extending the Exception Management Application Block
You may be satisfied with the functionality of the default publisher. The default publisher writes exception details to the Windows event log. However, you may want to develop your own custom publishers to log exception details to alternative locations or to notify operators (possibly through Microsoft Windows Management Instrumentation [WMI]), that an exception has occurred. This section discusses how to extend the functionality of the Exception Management Application Block.Internal Design
This section discusses the main elements of the Exception Management Application Block. The main elements of the Exception Management Application Block are as follows: The BaseApplicationException classThe ExceptionManagerSectionHandler classThe ExceptionManager classThe ExceptionManagerInstaller classThe DefaultPublisher classThe IExceptionPublisher interfaceThe IExceptionXmlPublisher interface

How to use structured exception handling in Visual C# .NET and in Visual C# 2005 or Visual C# 2008

Symptoms
This article describes how to use structured exception handling in Microsoft Visual C# .NET, in Microsoft Visual C# 2005, or in Microsoft Visual C# 2008.

RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need: Visual C# .NET or later versions of Visual C#
This article assumes that you are familiar with the following topics: Visual C# .NET or later versions of Visual C#Microsoft Visual Studio .NETor or later versions of Visual Studio
Structured Exception HandlingVisual C# .NET offers structured exception handling that provides a powerful and a more readable way to handle errors. Structured exception handling enables you to nest error handlers inside other error handlers that are in the same procedure. Structured exception handling uses a block syntax that is similar to the If…Else…End If statement. This makes Visual C# .NET code or code in later versionsof Visual C# more readable and easier to maintain. You can handle exceptions in Visual C# .NET or in later versions Visual C# by using a combination of exception handling statements:trycatchfinallythrow The basic syntax of structured error handling is as follows:

try{// Code that is expected to raise an exception.}catch(Exception e){// Code that can handle an error.}finally{// Code to do any final cleanup.}You can include any valid Visual C# code in the try block, or you can include another try block or another hierarchy of try blocks. When an exception occurs at any point, the common language runtime looks for the nearest try block that encloses this code and does not run any additional lines of code. The control is then passed to a matching catch block, if one exists, and to the associated finally block. You can also specify multiple catch statements so that each catch block handles a specific error.

Catch an ExceptionIn Microsoft Visual Studio.NET, click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
In Microsoft Visual Studio 2005, click Start, point to Programs, point toMicrosoft Visual Studio 2005, and then click Microsoft Visual Studio 2005.
In Microsoft Visual Studio 2008, click Start, point to Programs, point toMicrosoft Visual Studio 2008, and then click Microsoft Visual Studio 2008..On the File menu, point to New, and then click Project.In the New Project dialog box, follow these steps: Under Project Types, click Visual C# Projects.
Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types.Under Templates, click Console Application.In the Name box, type MyConsoleApp.In the Location box, type C:\, and then click OK.Add the following code in the Main() function:

int a = 0; int b = 10; int c = 0; try { a = b / c; } catch (Exception e) { Console.WriteLine(“A run-time error occurred.”); } finally { Console.ReadLine(); }To run the application, click Start on the Debug menu. The code tries to divide a number by 0. This operation is not valid and causes a divide-by-zero error. However, the catch block catches this error, and the Console window displays the following error message:

A run-time error occurredClose the Console window.
Catch Multiple ExceptionsThis section demonstrates how to use multiple catch statements to handle different errors. Open the Console Application project that you created in the “Catch an Exception” section of this article. Replace the existing code in the Main() function with the following code:

int a = 2147483647; int b = 0; int c = 0; try { a = checked(a + 1); } catch (DivideByZeroException e) { Console.WriteLine(“Error: Divide by Zero”, e.Message); } catch (OverflowException e) { Console.WriteLine(“Error: Overflow”, e.Message); } finally { Console.ReadLine(); }This code includes two catch blocks:One catch block catches the previous divide-by-zero error.One catch block catches the new overflow error.To run the application, click Start on the Debug menu. The Console window displays the following error message:

Error: OverflowClose the Console window.Because you cannot always anticipate every error that may occur, you can add a catch block for all exceptions that you cannot anticipate. For example, add the following code before the finally statement to catch any errors that you cannot anticipate andto display the appropriate error message:

catch (Exception e) { Console.WriteLine(“Error: “, e.Message); }On the File menu, click Close Solution.
Throw an ExceptionStructured exception handling uses the catch statement to catch an exception. With structured exception handling, you can also throw an exception. For example, you may find it useful to throw an exception when you perform data validation inside a Property Set procedure, because you may want to throw an error if a business rule is violated. In Microsoft Visual Studio.NET, click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
In Microsoft Visual Studio 2005, click Start, point to Programs, point toMicrosoft Visual Studio 2005, and then click Microsoft Visual Studio 2005.
In Microsoft Visual Studio 2008, click Start, point to Programs, point toMicrosoft Visual Studio 2008, and then click Microsoft Visual Studio 2008..On the File menu, point to New, and then click Project.In the New Project dialog box, follow these steps: Under Project Types, click Visual C# Projects.
Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types.Under Templates, click Console Application.In the Name box, type MyNewConsoleApp.In the Location box, type C:\, and then click OK.On the Project menu, click Add Class.In the Add New Item dialog box, type clsPerson.cs in the Name box, and then click Open.
Note In Visual Studio 2005 or in Visual Studio 2008, click Add.Add the following code in the clsPerson class:

public clsPerson() { } private int mintAge; public int Value; public int Age { get { Age = mintAge; return Age; } set { if(Value > 0) mintAge = Value; else throw new ArgumentException(“Age cannot be negative.”); } }This code creates an Age property. Because a person cannot have a negative age, an error is raised if the user of the class tries to set the Age property to a number that is less than 0. In the Main() function of Class1.cs, add the following code:

clsPerson p = new clsPerson(); try { p.Age = -1; } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.ReadLine(); }To run the application, click Start on the Debug menu. The Console window displays the following error message:

Age cannot be negativeClose the Console window.
Complete Code ListingCatch an Exception

using System;namespace MyConsoleApp{ class Class1 { [STAThread]static void Main(string[] args) { int a = 0; int b = 0; int c = 0; try { a = b / c; } catch (Exception e) { Console.WriteLine(“A run-time error occurred.”); } finally { Console.ReadLine(); } }}}Catch Multiple Exceptions

using System;namespace MyConsoleApp{ class Class1 { [STAThread] static void Main(string[] args) { int a = 2147483647; try { a = checked(a + 1); } catch (DivideByZeroException e) { Console.WriteLine(“Error: Divide by Zero”, e.Message); } catch (OverflowException e) { Console.WriteLine(“Error: Overflow”, e.Message); } catch (Exception e) { Console.WriteLine(“Error: “, e.Message); } finally { Console.ReadLine(); } } }}Throw an Exception

using System;namespace MyNewConsoleApp{ class Class1 { [STAThread] static void Main(string[] args) { clsPerson p = new clsPerson(); try { p.Age = -1; } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.ReadLine(); } } }public class clsPerson { public clsPerson() { } private int mintAge; public int Value; public int Age { get { Age = mintAge; return Age; } set { if(Value > 0) mintAge = Value; else throw new ArgumentException(“Age cannot be negative.”); } } }}

FIX: A System.NullReferenceException exception occurs when you deploy a composite database project in Microsoft Visual Studio Team System 2008 Database Edition GDR

Symptoms
Consider the following scenario in Microsoft Visual Studio Team System 2008 Database Edition GDR: You have a database project that includes tables.You have another database project that includes constraints for the tables in the same solution and you add a reference to the database project that includes tables.You connect both projects to the same target database on a server.Then, you deploy the projects to the database.In this scenario, you receive a System.NullReferenceException exception and then the Visual Studio IDE crashes. If you debug this exception, you receive the following error message:

System.NullReferenceException: Object reference not set to an instance of an object.
Resolution
To resolve this problem, upgrade to Microsoft Visual Studio Team System 2008 Database Edition GDR R2. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
970479?(http://support.microsoft.com/kb/970479/) List of fixed issues in the Microsoft Visual Studio Team System 2008 Database Edition GDR R2
Note Microsoft Visual Studio Team System 2008 Database Edition GDR R2 replaces Microsoft Visual Studio Team System 2008 Database Edition GDR.

FIX: A System.ArgumentException exception occurs when you try to create a Windows form that inherits from a Windows form that contains a dataset

Symptoms
When you try to create a Windows form that inherits from a Windows form that contains a dataset in Microsoft Visual Studio .NET 2003, you may receive the following exception error message:

An unhandled exception of type ‘System.ArgumentException’ occurred in system.data.dll
Additional information: Cannot add primary key constraint since primary key is already set for the table.
Resolution
If the dataset contains nested datatables, Visual Studio .NET incorrectly tries to add additional code to the inherited Windows form to enforce constraints on the inherited dataset. This code tries to add a constraint that already exists on the dataset. Therefore, the behavior that is mentioned in the “Symptoms” section occurs.

FIX: A floating-point exception may occur when an inline function in a C++ application returns an uninitialized floating-point variable in Visual Studio 2005

Symptoms
Consider the following scenario:In Microsoft Visual Studio 2005, you create a C++ application.You do not specify the /fp:except option. Therefore, floating-point exceptions are not enabled in the C++ application.You do not specify the /fp:strict option. Therefore, the strict floating-point model is not enabled in the C++ application.An inline function in the C++ application returns an uninitialized floating-point variable.You compile the C++ application.In this scenario, a floating-point exception may occur when you run the C++ application. Additionally, you may receive the following error message:

0xC0000090: Floating-point invalid operation.Note This problem may occur in debug builds and in release builds.
Resolution
This problem occurs when the uninitialized floating-point variable loads a memory location that contains a signaling not-a-number (NaN) value.