Caution when passing sqlparameter in .net environment
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
当在 value 参数中指定 Object 时,SqlDbType 将从 Object 的 .NET Framework 类型推断出。
请小心使用 SqlParameter 构造函数的这个重载来指定整数参数值。因为此重载接受 Object 类型的 value,所以当此值为零时,必须将整数值转换为 Object 类型,如下面的 C# 示例所示。
Parameter = new SqlParameter("@pname", Convert.ToInt32(0));
如果不执行该转换,则编译器将认为您尝试调用 SqlParameter(string、SqlDbType)构造函数重载。
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: