Microsoft KB Archive/815545

From BetaArchive Wiki

Article ID: 815545

Article Last Modified on 5/13/2007



APPLIES TO

  • Microsoft .NET Framework 1.1 Service Pack 1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition




SYMPTOMS

After you rename the columns of a DataTable object in the DataSet, if you try to refer to the columns in case-insensitive manner, you receive the following exception:

An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: Column 'ColumnName' does not belong to table 'Tablename'.

CAUSE

When you first create a column in a DataTable object of a DataSet, the name is added to two indexes: a case-sensitive index and a case-insensitive index. When you rename the column in a DataTable object, the new name is not added to the case-insensitive index. Therefore, when you rename a DataTable column, the ColumnName becomes case-sensitive.


RESOLUTION

To work around this problem after you rename the columns, copy the Dataset to itself. To do this, rename the columns, and then add the following statement to the code.

Microsoft Visual Basic .NET code

ds=ds.Copy()

Microsoft Visual C# .NET code

ds=ds.Copy();

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

  1. In Microsoft Visual Studio .NET, create a new Console Application by using either Microsoft Visual Basic .NET or Microsoft Visual C# .NET.
  2. Replace the existing code with the following code.

    Visual Basic .NET code

    Module Module1
    
        Sub Main()
          Dim cn As New SqlClient.SqlConnection()
          'Connect to SQLServer. 
          cn.ConnectionString = "data source=YourSQLServer;integrated security=SSPI;persist security info=False;initial catalog=Northwind"
          Dim da As New SqlClient.SqlDataAdapter("select * from products", cn)
          Dim ds As New DataSet()
          da.Fill(ds, "Products")
    
          Dim colname As String
          colname = ds.Tables(0).Columns(0).ColumnName
    
          'Display the data in the first column of the first row.
          Console.WriteLine(ds.Tables(0).Rows(0)(colname.ToLower()))
          Console.ReadLine()
    
          'Change the column names.
          Dim i As Integer
          For i = 0 To ds.Tables(0).Columns.Count - 1
             ds.Tables(0).Columns(i).ColumnName = "C" & i.ToString()
          Next
    
          'Display the data in the first column of the first row .
          colname = ds.Tables(0).Columns(0).ColumnName
          Console.WriteLine(ds.Tables(0).Rows(0)(colname.ToLower()))
          Console.ReadLine()
    
        End Sub
    
    End Module

    Visual C# .NET code

    using System;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace ConsoleApplication1
    {
       class Class1
       {
          [STAThread]
          static void Main(string[] args)
          { 
             SqlConnection cn;
             //Connect to SQLServer.
             cn = new SqlConnection();
             cn.ConnectionString = "data source=YourSQLServer;integrated security=SSPI;persist security info=False;initial catalog=Northwind";
            
             SqlDataAdapter da;
             da = new SqlDataAdapter("select * from products", cn);
              
             DataSet ds;
             ds = new DataSet();
    
             da.Fill(ds, "Products");
    
             String colname;
             colname = ds.Tables[0].Columns[0].ColumnName;
    
            //Display the data in the the first row of the first column.
             Console.WriteLine(ds.Tables[0].Rows[0][colname.ToLower()]);
             Console.ReadLine();
    
             //Change the column names.
             int i;
             for (i =0;i<= (ds.Tables[0].Columns.Count - 1);i=i+1)
                ds.Tables[0].Columns[i].ColumnName = "C" + i.ToString();
                 
             colname = ds.Tables[0].Columns[0].ColumnName;
             //display the data in the first row of the first column           
             Console.WriteLine(ds.Tables[0].Rows[0][colname.ToLower()]);
          }
       }
    }
  3. In the ConnectionString property of the SqlConnection class, replace YourSQLServer with the name of your SQL Server.
  4. On the Debug menu, click Start.

    You receive the exception that is mentioned in the "Symptoms" section.


REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

308656 How to open a SQL Server database by using the SQL Server .NET data provider with Visual Basic .NET


For more information about using .NET Framework data providers to access data, see the documentation in the Microsoft .NET Framework Software Development Kit, or visit the following MSDN Web site:

Keywords: kbtable kbsqlclient kbprovider kbdatabinding kbdatabase kbdataadapter kbprb KB815545