Microsoft KB Archive/307989

= DataSet column mapping throws an InvalidOperationException exception when the specified column is missing in a Visual C# application =

Article ID: 307989

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 2.0
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q307989



For a Microsoft Visual Basic .NET version of this article, see 307170.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data.Common
 * System.Data.OleDb



SYMPTOMS
When you use DataSet column mapping in ADO.NET, an InvalidOperationException exception is thrown if the MissingMappingAction property is set to Error, and if the column mapping that you specify is missing.



RESOLUTION
To resolve this problem, ensure that the column mapping that you specify exists. Refer to the &quot;More Information&quot; section for code that checks whether the specified column mapping exists.



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual C# .NET or Visual C# 2005. Form1 is added to the project by default. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not. Place a Command button on Form1, and change its Name property to btnTest .  Use the using statement on the System, System.Data, System.Data.Common, and System.Data.OleDb namespaces so that you are not required to qualify declarations in those namespaces later in your code. using System; using System.Data; using System.Data.Common; using System.Data.OleDb;   Switch to Form view, and then double-click the button to add the Click event handler. Add the following code to the handler: OleDbDataAdapter myDataAdapter = new OleDbDataAdapter; myDataAdapter.TableMappings.Add(&quot;Categories&quot;, &quot;DataCategories&quot;);

String myMessage; myMessage = &quot;Table Mappings: \n&quot;; System.Int32 i;   for (i=0 ; i <= myDataAdapter.TableMappings.Count -1 ; i++) {       myMessage += i.ToString + &quot; &quot; + myDataAdapter.TableMappings[i].ToString; }   System.Diagnostics.Debug.WriteLine(myMessage);

DataTableMapping myNewMapping1 = new DataTableMapping; //Uncomment the following if statement to check if the specified column mapping exists. //if (myDataAdapter.TableMappings.Contains(&quot;Categories1&quot;)) {       myNewMapping1 = DataTableMappingCollection.GetTableMappingBySchemaAction (myDataAdapter.TableMappings, &quot;Categories1&quot;, &quot;&quot;,           MissingMappingAction.Error); }                   </li> Save your project. On the Debug menu, click Start, and then run your project.</li> Click the button. Notice that you receive the above-mentioned exception.</li> To resolve this problem, uncomment the &quot;if&quot; loop in the sample code, and then run the project again. Because the &quot;if&quot; loop checks whether the specified column mapping exists, you no longer receive the exception.</li></ol>

<div class="references_section">