Microsoft Knowledge Base Email Alertz

KBAlertz.com: (815545) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 2.0 Web Hosting with SQL 2005: Click Here!
Discount ASP.NET Hosting


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




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

Article ID:815545
Last Review:May 13, 2007
Revision:1.4
On This Page

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'.

Back to the top

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.

Back to the top

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();

Back to the top

STATUS

This behavior is by design.

Back to the top

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.

Back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
308656 (http://kbalertz.com/Feedback.aspx?kbNumber=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:
http://msdn2.microsoft.com/en-us/library/s7ee2dwt(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/s7ee2dwt(vs.71).aspx)

Back to the top


APPLIES TO
Microsoft ADO.NET (included with the Windows .NET Framework 1.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

Back to the top

Keywords: 
kbtable kbsqlclient kbprovider kbdatabinding kbdatabase kbdataadapter kbprb KB815545

Back to the top

       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

bala - mbala38 NOSPAM-AT-NOSPAM hotmail.com Reported as Irrelevant  
Written: 5/10/2004 7:40 PM
i am not able to run the aspx code using localhost\filename.asapx. my asp files are showing results but not aspx. can anyone help me in this regard.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please