none
SqlParameter() constructor fails when the value specified as a constant. RRS feed

  • Question

  • Hi,
    I am using the constructor of SqlParameter to create a new SqlParameter object.
    When I try to initialise it with the call as below, the value of SqlParameter object is initialised to null.

    const Int32 ABC_VAL=0;
    // Array of parameters for stored procedures
    SqlParameter[] paramArray = new SqlParameter[3];
    paramArray[0] = new SqlParameter("@ABC",ABC_VAL);

    But if I create an object as per below method, value is initialised to the correct one i.e. '0'

    paramArray[0] = new SqlParameter("@ABC",SqlDbType.BigInt);
    paramArray[0].Value = ABC_VAL;

    My question is: In first method, why does the value property of SqlParameter gets initialised to null? Or do I always have to use the second method for initialising SQLParameter value?

    Regards,
    Paresh

    Thursday, July 2, 2009 1:23 PM

Answers

  • This is because your value of 0 is recognized as a enum value for the datatype (as in a second example that passes BigInt) not as an actual value. And, of course, value set to default DBNull value.
    Val Mazur (MVP) http://www.xporttools.net
    Friday, July 3, 2009 12:05 PM
    Moderator
  • This is because of a breaking change in the C# compiler. See http://msdn.microsoft.com/en-us/library/cc713578.aspx change number 12.

    A literal 0 is implicitly convertible to any enum type. In Visual C# 2005 and earlier versions of the compiler, there are also some constant expressions that evaluate to 0 that can implicitly convert to any enum type, but the rule that determines which of these expressions are convertible is unclear. In Visual C# 2008, all constant expressions that are equal to 0 can be implicitly converted to any enum type.


    Postings are provided "As Is" with no warranties and confer no rights.
    Tuesday, July 7, 2009 11:01 PM

All replies

  • This is because your value of 0 is recognized as a enum value for the datatype (as in a second example that passes BigInt) not as an actual value. And, of course, value set to default DBNull value.
    Val Mazur (MVP) http://www.xporttools.net
    Friday, July 3, 2009 12:05 PM
    Moderator
  • Hi Mazur,

    Thanks for your reply.

    Previously I forgot to mention that the code in the first scenario fails on the .NET framework 3.5 whereas the same code works for .NET 1.1.
    I am really confused why such thing happens as the value of the enum will get replaced at the constructor call, isn't it?

    Thanks once again and regards,
    Paresh



    Friday, July 3, 2009 12:11 PM
  • This is because of a breaking change in the C# compiler. See http://msdn.microsoft.com/en-us/library/cc713578.aspx change number 12.

    A literal 0 is implicitly convertible to any enum type. In Visual C# 2005 and earlier versions of the compiler, there are also some constant expressions that evaluate to 0 that can implicitly convert to any enum type, but the rule that determines which of these expressions are convertible is unclear. In Visual C# 2008, all constant expressions that are equal to 0 can be implicitly converted to any enum type.


    Postings are provided "As Is" with no warranties and confer no rights.
    Tuesday, July 7, 2009 11:01 PM