.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 ‘unhandled exception’

PRB: “System.Reflection.TargetInvocationException” Error Message When You Call the MethodInfo.Invoke Method

Symptoms
When you call the MethodInfo.Invoke method, you may receive the following error message:

An unhandled exception of type ‘System.Reflection.TargetInvocationException’ occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.
You may also receive an additional error message that is similar to the following error message:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.IO.FileLoadException: ‘ClassLibrary1′ is not a valid file.
File name: “ClassLibrary1″
at ClassLibrary2.Class1.GetString()
Resolution
You receive the System.Reflection.TargetInvocationException error because the common language runtime calls the MethodInfo.Invoke method by using reflection.
When you load an assembly by using the Assembly.LoadFrom method, the common language runtime places the loaded assembly in the LoadFrom context of your application. Any probes for the dependencies of the assembly first probe the current application directory. If this probe fails, the common language runtime then probes the LoadFrom context of your application.
You may load an assembly that has a simply-named dependency that has the same file name as a file in the current application directory. When you try to invoke a method in the loaded assembly by using the MethodInfo.Invoke method, and the invoked method uses the dependency, the common language runtime first probes the current directory path. When the common language runtime finds a file that has the same file name as the dependency, the probe stops. However, if this file does not have the same assembly identity as the dependency, the assembly bind fails, and the common language runtime generates a System.IO.FileLoadException error that is passed to the System.Reflection.TargetInvocationException error. Therefore, you may notice the behavior that is mentioned in the “Symptoms” section.

FIX: The System.EnterpriseServices.TransactionProxyException exception cannot be caught from other application domains when it is triggered during a transaction completion

Symptoms
When a System.EnterpriseServices.TransactionProxyException exception is triggered during a transaction completion, it cannot be caught from other application domains. Instead, you receive a System.Runtime.Serialization.SerializationException exception that resembles the following:

Unhandled Exception: System.Runtime.Serialization.SerializationException: Type ‘System.EnterpriseServices.TransactionProxyException’ in Assembly ‘System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ is not marked as serializable.
Resolution
This problem occurs because the TransactionProxyException exception is not serialized, and then cannot be caught from another application domain.

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.

Error message when you use CommandBuilder: “An unhandled exception of type ‘System.NullReferenceException’ occurred”

Symptoms
If you use the CommandBuilder object to explicitly get commands for the DataAdapter object as follows:

da.InsertCommand = cb.GetInsertCommand and then run the following Visual Basic .NET code

cb.DataAdapter = Nothing or the following Visual C# .NET code

cb.DataAdapter = null; the commands that you add to the DataAdapter are deleted, and you receive the following error message:

An unhandled exception of type ‘System.NullReferenceException’ occurred in app_name.exe
Additional information: Object reference not set to an instance of an object.
Resolution
CommandBuilder deletes the commands that it generates when it is disassociated from a DataAdapter. CommandBuilder and DataAdapter are linked; when they are unlinked or disassociated, the commands are nulled. This problem does not affect commands that you build from the beginning (from scratch).

Error message when you update a database by using a stored procedure: “System.Data.DBConcurrencyException”

Symptoms
When you use a DataAdapter object to write changes back to a database by means of a stored procedure, you may receive the following error message:

An unhandled exception of type ‘System.Data.DBConcurrencyException’ occurred in system.data.dll
Additional information: Concurrency violation: the UpdateCommand affected 0 records. If the data is not updated correctly, you should receive a DataConcurrencyException exception instead of the above-mentioned error message.
Resolution
This problem can occur if the stored procedure runs the SET NOCOUNT ON statement.

Error message when you try to view the service calendar in Microsoft Dynamics CRM 3.0: “An unhandled exception was generated during the execution of the current web request”

Symptoms
You cannot view the service calendar in Microsoft Dynamics CRM 3.0. Additionally,you receive the following error message:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Resolution
This problem occurs when you have working hours set in Microsoft Dynamics CRM 3.0 and when one of the following conditions is true: You change time zones on your computer.You change from daylight saving time to standard time,or you change fromstandard time to daylight saving time.