Caution when passing sqlparameter in. Net Environment

Source: Internet
Author: User
Check the followings out, I encounter with an exception when running the following code:

Datalist1.datasource = sqlhelper. executedatatable (commandtype. storedprocedure, "artspgetpainting01", new sqlparameter ("@ isforrecentorsearch", 0 ));


And I get the following message:

Procedure or function 'artspgetpainting01 'expects parameter' @ isforrecentorsearch', which was not supplied.

Open the immediate window and input the command to check the values for testing:

? New sqlparameter ("@ isforrecentorsearch", 1)

{@ Isforrecentorsearch}

Base {system. Data. Common. dbparameter }:{@ isforrecentorsearch}

Compareinfo: None

Dbtype: int32

Direction: Input

Isnullable: false

Localeid: 0

Offset: 0

Parametername: "@ isforrecentorsearch"

Precision: 0

Scale: 0

Size: 0

Sourcecolumn :""

Sourcecolumnnullmapping: false

Sourceversion: Current

Sqldbtype: int

Sqlvalue: {1}

Typename :""

Udttypename :""

Value: 1

Xmlschemacollectiondatabase :""

Xmlschemacollectionname :""

Xmlschemacollectionowningschema :""

? New sqlparameter ("@ isforrecentorsearch", 0)

{@ Isforrecentorsearch}

Base {system. Data. Common. dbparameter }:{@ isforrecentorsearch}

Compareinfo: None

Dbtype: int64

Direction: Input

Isnullable: false

Localeid: 0

Offset: 0

Parametername: "@ isforrecentorsearch"

Precision: 0

Scale: 0

Size: 0

Sourcecolumn :""

Sourcecolumnnullmapping: false

Sourceversion: Current

Sqldbtype: bigint

Sqlvalue: NULL

Typename :""

Udttypename :""

Value: NULL

Xmlschemacollectiondatabase :""

Xmlschemacollectionname :""

Xmlschemacollectionowningschema :""

? New sqlparameter ("@ isforrecentorsearch", (object) 0)

{@ Isforrecentorsearch}

Base {system. Data. Common. dbparameter }:{@ isforrecentorsearch}

Compareinfo: None

Dbtype: int32

Direction: Input

Isnullable: false

Localeid: 0

Offset: 0

Parametername: "@ isforrecentorsearch"

Precision: 0

Scale: 0

Size: 0

Sourcecolumn :""

Sourcecolumnnullmapping: false

Sourceversion: Current

Sqldbtype: int

Sqlvalue: {0}

Typename :""

Udttypename :""

Value: 0

Xmlschemacollectiondatabase :""

Xmlschemacollectionname :""

Xmlschemacollectionowningschema :""

Google exception message, and find the resolution of this issue:

Http://msdn.microsoft.com/zh-cn/library/0881fz2y.aspx
When an object is specified in the value parameter, sqldbtype is inferred from the. NET Framework type of the object.
Use this overload of the sqlparameter constructor to specify the integer parameter value. Because this overload accepts the object type value, when this value is zero, the integer must be converted to the object type, as shown in the C # example below.
Parameter = new sqlparameter ("@ pname", convert. toint32 (0 ));
If this conversion is not performed, the compiler considers that you are trying to call the sqlparameter (string, sqldbtype) constructor overload.

Http://msdn.microsoft.com/en-us/library/0881fz2y.aspx
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.
Parameter = new sqlparameter ("@ pname", convert. toint32 (0 ));
If you do not perform this conversion, the compiler assumes that you are trying to call the sqlparameter (string, sqldbtype) constructor overload.

In briefly, when input '0', the program invoke sqlparameter constructor 'sqlparameter (string, sqldbtype) ', not 'sqlparameter (string, object )', the essential is that zero can not be convert to 'object' value implicitly.

Pertinent info:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.