Home Dashboard Directory Help
Search

SqlParameter constructor does not accept enum:byte values by BetterToday


Status: 

Active


1
0
Sign in
to vote
Type: Bug
ID: 771647
Opened: 11/19/2012 1:29:36 PM
Access Restriction: Public
1
Workaround(s)
view
0
User(s) can reproduce this bug

Description

The SqlParameter(string parameterName, object value) constructor does not correctly read values coming from a byte based enumeration. It always sets null to the SqlValue property.
Details
Sign in to post a comment.
Posted by Microsoft on 8/26/2013 at 5:33 PM
For enum:byte value, incorrect constructor is being called – SqlParameter(string parameterName, System.Data.SqlDbType dbType)

There are three possible workarounds for this issue –
1) Convert.ToByte ensures that the enum.value is treated as an object and calls the correct constructor - SqlParameter(string parameterName, Object value).
            SqlParameter param = new SqlParameter("@sqlparam", Convert.ToByte(MyEnum.Value));

2) Using AddWithValue method, adding the typecast to enum.value, add the parameter to the command.parameters collection.
            command.Parameters.AddWithValue("@sqlparam", (byte)MyEnum.Value);

3) Or as mentioned on the MSDN page for SqlParameter Constructor (String, Object) , you may also explicitly typecast it into an object (to force the overload) –

When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object.
Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.
Copy
Parameter = new SqlParameter("@pname", (object)0);
If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameter (string, SqlDbType) constructor overload.

So that would look something like –
SqlParameter param = new SqlParameter("@sqlparam", (object)((byte)MyEnum.Value));
Posted by Microsoft on 11/27/2012 at 11:06 PM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Microsoft on 11/20/2012 at 12:13 AM
Thank you for submitting feedback on Visual Studio and .NET Framework. In order to efficiently investigate and reproduce this issue, we are requesting a demo project. Please submit this information to us within 3 business days. We look forward to hearing from you with this information.
Posted by Microsoft on 11/19/2012 at 1:52 PM
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)
Sign in to post a workaround.
Posted by Microsoft on 8/26/2013 at 5:36 PM
For enum.byte value, the incorrect constructor is being called – SqlParameter(string parameterName, System.Data.SqlDbType dbType)

There are three possible workarounds for this issue –
1) Convert.ToByte ensures that the enum.value is treated as an object and calls the correct constructor - SqlParameter(string parameterName, Object value).
            SqlParameter param = new SqlParameter("@sqlparam", Convert.ToByte(MyEnum.Value));

2) Or using AddWithValue method, adding the typecast to enum.value, add the parameter to the command.parameters collection.
            command.Parameters.AddWithValue("@sqlparam", (byte)MyEnum.Value);

3) Or as mentioned on the MSDN page for SqlParameter Constructor (String, Object) , you may also explicitly typecast it into an object (to force the overload) –

When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object.
Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.
Copy
Parameter = new SqlParameter("@pname", (object)0);
If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameter (string, SqlDbType) constructor overload.

So that would look something like –
SqlParameter param = new SqlParameter("@sqlparam", (object)((byte)MyEnum.Value));