none
ADO.NET - DataTable / Issue when Removing a column that has been previously renamed RRS feed

  • Question

  • Hi,

    I'm running into an issue while trying to remove a column from a DataTable, when this column has been previously created and renamed from same DataTable.

    Here is the code snipnet. I'm using .NET Frameword 4.5.

    It is pretty simple to reproduce.

    try
                {
                    DataTable data = new DataTable();
                    data.CaseSensitive = false;

                    data.Columns.Add("column1", typeof(string));
                    data.Columns.Add("column2", typeof(string));
                    data.Columns.Add("column3", typeof(string));

                    DataRow newRow = data.NewRow();
                    newRow["column1"] = "AAAAA";
                    newRow["column2"] = "BBBBB";
                    newRow["column3"] = "CCCCC";
                    data.Rows.Add(newRow);

                    data.AcceptChanges();

                    // Rename first column
                    Console.WriteLine("Rename [column1] to [COLUMN1] ...");
                    data.Columns["column1"].ColumnName = "COLUMN1";
                    data.AcceptChanges();

                    // Delete Column
                    Console.WriteLine("Delete column [column1] ...");
                    if (data.Columns.Contains("column1"))
                    {
                        data.Columns.Remove("column1");
                        data.AcceptChanges();
                        // Somehow the column will still partially exists after the 2 above lines
                        // If you look at the DataTable, it is gone, but if you test existance of the column, it will still think it is there
                        if (data.Columns.Contains("column1"))
                        {
                            Console.WriteLine("[column1] still Exists! WHY?");
                        }
                    }

                    // Then i need to Recreate that same column ... of course it will fail
                    Console.WriteLine("Recreate column [column1] ...");
                    data.Columns.Add("column1", typeof(string)); 
                    // this throw Exception as the data seems to still somehow exists in the DataTable
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error=" + ex.Message);
                }
                finally
                {
                    Console.WriteLine("Press any key to exit!");
                    Console.ReadLine();
                }

    I add AcceptChanges call almost everywhere to see if it makes a difference but no diff.

    Any idea?
    Am i doing something wrong?

    Thanks in advance for any comments/help

    Thursday, May 22, 2014 9:36 AM

Answers

All replies

  • Hi Dtf,

    I think this feature is by design.

    data.Columns[0].ColumnName = "COLUMN1";
    
                data.AcceptChanges();
    
                //return the same value:0
    
                int aindex = data.Columns.IndexOf("column1");
    
                int bindex = data.Columns.IndexOf("COLUMN1");
    
    
                data.Columns.RemoveAt(0);
    
                data.AcceptChanges();
    
                //return the same value:-1
    
                int aaaa = data.Columns.IndexOf("column1");
    
                int bbbb = data.Columns.IndexOf("COLUMN1");
    

    If you rename the column, the two name will direct to the same column. After remove column, if you access the row, it will throw exception. So don’t worry about this, it just keep the column reference.

    //the following code will throw exception
    
                Console.WriteLine(data.Rows[0]["column1"].ToString());
    
                Console.WriteLine(data.Rows[0]["COLUMN1"].ToString());
    

    The second problem, I assume you have some misunderstanding about AcceptChanges method. This method works for DataRow.

    When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

    You can find the above from the Remarks of the following link. http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges.aspx. We can see the AcceptChanges method is related to DataRow.

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 23, 2014 2:04 AM
    Moderator
  • Hi Herro,

    Thanks for your time and your reply

    Still i do not understand the behavior. It looks like a bug to me.

    If you add the following lines to your code:

    int aaaa = data.Columns.IndexOf("column1");

                    int bbbb = data.Columns.IndexOf("COLUMN1");

                    if( aaaa == -1 && bbbb == -1 )
                        data.Columns.Add("column1", typeof(string)); 

    It will thrown an exception.

    Then even though the column is suppose to be gone, you can not add it back. This does not happen if you do not rename the column. Somehow the DataTable keeps some reference to the column and i'm thinking it should not.

    Here it means if you rename a column in a DataTable and you remove it. You have no way to add it back.

    It does not seem right.

    Any idea on how to add the column back?

    Thanks in advance

    Nick

    Friday, May 23, 2014 7:58 AM
  • Hi Dtf,

    You can see the source code of DataTable class from http://referencesource.microsoft.com/#System.Data/data/System/Data/DataTable.cs. If possible, you can write your own DataTable to meet your requirement.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 27, 2014 5:29 AM
    Moderator