Bug 26546 - Issue when using null value for integer and datetime in SqlParameter
Summary: Issue when using null value for integer and datetime in SqlParameter
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data ()
Version: 3.12.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-29 11:44 UTC by Robert Rascalon
Modified: 2017-09-01 09:30 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Robert Rascalon 2015-01-29 11:44:17 UTC
Hi

Using null value or DBNull.value in SqlParameter (for integer type and DateTime) is causing an exception.

Code example:

            var now = DateTime.Now; 

            var query = session.GetNamedQuery("SearchByCriteria");

	    query.SetParameter("PageSize", criteria.PageSize, NHibernateUtil.Int32);
            query.SetParameter("PageNumber", criteria.PageNumber, NHibernateUtil.Int32);
            query.SetParameter("SortColumnName", criteria.SortColumnName, NHibernateUtil.String);
            query.SetParameter("SortDirection", criteria.SortColumnOrder, NHibernateUtil.String);
            query.SetParameter("LastName", criteria.LastName, NHibernateUtil.String);
            query.SetParameter("FirstName", criteria.FirstName, NHibernateUtil.String);
	    query.SetParameter("BirthDate", criteria.BirthDate, NHibernateUtil.DateTime);
	    query.SetParameter("BookingNumber", criteria.BookingNumber, NHibernateUtil.Int32);

            ...

            var dbResult = query.List<SearchResult>();

with:
    - criteria.BookingNumber = null and/or criteria.BirthDate = null


See exception below: (case when criteria.BirthDate, same when using nullable integer0

inner_exception	{System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 11 ("@p8"): Data type 0x6F has an invalid data length or metadata length.   at System.Data.SqlClient.SqlConnection.ErrorHandler (System.Object sender, Mono.Data.Tds.Protocol.TdsInternalErrorMessageEventArgs e) [0x00032] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Data/System.Data.SqlClient/SqlConnection.cs:337    at Mono.Data.Tds.Protocol.Tds.OnTdsErrorMessage (Mono.Data.Tds.Protocol.TdsInternalErrorMessageEventArgs e) [0x0000b] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/Mono.Data.Tds/Mono.Data.Tds.Protocol/Tds.cs:1721    at Mono.Data.Tds.Protocol.Tds.ProcessMessage (TdsPacketSubType subType) [0x000ef] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/Mono.Data.Tds/Mono.Data.Tds.Protocol/Tds.cs:1761    at Mono.Data.Tds.Protocol.Tds.ProcessSubPacket () [0x0012a] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/Mono.Data.Tds/Mono.Data.Tds.Protocol/Tds.cs:1813    at Mono.Data.Tds.Protocol.Tds.NextResult () [0x0004a] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/Mono.Data.Tds/Mono.Data.Tds.Protocol/Tds.cs:643    at System.Data.SqlClient.SqlDataReader.NextResult () [0x0003b] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Data/System.Data.SqlClient/SqlDataReader.cs:1515    at System.Data.SqlClient.SqlDataReader..ctor (System.Data.SqlClient.SqlCommand command) [0x00019] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Data/System.Data.SqlClient/SqlDataReader.cs:113    at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlDataReader:.ctor (System.Data.SqlClient.SqlCommand)   at System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior) [0x0003c] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Data/System.Data.SqlClient/SqlCommand.cs:597 }	System.Data.SqlClient.SqlException
Comment 1 Jonathan Braswell 2015-03-27 15:04:50 UTC
This has been fixed by https://github.com/mono/mono/commit/18fe8197084960179d6844416ffe94c5e79f1f93
Comment 2 Andrew Katz 2015-04-18 08:55:52 UTC
This fix resolved a number of my exceptions, but one remains, perhaps you can fix it as well?

We are calling into a stored procedure and passing NULL into a NVarChar parameter.

2015-04-18 12:29:04|13|E|CacheBuilder|an unexpected exception occurred attempting to build the cache. System.Data.SqlCli
ent.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Param
eter 2 ("@ModuleName"): Data type 0xE7 has an invalid data length or metadata length.
  at System.Data.SqlClient.SqlConnection.ErrorHandler (System.Object sender, Mono.Data.Tds.Protocol.TdsInternalErrorMess
ageEventArgs e) [0x00000] in <filename unknown>:0
  at Mono.Data.Tds.Protocol.Tds.OnTdsErrorMessage (Mono.Data.Tds.Protocol.TdsInternalErrorMessageEventArgs e) [0x00000]
in <filename unknown>:0
  at Mono.Data.Tds.Protocol.Tds.ProcessMessage (TdsPacketSubType subType) [0x00000] in <filename unknown>:0
  at Mono.Data.Tds.Protocol.Tds.ProcessSubPacket () [0x00000] in <filename unknown>:0
  at Mono.Data.Tds.Protocol.Tds.NextResult () [0x00000] in <filename unknown>:0
  at System.Data.SqlClient.SqlDataReader.NextResult () [0x00000] in <filename unknown>:0
  at System.Data.SqlClient.SqlDataReader..ctor (System.Data.SqlClient.SqlCommand command) [0x00000] in <filename unknown
>:0
  at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlDataReader:.ctor (System.Data.SqlClient.SqlCommand)
  at System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior) [0x00000] in <filename unknown>:0
Comment 3 Alexander Köplinger [MSFT] 2016-11-18 19:09:54 UTC
The original problem was fixed some time ago with https://github.com/mono/mono/pull/1664.

Please file new bugs for any other issues you see, thanks!