[Mono-bugs] [Bug 82189][Nor] New - SqlConnection throws an exception if Connection or Transaction is set to null

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Wed Jul 25 17:01:58 EDT 2007


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by tomd at monsooninteractive.com.

http://bugzilla.ximian.com/show_bug.cgi?id=82189

--- shadow/82189	2007-07-25 17:01:58.000000000 -0400
+++ shadow/82189.tmp.23866	2007-07-25 17:01:58.000000000 -0400
@@ -0,0 +1,108 @@
+Bug#: 82189
+Product: Mono: Class Libraries
+Version: 1.2
+OS: All
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: Sys.Data.SqlClient
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: tomd at monsooninteractive.com               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: SqlConnection throws an exception if Connection or Transaction is set to null
+
+Description of Problem:
+Setting the Command or Transaction properties on a
+System.Data.SqlClient.SqlCommand to null results in an exception if the
+object is referenced as an IDbCommand.
+Specifically Connection throws an InvalidCastException and Transaction
+throws an ArgumentException.
+In the Microsoft implementation, these do not throw exceptions.
+See the Additional Information for more on the exceptions.
+
+Steps to reproduce the problem:
+SqlConnection connection  = new SqlConnection (_connectionString);
+IDbCommand idbCommand = new SqlCommand (_query, connection);
+idbCommand.Connection = null;
+idbCommand.Transaction = null;
+
+
+Actual Results:
+ArgumentException/InvalidCastException is thrown.
+
+Expected Results:
+Transaction/Connection set to null.
+
+How often does this happen? 
+Every time.
+
+Additional Information:
+This is an identical problem to bug 78765, except this is with SqlCommand
+instead of OracleCommand.
+I don't know if they are supposed to throw InvalidCastExceptions or
+ArgumentExceptions (the resolution for bug 78765 causes both to throw
+InvalidCastExceptions).
+Below are two possible fixes, the first allows null assignment in the
+current code while allowing the type of exception thrown to be decided. 
+The second uses the same code as the bugfix for OracleCommand, and throws
+InvalidCastException in both cases.
+
+
+The current code is (for SqlConnection, SqlTransaction is similar):
+    IDbConnection IDbCommand.Connection {
+      get { return Connection; }
+      set { 
+        if (!(value is SqlConnection))
+          throw new InvalidCastException ("The value was not a valid
+SqlConnection.");
+        Connection = (SqlConnection) value;
+      }
+    }
+    IDbTransaction IDbCommand.Transaction {
+      get { return Transaction; }
+      set { 
+        if (!(value is SqlTransaction))
+          throw new ArgumentException ();
+        Transaction = (SqlTransaction) value; 
+      }
+	}
+
+
+The new code could be either:
+    IDbConnection IDbCommand.Connection {
+      get { return Connection; }
+      set { 
+        if (!(value == null || value is SqlConnection))
+          throw new InvalidCastException ("The value was not a valid
+SqlConnection.");
+        Connection = (SqlConnection) value;
+      }
+    }
+    IDbTransaction IDbCommand.Transaction {
+      get { return Transaction; }
+      set { 
+        if (!(value == null || value is SqlTransaction))
+          throw new ArgumentException ();
+        Transaction = (SqlTransaction) value; 
+      }
+	}
+
+or
+
+  IDbConnection IDbCommand.Connection {
+    get { return Connection; }
+    set {
+      Connection = (OracleConnection) value;
+    }
+  }
+  IDbTransaction IDbCommand.Transaction {
+    get { return Transaction; }
+    set {
+      Transaction = (SqlTransaction) value;
+    }
+  }


More information about the mono-bugs mailing list