locked
DataRowVersion Default throw and exception for deleted RowState RRS feed

  • Question

  • Hi,

    From msdn and other sources, it looks like if a datarow is state of deleted it should have a default version, which is the same as current version.  However, in my test, the only version available is version original, no other version, including default version is available.  I am in VS2010 on Win 7.  My codes/results are listed below, so I want to know whether there is something wrong in msdn, or my particular sample is not working

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Data.EntityClient;
    using System.Data;
    using System.Data.Objects;
    using System.Data.Common;

    namespace Sample
    {
        class Program
        {
            static void Main(string[] args)
            {

                DataTable dt = new DataTable();
                DataColumn dc = new DataColumn("ID", typeof(string));
                dt.Columns.Add(dc);

                DataRow dr = dt.NewRow();
                dr["ID"] = "123";
                dt.Rows.Add(dr);
                dt.AcceptChanges();

                dr.Delete();
                Console.WriteLine(dr.RowState);

                try
                {
                    Console.WriteLine("original version: {0}", dr["ID", DataRowVersion.Original]);
                }
                catch (Exception)
                {
                    Console.WriteLine("original version is not available!");
                }
                try
                {
                    Console.WriteLine("current version: {0}", dr["ID", DataRowVersion.Current]);
                }
                catch (Exception)
                {
                    Console.WriteLine("current version is not available!");
                }
                try
                {
                    Console.WriteLine("proposed version: {0}", dr["ID", DataRowVersion.Proposed]);
                }
                catch (Exception)
                {
                    Console.WriteLine("proposed version is not available!");
                }
                try
                {
                    Console.WriteLine("default version: {0}", dr["ID", DataRowVersion.Default]);
                }
                catch (Exception)
                {
                    Console.WriteLine("default version is not available!");
                }
                Console.In.ReadLine();
            }
        }
    }

     

    Deleted
    original version: 123
    current version is not available!
    proposed version is not available!
    default version is not available

    Thanks,

    Ning

    Wednesday, August 10, 2011 9:09 PM

Answers

All replies

  • It sounds correct to me that the deleted row has no default value - it is deleted and it "doesn't exist" anymore, except for adapter to perform the deletion - where on MSDN did you get that it should have a default state?
    Miha Markic [MVP C#] http://blog.rthand.com
    Thursday, August 11, 2011 8:41 AM
  • Here it is

    http://msdn.microsoft.com/en-us/library/system.data.datarowversion.aspx

    particularly here

    "The default version of DataRowState. For a DataRowState value of Added, Modified or Deleted, the default version is Current. For a DataRowState value of Detached, the version is Proposed."

    It says for "For a DataRowState value of Added, Modified or Deleted, the default version is Current.", so for deleted the default is set to current, and since that is msdn, every where using the same citation ...

     

     

     

    Thursday, August 11, 2011 1:29 PM
  • While it is Current it probably doesn't mean that it is a valid state...which makes sense IMO.
    Miha Markic [MVP C#] http://blog.rthand.com
    Thursday, August 11, 2011 1:39 PM
  • Hello,

    I think this question was related to AcceptChanges method. Regarding Added and Modified state, the RowState become Unchanged, but deleted the row was removed. Please check this for more information. http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Thursday, August 18, 2011 11:17 AM
    Saturday, August 13, 2011 8:38 AM