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 codeModule 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 codeusing 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:
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
| 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 |
|
|
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please