Microsoft KB Archive/815545

= An unhandled exception of the 'System.ArgumentException' type occurs after you rename the columns of a DataSet object =

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.



Steps to reproduce the behavior
 In Microsoft Visual Studio .NET, create a new Console Application by using either Microsoft Visual Basic .NET or Microsoft Visual C# .NET.  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 = &quot;data source=YourSQLServer;integrated security=SSPI;persist security info=False;initial catalog=Northwind&quot; Dim da As New SqlClient.SqlDataAdapter(&quot;select * from products&quot;, cn) Dim ds As New DataSet da.Fill(ds, &quot;Products&quot;)

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 = &quot;C&quot; & 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 = &quot;data source=YourSQLServer;integrated security=SSPI;persist security info=False;initial catalog=Northwind&quot;; SqlDataAdapter da; da = new SqlDataAdapter(&quot;select * from products&quot;, cn); DataSet ds; ds = new DataSet;

da.Fill(ds, &quot;Products&quot;);

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 = &quot;C&quot; + 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]); }  } }  In the ConnectionString property of the SqlConnection class, replace  with the name of your SQL Server. On the Debug menu, click Start.

You receive the exception that is mentioned in the &quot;Symptoms&quot; section.

<div class="references_section">