none
Issue with SQLparameter on .net4.5 and higher RRS feed

  • Question

  • Hi Guys,

        

          I recently acrossed an issue with SQLparameter, may be you guys have an insight.

          It never used to be a problem on .net4.0 but have this issue on .net4.5 and higher.

    Sample code:

    Case1: Working code:

            const long id1 = 2147484121;

            var param1 = new System.Data.SqlClient.SqlParameter("@id", id1);


    Case 2: Broken Code:

            var param = new System.Data.SqlClient.SqlParameter("@id", 2147484121);

    Any idea?

    Thank you,

    Ven

    Tuesday, May 13, 2014 4:06 PM

Answers

  • The compiler determines the literal 2147484121 to be an uint (System.UInt32) since you are not using any suffix and inferring a SqlDbType from UInt32 is not supported as stated on MSDN here: http://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.110).aspx

    This is the same for the .NET Framework 4 and 4.5. What you should do is to cast the literal to a long (System.Int64), either by using the cast syntax or by using the L prefix:

    var param = new System.Data.SqlClient.SqlParameter("@id", 2147484121L);
    //OR: var param = new System.Data.SqlClient.SqlParameter("@id", (long)2147484121);

    Then the SqlDbType will be set to BigInt as also stated in table presented on the linked MSDN page above.

    Tuesday, May 13, 2014 8:27 PM

All replies

  • The compiler determines the literal 2147484121 to be an uint (System.UInt32) since you are not using any suffix and inferring a SqlDbType from UInt32 is not supported as stated on MSDN here: http://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.110).aspx

    This is the same for the .NET Framework 4 and 4.5. What you should do is to cast the literal to a long (System.Int64), either by using the cast syntax or by using the L prefix:

    var param = new System.Data.SqlClient.SqlParameter("@id", 2147484121L);
    //OR: var param = new System.Data.SqlClient.SqlParameter("@id", (long)2147484121);

    Then the SqlDbType will be set to BigInt as also stated in table presented on the linked MSDN page above.

    Tuesday, May 13, 2014 8:27 PM
  • Thank's  Magnus for the information,

    I already have a solution for the problem as I mentioned in the question(working code). I was just wondering why it is throwing an error, but your answer explained it.

    Thanks again!!

     


    Ven (Microsoft Forum)

    Wednesday, May 14, 2014 8:54 PM