SQL InternalSmiConnection threw an exception of type 'System.InvalidCastException - by Jack_Sacramento

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 541609 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/12/2010 11:54:40 AM
Access Restriction Public


VS 2008 Professional SP1
Microsoft SQL Server 2005 Standard Edition Version 9.00.3353.00

VS console app ExecuteNonQuery() causes error in SQL

EventSink = '_updateID.EventSink' threw an exception of type 'System.InvalidCastException'

{"Unable to cast object of type 'System.Data.SqlClient.SqlInternalConnectionTds' to type 'System.Data.SqlClient.SqlInternalConnectionSmi'."}

Insert succeeds but _updateID.ExecuteNonQuery() returns -1

_updateID {System.Data.SqlClient.SqlCommand} contains SQL error response in
Non-Public Members
       EventSink '_updateID.EventSink' threw an exception of type 'System.InvalidCastException' System.Data.SqlClient.SqlCommand.CommandEventSink {System.InvalidCastException}
Sign in to post a comment.
Posted by Jack_Sacramento on 3/15/2010 at 2:35 PM
Please close this incident.
Posted by Jack_Sacramento on 3/15/2010 at 2:30 PM
Thanks for the help Microsoft team.
Posted by Nissim [MSFT] on 3/15/2010 at 2:24 PM

ExecuteNonQuery returns -1 because the stored procedure you are running has "SET NOCOUNT ON" in the beginning. This optimization stops the message that shows the count of the number of rows affected by a Transact-SQL statement or stored procedure from being returned as part of the result set. Thus, ExecuteNonQuery does not receive this information from SQL Server and returns -1.

This behavior is by design. Please refer to SQL Books online for more details about NOCOUNT option here: http://msdn.microsoft.com/en-us/library/ms189837.aspx.

I also mentioned the solution in Workaround section.

Note for the exception message in debugger's Watch or Locals window (EventSink in non-public properties of SqlCommand) - this is a different issue. EventSink property is internally used in a different scenario (SQL context connections) and is never used for regular connectiosn to SQL Server. Since debugger queries all properties of the object, EventSink raises exception as expected.

Thanks for your report - let us know if you have more questions.

Nissim Natanov,
SQL Connectivity Team, Microsoft
Posted by Microsoft on 3/15/2010 at 1:49 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Posted by Jack_Sacramento on 3/12/2010 at 2:23 PM
Why is this exception not thrown back to the application?
Posted by Jack_Sacramento on 3/12/2010 at 2:11 PM
More info from stack trace. Looks like the error is thrown in the System.Data.SQLClient

StackTrace " at System.Data.SqlClient.SqlCommand.get_InternalSmiConnection()\r\n at System.Data.SqlClient.SqlCommand.get_EventSink()"    string

I have reproduced using a different stored procedure.
Posted by Jack_Sacramento on 3/12/2010 at 12:06 PM
MS please contact me regarding the attached file.