DataSet column mapping throws an InvalidOperationException exception when the specified column is missing in ADO.NET

Article translations Article translations
Article ID: 307170 - View products that this article applies to.
This article was previously published under Q307170
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C# .NET version of this article, see 307989.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Data
Expand all | Collapse all

On This Page

SYMPTOMS

When you use DataSet column mapping in ADO.NET, an InvalidOperationException exception is thrown if the MissingMappingAction property is set to Error and 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 "More Information" section for code that checks whether the specified column mapping exists.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  3. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
  4. Place a Button control on Form1, and change its Name property to btnTest.
  5. Use the Imports 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.
    Imports System
    Imports System.Data
    Imports System.Data.Common
    Imports System.Data.OleDb
    					
  6. Paste the following code in the Code window after the "Windows Form Designer generated code" region:
        Private Sub btnTest_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnTest.Click
            Dim myDataAdapter As New OleDbDataAdapter()
            myDataAdapter.TableMappings.Add("Categories", "DataCategories")
    
            Dim myMessage As String = "Table Mappings:" + ControlChars.Cr
            Dim i As Integer
            For i = 0 To myDataAdapter.TableMappings.Count - 1
                myMessage += i.ToString() + " " _
                   + myDataAdapter.TableMappings(i).ToString() + ControlChars.Cr
            Next i
            MessageBox.Show(myMessage)
    
            Dim myNewMapping As New DataTableMapping()
    
            'Uncomment the if loop to check if the column mapping that you specify exists.
            'If myDataAdapter.TableMappings.Contains("Categories1") Then
                myNewMapping = DataTableMappingCollection.GetTableMappingBySchemaAction _
                   (myDataAdapter.TableMappings, "Categories1", "", MissingMappingAction.Error)
            'End If
        End Sub
    					
  7. Save your project. On the Debug menu, click Start, and then run your project.
  8. Click the button. You receive the above-mentioned exception.
  9. To resolve this problem, uncomment the "if" loop in the preceding sample code, and then run the project again. Because the "if" loop checks whether the specified column mapping exists, you no longer receive the exception.

REFERENCES

For more information on ADO.NET objects and syntax, refer to the following Microsoft .NET Framework Software Development Kit (SDK) documentation:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx

Properties

Article ID: 307170 - Last Review: February 27, 2014 - Revision: 1.9
APPLIES TO
  • Microsoft ADO.NET 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 2.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbnosurvey kbarchive kbtshoot kberrmsg kbnofix kbprb kbreadme kbsystemdata KB307170

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com