BUG: You receive an error message when you serialize a DBConcurrencyException object in ADO.NET 1.0 (306726)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft .NET Framework
  • Microsoft Windows .NET Framework 1.1

This article was previously published under Q306726
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.IO

SYMPTOMS

When you serialize a DBConcurrencyException object, the value of the Row property in the serialized version of the object is set to a null value and an exception occurs:
An unhandled exception of type 'System.NullReferenceException' occurred in app_name.exe

Additional information: Object reference not set to an instance of an object.

CAUSE

Serialization support is not provided for DataRows in this version of Microsoft Visual Studio .NET.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section of this article.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create a new Microsoft Visual C# .NET Console application.
  2. Paste the following code:
    using System;
    using System.IO;
    using System.Collections;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Data;
    using System.Data.SqlClient;
    
     
    class CRepro
    {
          public static void printObjectState(object obj)
          {
                Console.WriteLine("=== " + obj.GetType().FullName + " ===");
                foreach(PropertyInfo prop in obj.GetType().GetProperties())
                {
                      if (prop.DeclaringType == obj.GetType())
                      {
                            if (prop.GetValue(obj, null).GetType().GetInterface("ICollection") != null)
                            {
                                  Console.WriteLine("=== " + prop.GetValue(obj, null).GetType().FullName + " ===");
                                  foreach(object collectionObj in (ICollection)prop.GetValue(obj, null))
                                  {
                                        printObjectState(collectionObj);
                                  }
                            }
                            else
                            {
                                  Console.WriteLine(prop.Name + "=" + prop.GetValue(obj, null));
                            }
                      }
                }
                Console.WriteLine("===============================");
          }
          public static void genException()
          {
                try
                {
                      SqlConnection conn = new SqlConnection("Data Source=WITSTER; User Id=xxxxxxxxxx; " +
    					"Password=xxxxxxx; Database=Northwind");
                      conn.Open();
                      SqlCommand cmd = new SqlCommand("if exists(select * from sysobjects where name = 'test') "+
    					"drop table test",conn);
                      cmd.ExecuteNonQuery();
                      cmd.CommandText = "create table test (column1 int PRIMARY KEY CLUSTERED)";
                      cmd.ExecuteNonQuery();
                      cmd.CommandText = "Insert into test values(1)";
                      cmd.ExecuteNonQuery();
                      SqlDataAdapter adapterA = new SqlDataAdapter("select * from test","Data Source=WITSTER; "+
    					"User Id=sa; Password=Password1; Database=Northwind");
                     SqlDataAdapter adapterB = new SqlDataAdapter("select * from test","Data Source=WITSTER; "+
    					"User Id=sa; Password=Password1; Database=Northwind");
                      DataSet dataSetA = new DataSet();
                      DataSet dataSetB = new DataSet();
                      SqlCommandBuilder builderA = new SqlCommandBuilder(adapterA);
                      SqlCommandBuilder builderB = new SqlCommandBuilder(adapterB);
                      adapterA.Fill(dataSetA);
                      adapterB.Fill(dataSetB);
                      dataSetA.Tables[0].Rows[0].Delete();
                      adapterA.Update(dataSetA);
                      dataSetB.Tables[0].Rows[0].Delete();
                      adapterB.Update(dataSetB);                            
                }
                catch (Exception e)
                {
                      printObjectState(e);
                      Stream stream = File.Open("DBException.bin", FileMode.Create);
                      BinaryFormatter bformatter = new BinaryFormatter();
                      bformatter.Serialize(stream, e);
                      stream.Close();
                      // Deserialize
                      stream = File.Open("DBException.bin", FileMode.Open);
                      bformatter = new BinaryFormatter();
                      Exception eAfter = (Exception)bformatter.Deserialize(stream);
                      stream.Close();
                      printObjectState(eAfter);
                }
          }
          public static int Main(String[] args)
          {
                genException();
                return 0;
          }
    }
    					
  3. Replace the data source, the user ID, and the password with the appropriate values.
  4. Press F5 to run the application.

Modification Type:MinorLast Reviewed:3/10/2006
Keywords:kbbug kbnofix kbreadme kbSqlClient kbSystemData KB306726